python SOLID
-
Interface Segregation Principlepython/SOLID 2023. 12. 6. 23:20
정의 No client should be forced to depend on methods it does not use 위반코드 from abc import ABC, abstractmethod class Phone(ABC): @abstractmethod def call(self, number): pass @abstractmethod def swipe_to_unlock(self): pass class IPhone(Phone): def call(self, number): print(f"Calling Number: {number} from iPhone") def swipe_to_unlock(self): print("iPhone is unlocked") Phone interface 는 전화(call) 와 잠금 ..
-
Liskov Substitution Principlepython/SOLID 2023. 12. 6. 22:53
정의 Subclasses should NOT change the behavior of superclasses in unexpected ways. Selecting on types 유형 위반 코드 class Employee: def __init__(self, name): self.name = name class Manager(Employee): def __init__(self, name, department): super().__init__(name) self.department = department def print_employee(e): if type(e) is Employee: print(f"{e.name} is an employee") elif type(e) is Manager: print(f"{..
-
Open-closed Principlepython/SOLID 2023. 12. 6. 20:27
정의 Open for extension but closed for modification. 위반 코드 class Employee: def __init__(self, name): self.name = name class Manager(Employee): def __init__(self, name, department): super().__init__(name) self.department = department def print_employee(e): if type(e) is Employee: print(f"{e.name} is an employee") elif type(e) is Manager: print(f"{e.name} leads department {e.department}") print_empl..
-
Single Responsibility Principlepython/SOLID 2023. 12. 5. 20:11
정의 Things should have only one reason to change. Mixing Resposibility 유형 위반 코드 class Employee: xml_filename = "emp.xml" def __init__(self, name, salary): self.name = name self.salary = salary def raise_salary(self, factor): return self.salary * factor def save_as_xml(self): with open(self.xml_filename, "w") as file: file.write(f"{self.name}") 위의 Employee 클래스는 business logic (raise_salary) 와 stor..