목록Design Pattern (7)
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..

이터레이터 패턴(iterator pattern)이란? 컬렉션 구현 방법을 노출시키지 않으면서도 그 집합체 안에 들어있는 모든 항목에 접근할 수 있는 방법을 제공한다. 이터레이터 패턴을 사용하면 모든 항목에 일일이 접근하는 작업을 컬렉션 객체가 아닌 반복자 객체에서 맡게 된다. 이렇게 하면 집합체의 인터페이스 및 구현이 간단해질 뿐 아니라, 집합체에서는 반복작업에서 손을 떼고 원래 자신이 할 일(객체 컬렉션 관리)에만 전념할 수 있다. Aggregate : Aggregate의 역활 Iterator : 하나씩 나열하면서 검색을 실행하는 인터페이스 Book : 책을 나타내는 클래스 BookShelf : 서가를 나타내는 클래스 BookShelflterator : 서가를 검색하는 클래스 MainApp : 동작 테..

템플릿 메서드 패턴(templatemethod pattern)이란? 상위 클래스 쪽에 템플릿에 해당하는 메소드가 정의가 되어 있고 메소드의 정의안에는 추상 메소드가 사용 된다. 추상 메소드를 실제로 구현 하는것은 하위 클래스 , 즉 상위클래스 에서 뼈대를 구성하고 하위 클래스에서 구체적인 내용을 결정하는 디자인 패턴 AbstractClass(추상 클래스)의 역할 AbstractClass는 템플릿 메소드를 구현하고 추상 메소드를 선언 하위 클래스 ConcreateClass 역활에 의해 구현 ConcreateClass(구현 클래스)의 역할 AbstractClass 역할에서 정의되어 있는 추상 메소드를 구현 여기에서 구현한 메소드는 AbstractClass역의 템플릿 메소드에서 호출 결과 - 적용해서 얻는 결..