목록Design Pattern/SOLID (5)
PDH 개발 공부
Traditional Pattern class Cat: def speak(self): print("meow") class Dog: def speak(self): print("bark") #Zoo depdns on Cat and Dog class Zoo: def __init__(self): self.dog = Dog() self.cat = Cat() def speakAll(self): self.cat.speak() self.dog.speak() zoo = Zoo() zoo.speakAll() 동물원은 고양이와 개와 의존적인 형태다. 만약에 양과 소가 추가 된다면 더 많은 의존적인 객체들이 생성이 되어진다 즉 코드 관리가 힘들어짐. Dependency Inversion Pattern class Animal:..
//No Interface Segregation Principle //Large Interface Interface ICarBoatInterface { void drive(); void turnLeft(); void turnRight(); void steer(); void steerLeft(); void steerRight(); } //Interface Segregation Principle //two small interfaces (Car, Boat) Interface ICarInterface { void drive(); void turnLeft(); void turnRight(); } Interface IBoatInterface { void steer(); void steerLeft(); void s..
리스코프 치환 법칙 이란 타입을 치환 한다 하더라도 프로그램이 돌아 가도록 함 코드 class Cat: def speak(self): print("meow") class BlackCat(Cat): def speak(self): print("black meow") def speak(cat:Cat): cat.speak() cat = Cat() speak(cat) // 결과 meow --------------------------------------------------------- cat = BlackCat() speak(cat) // 결과 black meow ---------------------------------------------------------- class Fish(Cat): def sp..
Open closed principle을 준수하지 않는 Animal class Animal(): def __init__(self,type): self.type = type def hey(animal): if animal.type == 'Cat': print('meow') elif animal.type == 'Dog': print('bark') bingo = Animal('Dog') kitty = Animal('Cat') #Cow와 Sheep을 추가하기위해 hey함수의 수정이 필요하다. hey(bingo) hey(kitty) // 결과 bark meow 상속을 이용한 Animal class. 추가되는 동물에 대해 hey함수의 수정을 필요로 하지 않는다 class Animal: def speak(self):..
코드 class Cat: def __init__(self,age,name): self.age = age self.name = name def eat(self): print("eating..") def walk(self): print("walking..") def speak(self): print("meow~") def repr(self): // 고양이 상태를 나타내주는 함수 return f"name:{self.name}, age:{self.age}" // 요기서 print , log를 사용하지말자. kitty = Cat(3,"kitty") kitty.eat() kitty.walk() kitty.speak() print(kitty.repr()) #Logger.log(kitty.repr()) , If you..