"파이썬 클래스 설계방법"의 두 판 사이의 차이
DB CAFE
(→SRP(Single responsibility principle) 단일 책임 원칙) |
|||
50번째 줄: | 50번째 줄: | ||
def get_courses(self, student_name): | def get_courses(self, student_name): | ||
pass | pass | ||
+ | </source> | ||
+ | |||
+ | === OCP(Open Closed Principle) 개방-폐쇄 원칙 === | ||
+ | |||
+ | # 확장에는 열려있어야 하고, 변경에는 닫혀있어야 함 | ||
+ | #: 예: 캐릭터 클래스를 만들 때, 캐릭터마다 행동이 다르다면, 행동 구현은 캐릭터 클래스의 자식 클래스에서 재정의(Method Override)한다. | ||
+ | #:이 경우, 캐릭터 클래스는 수정할 필요 없고(변경에 닫혀 있음) | ||
+ | #:자식 클래스에서 재정의하면 됨(확장에 대해 개방됨) | ||
+ | #: | ||
+ | ## 나쁜 예 | ||
+ | <source lang=python> | ||
+ | class Rectangle(object): | ||
+ | def __init__(self, width, height): | ||
+ | self.width = width | ||
+ | self.height = height | ||
+ | |||
+ | class Circle: | ||
+ | def __init__(self, radius): | ||
+ | self.radius = radius | ||
+ | |||
+ | class AreaCalculator(object): | ||
+ | def __init__(self, shapes): | ||
+ | self.shapes = shapes | ||
+ | |||
+ | def total_area(self): | ||
+ | total = 0 | ||
+ | for shape in self.shapes: | ||
+ | total += shape.width * shape.height | ||
+ | return total | ||
+ | |||
+ | shapes = [Rectangle(2, 3), Rectangle(1, 6)] | ||
+ | calculator = AreaCalculator(shapes) | ||
+ | print("The total area is: ", calculator.total_area()) | ||
+ | </source> | ||
+ | |||
+ | ## 좋은 예 | ||
+ | <source lang=python> | ||
+ | class Rectangle: | ||
+ | def __init__(self, width, height): | ||
+ | self.width = width | ||
+ | self.height = height | ||
+ | |||
+ | def area(self): | ||
+ | return self.width * self.height | ||
+ | |||
+ | class Circle: | ||
+ | def __init__(self, radius): | ||
+ | self.radius = radius | ||
+ | |||
+ | def area(self): | ||
+ | return 3.14 * self.radius ** 2 | ||
+ | |||
+ | |||
+ | '''다른 도형에 대해 확장하기 위해서, | ||
+ | AreaCalculator는 수정이 필요 없음 (변경에 닫혀 있음) | ||
+ | 단지, Shape을 상속받은 다른 class를 정의하기만 하면 됨 (확장에 대해 개방됨) | ||
+ | ''' | ||
+ | class AreaCalculator(object): | ||
+ | def __init__(self, shapes): | ||
+ | self.shapes = shapes | ||
+ | |||
+ | def total_area(self): | ||
+ | total = 0 | ||
+ | for shape in self.shapes: | ||
+ | total += shape.area() | ||
+ | return total | ||
+ | |||
+ | |||
+ | shapes = [Rectangle(1, 6), Rectangle(2, 3), Circle(5), Circle(7)] | ||
+ | calculator = AreaCalculator(shapes) | ||
+ | |||
+ | print("The total area is: ", calculator.total_area()) | ||
</source> | </source> |
2020년 9월 23일 (수) 09:57 판
thumb_up 추천메뉴 바로가기
- DBA { Oracle DBA 명령어 > DBA 초급 과정 > DBA 고급 과정 }
- 튜닝 { 오라클 튜닝 목록 }
- 모델링 { 데이터 모델링 가이드 }
목차
1 클래스 설계 방법론[편집]
1.1 5가지 클래스 설계의 원칙 (S.O.L.I.D)[편집]
- S - SRP(Single responsibility principle) 단일 책임 원칙
- O - OCP(Open Closed Principle) 개방 - 폐쇄 원칙
- L - LSP(Liskov Substitusion Principle) 리스코프 치환 법칙
- I - ISP(Interface Segregation Principle) 인터페이스 분리 원칙
- D - DIP(Dependency Inversion Principle) 의존성 역전 법칙
출처) https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design
1.2 상세 페이지 참고[편집]
1.3 SRP(Single responsibility principle) 단일 책임 원칙[편집]
- 클래스는 단 한개의 책임을 가져야 함 (클래스를 수정할 이유가 오직 하나)
- 예: 계산기 기능 구현시, 계산을 하는 책임과 GUI를 나타낸다는 책임을 서로 분리하여, 각각 클래스로 설계
- 실제 애매한 부분이 많이 존재함, 가급적 설계시 고려하면 좋음.
- 나쁜 예
- 학생성적과 수강하는 코스를 한개의 class에서 다루는 예
- 한 클래스에서 두개의 책임을 갖기 때문에, 수정이 용이하지 않다.
- 나쁜 예
class StudentScoreAndCourseManager(object):
def __init__(self):
scores = {}
courses = {}
def get_score(self, student_name, course):
pass
def get_courses(self, student_name):
pass
- 변경 예
- 각각의 책임을 한개로 줄여서, 각각 수정이 다른 것에 영향을 미치지 않도록 함
- 변경 예
class ScoreManager(object):
def __init__(self):
scores = {}
def get_score(self, student_name, course):
pass
class CourseManager(object):
def __init__(self):
courses = {}
def get_courses(self, student_name):
pass
1.4 OCP(Open Closed Principle) 개방-폐쇄 원칙[편집]
- 확장에는 열려있어야 하고, 변경에는 닫혀있어야 함
- 예: 캐릭터 클래스를 만들 때, 캐릭터마다 행동이 다르다면, 행동 구현은 캐릭터 클래스의 자식 클래스에서 재정의(Method Override)한다.
- 이 경우, 캐릭터 클래스는 수정할 필요 없고(변경에 닫혀 있음)
- 자식 클래스에서 재정의하면 됨(확장에 대해 개방됨)
- 나쁜 예
class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
class Circle:
def __init__(self, radius):
self.radius = radius
class AreaCalculator(object):
def __init__(self, shapes):
self.shapes = shapes
def total_area(self):
total = 0
for shape in self.shapes:
total += shape.width * shape.height
return total
shapes = [Rectangle(2, 3), Rectangle(1, 6)]
calculator = AreaCalculator(shapes)
print("The total area is: ", calculator.total_area())
- 좋은 예
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
'''다른 도형에 대해 확장하기 위해서,
AreaCalculator는 수정이 필요 없음 (변경에 닫혀 있음)
단지, Shape을 상속받은 다른 class를 정의하기만 하면 됨 (확장에 대해 개방됨)
'''
class AreaCalculator(object):
def __init__(self, shapes):
self.shapes = shapes
def total_area(self):
total = 0
for shape in self.shapes:
total += shape.area()
return total
shapes = [Rectangle(1, 6), Rectangle(2, 3), Circle(5), Circle(7)]
calculator = AreaCalculator(shapes)
print("The total area is: ", calculator.total_area())