Design Patterns: Set 3 Design Patterns: Set 3 V ISITOR V ISITOR � The V � � The V � The V ISITOR ISITOR family family The V ISITOR ISITOR family allows new family allows new methods to be added to existing � V � methods to be added to existing V ISITOR ISITOR hierarchies without modifying the hierarchies without modifying the � A � A CYCLIC CYCLIC V V ISITOR ISITOR hierarchies hierarchies � D � D ECORATOR ECORATOR � Every derivative of the visited � � E � Every derivative of the visited E XTENSION XTENSION O O BJECT BJECT hierarchy has a method in V ISITOR hierarchy has a method in V � S � ISITOR S TATE TATE � Dual dispatch: two polymorphic � Dual dispatch: two polymorphic dispatches dispatches Example: Modem Hierarchy Example: Modem Hierarchy Example: Modem Hierarchy (cont’d) Example: Modem Hierarchy (cont’d) «interface « interface» » «interface « interface» » « «interface interface» » Modem Modem Modem Visitor Modem Visitor Modem Modem + visit( Hayes Hayes ) ) + dial() + dial() + visit( + dial() + dial() + send() + visit( Zoom + visit( Zoom ) ) + send() + send() + send() + visit( Ernie + visit( Ernie ) ) + hangup() + hangup() + hangup() + hangup() + receive() + receive() + receive() + receive() + accept( ModemVisitor ModemVisitor ) ) + accept( UnixModem UnixModem Configurator Configurator Hayes Hayes Zoom Zoom Ernie Ernie Ernie Hayes Hayes Zoom Zoom Ernie W indow sModem W indow sModem Configurator Configurator Example: Modem Hierarchy (cont’d) Example: Modem Hierarchy (cont’d) Example: Modem Hierarchy (cont’d) Example: Modem Hierarchy (cont’d) publ i c i nt er f ace publ i c i nt er f ace M M odem odem { { publ i c cl ass HayesM publ i c cl ass HayesM odem odem i m i m pl em pl em ent s M ent s M odem odem { { publ i c voi d accept ( accept ( M M odem Vi si t or v) { publ i c voi d odem Vi si t or v) { publ i c voi d di al ( publ i c voi d di al ( St r i ng St r i ng pno) ; pno) ; v. vi si t ( t hi s t hi s) ; ) ; v. vi si t ( publ i c voi d hangup( ) ; hangup( ) ; publ i c voi d } } publ i c voi d send( publ i c voi d send( char char c) ; c) ; / * r est of t he i m / * r est of t he i m pl em pl em ent at i on om ent at i on om i t t ed * / i t t ed * / publ i c char publ i c char r ecei ve( ) ; r ecei ve( ) ; } } publ i c voi d accept ( publ i c voi d accept ( M M odem odem Vi si t or Vi si t or v) ; v) ; publ i c cl ass Uni xM publ i c cl ass Uni xM odem odem Conf i gur at or Conf i gur at or i m i m pl em pl em ent s M ent s M odem odem Vi si t or Vi si t or { { } } publ i c voi d publ i c voi d vi si t ( vi si t ( HayesM HayesM odem odem m m ) { ) { m m . set Conf i gur at i onSt r i ng( . set Conf i gur at i onSt r i ng( " " &s1=4&D=3 &s1=4&D=3" " ) ) ; ; publ i c i nt er f ace M publ i c i nt er f ace M odem odem Vi si t or Vi si t or { { } } publ i c voi d publ i c voi d vi si t ( vi si t ( Zoom Zoom M M odem odem m m ) { ) { publ i c voi d publ i c voi d vi si t ( vi si t ( HayesM HayesM odem m odem m odem odem ) ; ) ; m m . set Conf i gur at i onVal ue( 42) ; . set Conf i gur at i onVal ue( 42) ; publ i c voi d publ i c voi d vi si t ( vi si t ( Zoom Zoom M M odem m odem m odem odem ) ; ) ; } } publ i c voi d publ i c voi d vi si t ( vi si t ( Er ni eM Er ni eM odem odem m m odem odem ) ; ) ; publ i c voi d publ i c voi d vi si t ( vi si t ( Er ni eM Er ni eM odem odem m m ) { ) { } } m m . set I nt er nal Pat t er n( . set I nt er nal Pat t er n( " " C i s t oo sl ow C i s t oo sl ow" " ) ) ; ; } } } } 1
Example: Modem Hierarchy (cont’d) V ISITOR V ISITOR as a Matrix as a Matrix Example: Modem Hierarchy (cont’d) � To configure a modem for Unix, � To configure a modem for Unix, Unix Unix Windows Windows create an instance of the visitor and create an instance of the visitor and pass it to accept pass it to accept Initialization of Initialization of Initialization of Initialization of Hayes Hayes Hayes in Unix Hayes in Windows Hayes in Unix Hayes in Windows � The appropriate derivative calls � The appropriate derivative calls vi si t ( t hi s vi si t ( t hi s) ) Initialization of Initialization of Zoom Zoom Initialization of Initialization of Zoom in Unix Zoom in Unix Zoom in Windows Zoom in Windows � New OS configuration can be added � New OS configuration can be added by adding a new derivative of the by adding a new derivative of the Initialization of Initialization of Initialization of Initialization of Ernie Ernie visitor visitor Ernie in Unix Ernie in Windows Ernie in Unix Ernie in Windows Observations A CYCLIC CYCLIC V V ISITOR Observations A ISITOR � In V � In V ISITOR � For a volatile hierarchy ISITOR � For a volatile hierarchy � the visited hierarchy depends on the base class of the � the visited hierarchy depends on the base class of the � � new derivatives are created new derivatives are created visitor hierarchy visitor hierarchy � the base class of the visitor hierarchy has a function for � the base class of the visitor hierarchy has a function for � quick compilation time is needed � quick compilation time is needed each derivative of the visited hierarchy each derivative of the visited hierarchy � A cycle of dependencies ties all the visited � � A A cycle of dependencies ties all the visited � A CYCLIC CYCLIC V V ISITOR ISITOR breaks the breaks the derivatives together derivatives together dependency cycle by making the dependency cycle by making the � difficult to compile incrementally � difficult to compile incrementally � difficult to add new derivatives of visited hierarchy � visitor base class degenerate (i.e. it difficult to add new derivatives of visited hierarchy visitor base class degenerate (i.e. it � Visitor work well if the hierarchy is not modified � Visitor work well if the hierarchy is not modified has no methods) has no methods) often often Example: Modem Hierarchy Example: Modem Hierarchy Example: Modem Hierarcy (cont’d) Example: Modem Hierarcy (cont’d) «interface interface» » «degenerate degenerate» » « « publ i c i nt er f ace M M odem { { publ i c i nt er f ace odem Modem Modem Modem Visitor Modem Visitor publ i c voi d di al ( publ i c voi d di al ( St r i ng St r i ng pno) ; pno) ; + dial() + dial() publ i c voi d hangup( ) ; publ i c voi d hangup( ) ; + send() + send() + hangup() + hangup() publ i c voi d send( publ i c voi d send( char char c) ; c) ; + receive() + receive() + accept( ModemVisitor + accept( ModemVisitor ) ) publ i c char r ecei ve( ) ; publ i c char r ecei ve( ) ; publ i c voi d accept ( publ i c voi d accept ( M M odem odem Vi si t or Vi si t or v) ; v) ; } } Hayes Hayes Zoom Zoom Ernie Ernie publ i c i nt er f ace M M odem Vi si t or { publ i c i nt er f ace odem Vi si t or { «interface» «interface» «interface» «interface» «interface» «interface» HayesVisitor HayesVisitor Zoom Visitor Zoom Visitor ErnieVisitor ErnieVisitor } } + visit + visit (Hayes) (Hayes) + visit (Zoom) + visit (Zoom) + visit + visit (Ernie) (Ernie) UnixModem UnixModem Configurator Configurator 2
Recommend
More recommend