SlideShare uma empresa Scribd logo
1 de 22
Holub on PatternsChapter 3.1 – Game of Life 아꿈사http://andstudy.com 안명환http://eritaka.net
John Conway’s Game of Life 오직 2 가지 법칙 죽어 있는 셀은 3개의 이웃이 있으면 살아난다 두 개 혹은 세 개의 이웃만을 가져야만 산다
특징 – Game of Life Emergence 하위 수준에는 없는 특성이 상위 수준에서 자발적으로 출현하는 현상 미시 동기의 결합으로 인한 거시 행동 형성 다양한 패턴 발생
Game of Life
Static Model
Design Pattern Singleton Visitor Observer Composite Facade Mediator Prototype Memento Flyweight
class Java Menu SubSyst... JComponent «Property> -  name -  text +  setName(String) : void +  getName() : String +  setText(String) : void +  getText() : String AbstractButton JMenuBar «interface> notifies ActionListener +  addActionListener(ActionListener) : void +  add(JMenu) : void «call? +  actionPerformed(ActionEvent) : void +  removeActionListener(ActionListener) : void #  fireActionPerformed(ActionEvent) : void Observer 1 Subject Observer JMenuItem Component & Leaf +  JMenuItem(String) Concrete Observer Concrete Subject * Composite {submenus} <<anonymous>> 1 Composite +  actionPerformed(ActionEvent) : void Concrete Subject JMenu +  add(JMenuItem) : void * {menus} Case Study: Menu (Java Swing)
Observer 문제 구현 상속 기반의 해결안 abstract class BadJMenuItem { abstract void itemSelected(); } classMyMenuItemextendsBadJMenuItem { public void itemSelected() { //아이템이 선택되었을 때 수행할 것... 	} } ,[object Object]
모든 메뉴 아이템이 BadJMenuItem상속
BadJMenuItem을 상속한 개체에만 통지,[object Object]
Observer: 멀티스레드 환경 1 해결책 1의 문제 class Publisher1 { ArrayList subscribers = new ArrayList(); // fireEvent를 기다리다가 기아 현상 발생 public synchronized void subscribe(Runnable subscriber) { subscribers.add(subscriber); } public synchronized void cancelSubscribe(Runnable subscriber) { subscribers.remove(subscriber); } private synchronized void fireEvent() { for(inti=0; i < subscribers.size(); ++i) 		((Runnable)subscribers.get(i)).run(); // 시간이 걸린다! }}
Observer: 멀티스레드 환경 2 해결책 2의 문제 class Publisher2 { private Collection subscribers = newLinkedList(); // Iterator연산 중 add, remove 호출 시 예외 발생 public synchronized void subscribe(Runnable subscriber) { subscribers.add(subscriber); } public synchronized void cancelSubscribe(Runnable subscriber) { subscribers.remove(subscriber); } private void fireEvent() { for( Iteratori=subscribers.iterator(); i.hasNext(); ) 		((Runnable)i.next()).run();  }}
Observer: 멀티스레드 환경 3 해결책 3의 문제 class Publisher3 { private Collection subscribers = newLinkedList(); public synchronized void subscribe(Runnable subscriber) { subscribers.add(subscriber); } public synchronized void cancelSubscribe(Runnable subscriber) { subscribers.remove(subscriber); } // 통지 이벤트 시마다 복사본이 생성 (구독취소 이벤트와 상관없이) private void fireEvent() { Collection localCopy; synchronized( this ) { localCopy = subscribers.clone(); } for( Iteratori=localCopy.iterator(); i.hasNext();) 	((Runnable)i.next()).run(); }}
Observer: 멀티스레드 환경 4 불변 객체 노드를 사용하는 리스트 해결법 이전: 헤드 d:Object c:Object b:Object a:Object 이후: 가비지 컬렉션 될 것임 이전 헤드 d:Object c:Object b:Object a:Object 헤드
class GoF_Visitor Visitor +  VisitConcreteElementA(ConcreteElementA) +  VisitConcreteElementB(ConcreteElementB) ConcreteVisitor1 ConcreteVisitor2 +  VisitConcreteElementA(ConcreteElementA) +  VisitConcreteElementA(ConcreteElementA) +  VisitConcreteElementB(ConcreteElementB) +  VisitConcreteElementB(ConcreteElementB) Element ObjectStructure 1..* +  Accept(Visitor) ConcreteElementA ConcreteElementB +  Accept(Visitor) +  Accept(Visitor) v->VisitConcreteElementA(this) v->VisitConcreteElementB(this) GoF– Visitor Pattern 의도 기존 계층 구조를 수정하지 않고 새로운 메소드를 추가하고자 할 때 전문가 도입
sd LifeGameVisitor Clock publisher subscribers[i] : :Distributor subscribers[i].subscriber Node tick() publish(:Distributor) *accept(:Distributor) deliverTo(subscribers[i].subscriber) tick() "visit"  메소드 sd GoF_Visitor aObjectStructure aConcreteElement[i] aConcreteVisitor *accept(Distributor) visit(aConcreteElement[i]) operation() Holub Visitor vsGoF Visitor 1
class Visitor_Car «interface? CarElementVisitor +  visit(Wheel) : void +  visit(Engine) : void +  visit(Body) : void «interface? CarElement +  accept(CarElementVisitor) : void CarElementPrintVisitor Engine Wheel Body class Visitor_Holub +  accept(CarElementVisitor) : void +  accept(CarElementVisitor) : void +  accept(CarElementVisitor) : void v.visit(this) v.visit(this) v.visit(this) Publisher «interface? Distributor +  accept(Distributor) : void forall node in list +  deliverTo(Object) : void    node.accept(:Distributor); <<anonymous>> Node +  deliverTo(Object) : void -  subscriber:  Object ((Observer)subscriber).notify(); +  accept(Distributor) : void distributor.deliverTo(subscriber); Holub Visitor vsGoF Visitor 2
Publisher vsAWTEventMulticaster AWTEventMulticaster AWT의 모든 리스너 인터페이스 구현 이벤트 타입이 추가될 때마다 해당 인터페이스에 대한 과도한 코드 수정 Publisher 임의의 이벤트를 임의의 구독 객체에 출판 이벤트 타입 추가 시 코드 증가량이 작음
class GoF_Composite Component 1..* +  Operation() Client +  Add() : Component +  Remove() : Component +  GetChild() : Component Leaf Composite +  Operation() +  Operation() -children forall g in children    g.Operation(); +  Add() : Component +  Remove() : Component +  GetChild() : Component GoF – Composite Pattern 의도 개별 객체와 복합 객체를 동일하게 다루고 싶을 경우
class AWT_ComponentContainer public void doLayout() { Component     for( every Component in contents )         doLayout(); +  doLayout() : void } 0..* 1 Component Container +  doLayout() : void Button Leaf +  add(Component) : Component Composite Checkbox Leaf Composite Composite Window Composite Leaf Choice Frame Composite Dialog Case Study: AWT Component/Container
패턴은 변형되어 실체화된다 Composite 패턴의 의도를 보라 class Directory System Leaf and Component SimpleFile Composite +  open() +  close() Composite +  print() Directory public void print() 0..* { {contents} +  print()     for(int i=0; i < contents.length; ++i) +  add(SimpleFile)         contents[i].print(); 1 +  remove(SimpleFile) } +  contents() : Iterator Case Study: Directory System
VS

Mais conteúdo relacionado

Mais procurados

[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발
[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발
[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발정석 양
 
Blockchain 4th dapp programming
Blockchain 4th dapp programmingBlockchain 4th dapp programming
Blockchain 4th dapp programmingihpark92
 
GCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing System
GCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing SystemGCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing System
GCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing System상현 조
 
Blockchain 3rd smart contract programming
Blockchain 3rd smart contract programmingBlockchain 3rd smart contract programming
Blockchain 3rd smart contract programmingihpark92
 
Surface flingerservice(서피스 출력 요청 jb)
Surface flingerservice(서피스 출력 요청 jb)Surface flingerservice(서피스 출력 요청 jb)
Surface flingerservice(서피스 출력 요청 jb)fefe7270
 
GCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with ExceptionGCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with Exception상현 조
 
GCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group System
GCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group SystemGCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group System
GCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group System상현 조
 
GCGC- CGCII 서버 엔진에 적용된 기술 (3) - Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (3) - ExceptionGCGC- CGCII 서버 엔진에 적용된 기술 (3) - Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (3) - Exception상현 조
 
Blockchain 1st bitcoin_core
Blockchain 1st bitcoin_coreBlockchain 1st bitcoin_core
Blockchain 1st bitcoin_coreihpark92
 
만들면서배우는Cocos2d-x(12-13)
만들면서배우는Cocos2d-x(12-13)만들면서배우는Cocos2d-x(12-13)
만들면서배우는Cocos2d-x(12-13)Seungyup Choi
 
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?Leonardo YongUk Kim
 
Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)fefe7270
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기Jong Wook Kim
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSCirculus
 
Multithread design pattern
Multithread design patternMultithread design pattern
Multithread design pattern종빈 오
 
GCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server Sample
GCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server SampleGCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server Sample
GCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server Sample상현 조
 
Blockchain 2nd ethereum_core
Blockchain 2nd ethereum_coreBlockchain 2nd ethereum_core
Blockchain 2nd ethereum_coreihpark92
 
Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)fefe7270
 
Surface flingerservice(서피스플링거서비스초기화 ics)
Surface flingerservice(서피스플링거서비스초기화 ics)Surface flingerservice(서피스플링거서비스초기화 ics)
Surface flingerservice(서피스플링거서비스초기화 ics)fefe7270
 

Mais procurados (20)

[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발
[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발
[하코사세미나]미리보는 대규모 자바스크립트 어플리케이션 개발
 
Blockchain 4th dapp programming
Blockchain 4th dapp programmingBlockchain 4th dapp programming
Blockchain 4th dapp programming
 
GCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing System
GCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing SystemGCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing System
GCGC- CGCII 서버 엔진에 적용된 기술 (4) - Executing System
 
Blockchain 3rd smart contract programming
Blockchain 3rd smart contract programmingBlockchain 3rd smart contract programming
Blockchain 3rd smart contract programming
 
Surface flingerservice(서피스 출력 요청 jb)
Surface flingerservice(서피스 출력 요청 jb)Surface flingerservice(서피스 출력 요청 jb)
Surface flingerservice(서피스 출력 요청 jb)
 
GCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with ExceptionGCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (5) - Executor with Exception
 
GCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group System
GCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group SystemGCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group System
GCGC- CGCII 서버 엔진에 적용된 기술 (8) - Group System
 
GCGC- CGCII 서버 엔진에 적용된 기술 (3) - Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (3) - ExceptionGCGC- CGCII 서버 엔진에 적용된 기술 (3) - Exception
GCGC- CGCII 서버 엔진에 적용된 기술 (3) - Exception
 
Blockchain 1st bitcoin_core
Blockchain 1st bitcoin_coreBlockchain 1st bitcoin_core
Blockchain 1st bitcoin_core
 
ES6-02
ES6-02ES6-02
ES6-02
 
만들면서배우는Cocos2d-x(12-13)
만들면서배우는Cocos2d-x(12-13)만들면서배우는Cocos2d-x(12-13)
만들면서배우는Cocos2d-x(12-13)
 
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
 
Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JS
 
Multithread design pattern
Multithread design patternMultithread design pattern
Multithread design pattern
 
GCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server Sample
GCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server SampleGCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server Sample
GCGC- CGCII 서버 엔진에 적용된 기술 (6) - CGCII Server Sample
 
Blockchain 2nd ethereum_core
Blockchain 2nd ethereum_coreBlockchain 2nd ethereum_core
Blockchain 2nd ethereum_core
 
Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)
 
Surface flingerservice(서피스플링거서비스초기화 ics)
Surface flingerservice(서피스플링거서비스초기화 ics)Surface flingerservice(서피스플링거서비스초기화 ics)
Surface flingerservice(서피스플링거서비스초기화 ics)
 

Semelhante a HolubOnPatterns/chapter3_1

NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기
NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기
NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기Jaeseung Ha
 
Effective c++(chapter 5,6)
Effective c++(chapter 5,6)Effective c++(chapter 5,6)
Effective c++(chapter 5,6)문익 장
 
[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담
[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담
[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담Sumin Byeon
 
Api design for c++ ch3 pattern
Api design for c++ ch3 patternApi design for c++ ch3 pattern
Api design for c++ ch3 patternjinho park
 
Api design for c++ pattern
Api design for c++ patternApi design for c++ pattern
Api design for c++ patternjinho park
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinDong Chan Shin
 
7가지 동시성 모델 - 데이터 병렬성
7가지 동시성 모델 - 데이터 병렬성7가지 동시성 모델 - 데이터 병렬성
7가지 동시성 모델 - 데이터 병렬성HyeonSeok Choi
 

Semelhante a HolubOnPatterns/chapter3_1 (8)

NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기
NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기
NDC 2017 하재승 NEXON ZERO (넥슨 제로) 점검없이 실시간으로 코드 수정 및 게임 정보 수집하기
 
Effective c++(chapter 5,6)
Effective c++(chapter 5,6)Effective c++(chapter 5,6)
Effective c++(chapter 5,6)
 
[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담
[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담
[야생의 땅: 듀랑고]의 식물 생태계를 담당하는 21세기 정원사의 OpenCL 경험담
 
Api design for c++ ch3 pattern
Api design for c++ ch3 patternApi design for c++ ch3 pattern
Api design for c++ ch3 pattern
 
Api design for c++ pattern
Api design for c++ patternApi design for c++ pattern
Api design for c++ pattern
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshin
 
Nodejs_chapter3
Nodejs_chapter3Nodejs_chapter3
Nodejs_chapter3
 
7가지 동시성 모델 - 데이터 병렬성
7가지 동시성 모델 - 데이터 병렬성7가지 동시성 모델 - 데이터 병렬성
7가지 동시성 모델 - 데이터 병렬성
 

HolubOnPatterns/chapter3_1

  • 1. Holub on PatternsChapter 3.1 – Game of Life 아꿈사http://andstudy.com 안명환http://eritaka.net
  • 2. John Conway’s Game of Life 오직 2 가지 법칙 죽어 있는 셀은 3개의 이웃이 있으면 살아난다 두 개 혹은 세 개의 이웃만을 가져야만 산다
  • 3. 특징 – Game of Life Emergence 하위 수준에는 없는 특성이 상위 수준에서 자발적으로 출현하는 현상 미시 동기의 결합으로 인한 거시 행동 형성 다양한 패턴 발생
  • 6. Design Pattern Singleton Visitor Observer Composite Facade Mediator Prototype Memento Flyweight
  • 7. class Java Menu SubSyst... JComponent «Property> - name - text + setName(String) : void + getName() : String + setText(String) : void + getText() : String AbstractButton JMenuBar «interface> notifies ActionListener + addActionListener(ActionListener) : void + add(JMenu) : void «call? + actionPerformed(ActionEvent) : void + removeActionListener(ActionListener) : void # fireActionPerformed(ActionEvent) : void Observer 1 Subject Observer JMenuItem Component & Leaf + JMenuItem(String) Concrete Observer Concrete Subject * Composite {submenus} <<anonymous>> 1 Composite + actionPerformed(ActionEvent) : void Concrete Subject JMenu + add(JMenuItem) : void * {menus} Case Study: Menu (Java Swing)
  • 8.
  • 9. 모든 메뉴 아이템이 BadJMenuItem상속
  • 10.
  • 11. Observer: 멀티스레드 환경 1 해결책 1의 문제 class Publisher1 { ArrayList subscribers = new ArrayList(); // fireEvent를 기다리다가 기아 현상 발생 public synchronized void subscribe(Runnable subscriber) { subscribers.add(subscriber); } public synchronized void cancelSubscribe(Runnable subscriber) { subscribers.remove(subscriber); } private synchronized void fireEvent() { for(inti=0; i < subscribers.size(); ++i) ((Runnable)subscribers.get(i)).run(); // 시간이 걸린다! }}
  • 12. Observer: 멀티스레드 환경 2 해결책 2의 문제 class Publisher2 { private Collection subscribers = newLinkedList(); // Iterator연산 중 add, remove 호출 시 예외 발생 public synchronized void subscribe(Runnable subscriber) { subscribers.add(subscriber); } public synchronized void cancelSubscribe(Runnable subscriber) { subscribers.remove(subscriber); } private void fireEvent() { for( Iteratori=subscribers.iterator(); i.hasNext(); ) ((Runnable)i.next()).run(); }}
  • 13. Observer: 멀티스레드 환경 3 해결책 3의 문제 class Publisher3 { private Collection subscribers = newLinkedList(); public synchronized void subscribe(Runnable subscriber) { subscribers.add(subscriber); } public synchronized void cancelSubscribe(Runnable subscriber) { subscribers.remove(subscriber); } // 통지 이벤트 시마다 복사본이 생성 (구독취소 이벤트와 상관없이) private void fireEvent() { Collection localCopy; synchronized( this ) { localCopy = subscribers.clone(); } for( Iteratori=localCopy.iterator(); i.hasNext();) ((Runnable)i.next()).run(); }}
  • 14. Observer: 멀티스레드 환경 4 불변 객체 노드를 사용하는 리스트 해결법 이전: 헤드 d:Object c:Object b:Object a:Object 이후: 가비지 컬렉션 될 것임 이전 헤드 d:Object c:Object b:Object a:Object 헤드
  • 15. class GoF_Visitor Visitor + VisitConcreteElementA(ConcreteElementA) + VisitConcreteElementB(ConcreteElementB) ConcreteVisitor1 ConcreteVisitor2 + VisitConcreteElementA(ConcreteElementA) + VisitConcreteElementA(ConcreteElementA) + VisitConcreteElementB(ConcreteElementB) + VisitConcreteElementB(ConcreteElementB) Element ObjectStructure 1..* + Accept(Visitor) ConcreteElementA ConcreteElementB + Accept(Visitor) + Accept(Visitor) v->VisitConcreteElementA(this) v->VisitConcreteElementB(this) GoF– Visitor Pattern 의도 기존 계층 구조를 수정하지 않고 새로운 메소드를 추가하고자 할 때 전문가 도입
  • 16. sd LifeGameVisitor Clock publisher subscribers[i] : :Distributor subscribers[i].subscriber Node tick() publish(:Distributor) *accept(:Distributor) deliverTo(subscribers[i].subscriber) tick() "visit" 메소드 sd GoF_Visitor aObjectStructure aConcreteElement[i] aConcreteVisitor *accept(Distributor) visit(aConcreteElement[i]) operation() Holub Visitor vsGoF Visitor 1
  • 17. class Visitor_Car «interface? CarElementVisitor + visit(Wheel) : void + visit(Engine) : void + visit(Body) : void «interface? CarElement + accept(CarElementVisitor) : void CarElementPrintVisitor Engine Wheel Body class Visitor_Holub + accept(CarElementVisitor) : void + accept(CarElementVisitor) : void + accept(CarElementVisitor) : void v.visit(this) v.visit(this) v.visit(this) Publisher «interface? Distributor + accept(Distributor) : void forall node in list + deliverTo(Object) : void node.accept(:Distributor); <<anonymous>> Node + deliverTo(Object) : void - subscriber: Object ((Observer)subscriber).notify(); + accept(Distributor) : void distributor.deliverTo(subscriber); Holub Visitor vsGoF Visitor 2
  • 18. Publisher vsAWTEventMulticaster AWTEventMulticaster AWT의 모든 리스너 인터페이스 구현 이벤트 타입이 추가될 때마다 해당 인터페이스에 대한 과도한 코드 수정 Publisher 임의의 이벤트를 임의의 구독 객체에 출판 이벤트 타입 추가 시 코드 증가량이 작음
  • 19. class GoF_Composite Component 1..* + Operation() Client + Add() : Component + Remove() : Component + GetChild() : Component Leaf Composite + Operation() + Operation() -children forall g in children g.Operation(); + Add() : Component + Remove() : Component + GetChild() : Component GoF – Composite Pattern 의도 개별 객체와 복합 객체를 동일하게 다루고 싶을 경우
  • 20. class AWT_ComponentContainer public void doLayout() { Component for( every Component in contents ) doLayout(); + doLayout() : void } 0..* 1 Component Container + doLayout() : void Button Leaf + add(Component) : Component Composite Checkbox Leaf Composite Composite Window Composite Leaf Choice Frame Composite Dialog Case Study: AWT Component/Container
  • 21. 패턴은 변형되어 실체화된다 Composite 패턴의 의도를 보라 class Directory System Leaf and Component SimpleFile Composite + open() + close() Composite + print() Directory public void print() 0..* { {contents} + print() for(int i=0; i < contents.length; ++i) + add(SimpleFile) contents[i].print(); 1 + remove(SimpleFile) } + contents() : Iterator Case Study: Directory System
  • 22. VS