1 / 11

Factory Design Pattern

Factory Design Pattern . 패턴 그리고 객체지향적 코딩의 법칙 , 문우식 저. 이 장 호 시스템솔루션팀 2010.03.16. Contents. 문제 나초보 코드 Factory pattern 이란 Implementation 더 생각해보기. 문제. Office 프로그램에서 사용할 사각형의 도장 (stamp) 기능 구현 CSquareStamp 클래스 새로운 요구사항 도착 삼각형 , 원 모양의 도장이 필요함 . 사용자의 요구에 의해 추가될 수 있음 . 나초보 code.

dolan
Télécharger la présentation

Factory Design Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Factory Design Pattern 패턴 그리고 객체지향적 코딩의 법칙, 문우식 저 이 장 호 시스템솔루션팀 2010.03.16

  2. Contents • 문제 • 나초보 코드 • Factory pattern이란 • Implementation • 더 생각해보기 시스템솔루션팀

  3. 문제 • Office 프로그램에서 사용할 사각형의 도장(stamp) 기능 구현 • CSquareStamp클래스 • 새로운 요구사항 도착 • 삼각형, 원 모양의 도장이 필요함. • 사용자의 요구에 의해 추가될 수 있음. 시스템솔루션팀

  4. 나초보code • 살짝 짜증 typedefenum { eSTAMP_TRIANGLE, eSTAMP_SQUARE, eSTAMP_CIRCLE } ESTAMPTYPE; switch(eStampType) { case eSTAMP_TRIANGLE: … = new CTriangleStamp; break; case eSTAMP_SQUARE: … = new CSquareStamp; break; case eSTAMP_CIRCLE: … = new CCircleStamp; break; } 시스템솔루션팀

  5. 나초보code • Code review • 상속을 통해서 공통점과 차이점을 결합한다. • ‘조금만 알기’ 규칙에 부적합 Client Code Client Code Client Code 종류에 따른 도장 생성 종류에 따른 도장 생성 종류에 따른 도장 생성 CTriangleStamp CSquareStamp CCircleStamp 시스템솔루션팀

  6. Factory pattern이란 Creational pattern singleton Factory method Object pool Prototype Abstract factory Builder Factory 시스템솔루션팀

  7. Factory pattern이란 • Motivation • Maybe the most used pattern in modern languages like Java. • Extensions of Factory method pattern, abstract factory pattern • Not included in the GoF. • Intent • creates objects without exposing the instantiation logic to the client. • refers to the newly created object through a common interface 시스템솔루션팀

  8. Implementation 시스템솔루션팀

  9. Implementation • Factory 클래스의 Create 함수는 보통 static으로 선언한다. • 참조하는 선의 수가 9개에서 6개로 줄었다. Client Code Client Code Client Code 종류에 따른 도장 생성 종류에 따른 도장 생성 종류에 따른 도장 생성 CFactory CTriangleStamp CSquareStamp CCircleStamp 시스템솔루션팀

  10. Implementation • 주의할 점  클래스 상속 트리가 책임이 명확하고 노출된 함수의 형태가 객체지향 언어가 지원하는 다형성에 의해 잘 동작할 수 있을 때 factory pattern은 진정한 가치를 할 수 있다. 시스템솔루션팀

  11. 더 생각해보기 • new/delete 쌍 • 같은 depth에 위치해야 한다. • Factory pattern은 new/delete의 depth가 다른가? • Factory class는 new 연산자와 같은 개념. • 추가적으로 필요한 인터페이스가 생긴다면? • Downcasting은 매우 위험!!! • 공통 부분 클래스로 옮긴다. • 공통 부분 클래스가 난잡해진다면, factory pattern이 적합하지 않는다는 의미. • 인터페이스의 name 결정은 중요하다. 포괄적인 개념의 이름 사용. 시스템솔루션팀

More Related