拜訪者模式 Visitor Pattern - BasuyaMuknana/java-design-pattern GitHub Wiki
拜訪者模式 Visitor Pattern
示意圖
wiki_images/visitor_pattern_uml_diagram.jpg
謫要
- 當有一個合成物件(Computer),裡面的組成物件(Mouse, Keyboard等)對不同的使用者會有不同的反應,就是使用拜訪者模式來抽換被拜訪者的行為。
- 拜訪者介面(ComputerPartVisitor)定義對所有組成物件的拜訪方法,實體拜訪者(ComputerPartDisplayVisitor)需實做這些方法。
- 組成物件介面(ComputerPart)定義accept方法,實體組成物件的實作裡面會呼叫ComputerPartVisitor.visit(this)。(接受某個visitor拜訪自己)
- Computer與其他ComputerPart使用了合成模式