1 Design Patterns: Set 3 Design Patterns: Set 3
- The V
The VISITOR
ISITOR family
family
- V
VISITOR
ISITOR
- A
ACYCLIC
CYCLIC V
VISITOR
ISITOR
- D
DECORATOR
ECORATOR
- E
EXTENSION
XTENSION O
OBJECT
BJECT
- S
STATE
TATE
V VISITOR
ISITOR
- The V
The VISITOR
ISITOR family allows new
family allows new methods to be added to existing methods to be added to existing hierarchies without modifying the hierarchies without modifying the hierarchies hierarchies
- Every derivative of the visited
Every derivative of the visited hierarchy has a method in V hierarchy has a method in VISITOR
ISITOR
- Dual dispatch: two polymorphic
Dual dispatch: two polymorphic dispatches dispatches
Example: Modem Hierarchy Example: Modem Hierarchy
« «interface interface» » Modem Modem + dial() + dial() + send() + send() + hangup() + hangup() + receive() + receive() Hayes Hayes Zoom Zoom Ernie Ernie
Example: Modem Hierarchy (cont’d) Example: Modem Hierarchy (cont’d)
« «interface interface» » Modem Modem + dial() + dial() + send() + send() + hangup() + hangup() + receive() + receive() + accept( + accept( ModemVisitor ModemVisitor) ) Hayes Hayes Zoom Zoom Ernie Ernie « «interface interface» » Modem Visitor Modem Visitor + visit( + visit( Hayes Hayes) ) + visit( + visit( Zoom Zoom ) ) + visit( + visit( Ernie Ernie) )
UnixModem UnixModem Configurator Configurator W indow sModem W indow sModem Configurator Configurator
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
- dem
M
- dem {
{ publ i c voi d publ i c voi d di al ( di al ( St r i ng St r i ng pno) ; pno) ; publ i c voi d publ i c voi d hangup( ) ; hangup( ) ; publ i c voi d publ i c voi d send( send( char char c) ; c) ; publ i c char publ i c char r ecei ve( ) ; r ecei ve( ) ; publ i c voi d publ i c voi d accept ( accept ( M
- dem
Vi si t or M
- dem
Vi si t or v) ; v) ; } } publ i c i nt er f ace publ i c i nt er f ace M
- dem
Vi si t or M
- dem
Vi si t or { { publ i c voi d publ i c voi d vi si t ( vi si t ( HayesM
- dem
HayesM
- dem m
- dem
) ; m
- dem
) ; publ i c voi d publ i c voi d vi si t ( vi si t ( Zoom M
- dem
Zoom M
- dem m
- dem
) ; m
- dem
) ; publ i c voi d publ i c voi d vi si t ( vi si t ( Er ni eM
- dem
Er ni eM
- dem m
- dem
) ; m
- dem
) ; } }
Example: Modem Hierarchy (cont’d) Example: Modem Hierarchy (cont’d)
publ i c cl ass publ i c cl ass HayesM
- dem
HayesM
- dem
i m pl em ent s i m pl em ent s M
- dem
M
- dem {
{ publ i c voi d publ i c voi d accept ( accept ( M
- dem
Vi si t or M
- dem
Vi si t or v) { v) {
- v. vi si t (
- v. vi si t ( t hi s
t hi s) ; ) ; } } / * r est of t he i m pl em ent at i on om i t t ed * / / * r est of t he i m pl em ent at i on om i t t ed * / } } publ i c cl ass publ i c cl ass Uni xM
- dem
Conf i gur at or Uni xM
- dem
Conf i gur at or i m pl em ent s i m pl em ent s M
- dem
Vi si t or M
- dem
Vi si t or { { publ i c voi d publ i c voi d vi si t ( vi si t ( HayesM
- dem
HayesM
- dem m
) { m ) { m . set Conf i gur at i onSt r i ng( m . set Conf i gur at i onSt r i ng( " " &s1=4&D=3 &s1=4&D=3" " ) ) ; ; } } publ i c voi d publ i c voi d vi si t ( vi si t ( Zoom M
- dem
Zoom M
- dem m
) { m ) { m . set Conf i gur at i onVal ue( 42) ; m . 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 ( Er ni eM
- dem
Er ni eM
- dem m
) { m ) { m . set I nt er nal Pat t er n( m . set I nt er nal Pat t er n( " " C i s t oo sl ow C i s t oo sl ow" " ) ) ; ; } } } }