ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Single Responsibility Principle
    python/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"<xml><name>{self.name}</name></xml>")

     

    위의 Employee 클래스는

    • business logic (raise_salary) 와 storage logic (save_as_xml) 이 혼재되어 있다. 클래스 이름이 EmployeeAndStorage 가 더 어울릴 것 같다. 클래스 이름만으로 두가지 역할을 하고 있음을 알 수 있다.
    • storage logic 을 위해 xml_filename 과 save_as_xml() 이 분리되어 있다. 만약 xml 포맷이 아니라 json 포맷으로 저장하기로 바꿀 때, save_as_xml() 함수는 제거했는데 깜빡하고 xml_filename 변수는 제거하지 않을 수 있다.

    개선 코드

    class Employee:
        def __init__(self, name, salary):
            self.name = name
            self.salary = salary
    
        def raise_salary(self, factor):
            return self.salary * factor
    
    
    class Storage:
        xml_filename = "emp.xml"
    
        def save_as_xml(self, employee):
            with open(self.xml_filename, "w") as file:
                file.write(f"<xml><name>{employee.name}</name></xml>")
    
    
    if __name__ == '__main__':
        employee = Employee("Vera", 2000)
        storage = Storage()
        storage.save_as_xml(employee)

     

     

    Dependencies in libraries 유형

    위반 코드

    import jsonlibrary
    
    class Employee:
        def __init__(self, name, salary):
            @jsonlibrary.jsonserializable
            self.name = name
    
            @jsonlibrary.jsonserializable
            self.salary = salary
    
        def raise_salary(self, factor):
            return self.salary * factor
    
    
    class Storage:
        json_filename = "emp.json"
    
        def save_as_json(self, employee):
            jsonlibrary.save(self.json_filename, employee)
    • 외부 라이브러리 jsonlibrary 와의 의존성 때문에 Employee class 를 다른 곳(다른 프로젝트) 에서 재사용하기 어렵다.

    개선 코드

    import jsonlibrary
    
    class Employee:
        def __init__(self, name, salary):
            self.name = name
            self.salary = salary
    
        def raise_salary(self, factor):
            return self.salary * factor
    
    
    class JSONEmployee:
        def __init__(self, name, salary):
            @jsonlibrary.jsonserializable
            self.name = name
    
            @jsonlibrary.jsonserializable
            self.salary = salary
    
    class Storage:
        json_filename = "emp.json"
    
        def save_as_json(self, employee):
            json_employee = JSONEmployee(employee.name, employee.salary)
            jsonlibrary.save(self.json_filename, json_employee)

     

    • JSONEmployee 처럼 Storage 를 위한 중간 클래스를 만들어 사용한다. (class 를 하나 더 만들어야 하는 단점?)
    • Employee 데이터를 복사해 JSONEmployee 를 만들어 사용한다.

     

     

    참고: https://www.udemy.com/course/solid-design-principles-with-python

    'python > SOLID' 카테고리의 다른 글

    Dependency Inversion Principle  (0) 2023.12.06
    Interface Segregation Principle  (0) 2023.12.06
    Liskov Substitution Principle  (0) 2023.12.06
    Open-closed Principle  (1) 2023.12.06

    댓글

Designed by Tistory.