Pattern Tips Ö®Ò»
×÷ÕߣºÎÂêÅ
¸Ðл£º¡¶Éè¼ÆÄ£Ê½¡·Ò»ÊéµÄ×÷ÕßGamma£¬Helm£¬JohnsonºÍVilssides£¬ÒëÕßÀîÓ¢¾üµÈ
----------------------------------˵Ã÷----------------------------
Adapter£¬Bridge£¬Facade£¬Proxy¡£ËüÃǶ¼ÊÇStructural Patterns£¬ËüÃǵĹØÏµÈçÏÂͼËùʾ£º

----------------------------------Adapter----------------------------
¡ñTip 1£º¹Ø¼ü×Ö¡£existing£¬reuse¡£
¡ñTip 2£ºÍ¼¡£
¿ÉÒÔ¿´µ½£¬AdapterÊÇÔÚTargetºÍAdapteeÒѾexistingµÄÇé¿öÏ£¬ÁÙΣÊÜÃüµÄ£¬ÊÇ¡°Êº󹤳̡±¡£
¡ñTip 3£ºÊµÏÖºÍʹÓᣵ½µ×ʹÓÃClass Adapter»¹ÊÇObject Adapter£¬ÒªÊÓ²»Í¬Çé¿ö¶ø¶¨¡£
Class AdapterÓŵ㣺Lets Adapter override some of Adaptee''s behavior, since
Adapter is a subclass of Adaptee¡£ÁíÍ⣬²»±ØÒýÈë¶îÍâµÄʵÀý»¯ÎÊÌ⣬Introduces only one object,
and no additional pointer indirection is needed to get to the adaptee¡£
Class Adapterȱµã£ºAdapts Adaptee to Target by committing to a concrete
Adapter class. As a consequence, a class adapter won''t work when we want
to adapt a class and all its subclasses.
Object AdapterÓŵ㣺Lets a single Adapter work with many Adaptees©¤that is,
the Adaptee itself and all of its subclasses (if any). The Adapter can
also add functionality to all Adaptees at once.
Object Adapterȱµã£ºMakes it harder to override Adaptee behavior.
MFC±¾Éí¾ÍÊÇAdapterµÄÀý×Ó£¬Win32 APIÊÇ»ùÓÚfuncµÄ£¬MFCÊÇ»ùÓÚclassµÄ¡£
¡ñTip 4£ºÓŵ㡣¿ÉÓÃÓÚÕûºÏÒÅÁôϵͳ¡£
¡ñTip 5£º¾ÖÏÞÐÔ¡£Èç¹ûÒÅÁôϵͳ»¹ÔÚ·¢Õ¹¡¢±ä»¯ºÍÔö³¤Ö®ÖУ¬ÕûºÏµÄ¿ªÏú»áºÜ´ó¡£
----------------------------------Bridge----------------------------
¡ñTip 1£º¹Ø¼ü×Ö¡£Abstraction and its Implementation¡£
¡ñTip 2£ºÍ¼¡£
¿ÉÒÔ¿´µ½£¬ÎÒ°Ñͼ·Ö³ÉÁË4¸öLayer£ºApplication£¬Abstraction£¬Logic£¬Implementor¡£
Application²ãµÄClientʹÓÃAbstraction²ãµÄ³éÏó¶ÔÏó£¬ÕâЩ³éÏó¶ÔÏóÊǺ;ßÌåÆ½Ì¨Î޹صģ»Abstraction²ãµÄ³éÏó¶ÔÏóÓÖÊÇÓÉImplementor²ãµÄ¶ÔÏóʵÏֵģ¬ºÍ¾ßÌåÆ½Ì¨ÓйصĶÔÏóÔÚImplementor²ã£»ÎªÁ˸ü¼ÓÇåÎúµØËµÃ÷ÎÊÌ⣬ÎÒÔÚAbstraction²ãºÍImplementor²ãÖ®¼äÔö¼ÓÁËLogic²ã£¬¸Ã²ãÊÇImplementorÈçºÎʵÏÖAbstractionµÄ³ÌÐòÂß¼¡£
Æäʵ£¬BridgeģʽÊǷdz£µäÐ͵ÄLayer-likeģʽ¡£ÎªÁ˶Աȷ½±ã£¬ÎÒ·ÅÒ»ÕÅET++µÄLayerͼÔÚϱߡ£

¡ñTip 3£ºÊµÏÖºÍʹÓá£
Ôڴ˼¯ÖÐÌÖÂÛÒ»¸öÎÊÌ⣺RefinedImplementorµÄʵÀý»¯ÎÊÌâ¡£ÒòΪRefinedAbstraction¿Ï¶¨ÊÇÓÉClientʵÀý»¯µÄ£¬µ«RefinedImplementorµÄʵÀý»¯È´¿ÉÒÔ·ÖΪ2ÖÖÇé¿ö£º
µÚ1ÖÖÇé¿ö£¬ÓÉAbstraction»òRefinedAbstractionʵÀý»¯¡£ÕâÒªÇóAbstraction»òRefinedAbstractionÖªµÀËùÓеÄRefinedImplementor£¬¾ßÌåʵÀý»¯ÄÄÒ»¸ö£¬¿ÉÒÔͨ¹ýAbstraction::Abstraction(para)µÄ²ÎÊýÀ´È·¶¨¡£
µÚ2ÖÖÇé¿ö£¬Î¯Íиø±ðµÄ¶ÔÏóÀ´ÊµÀý»¯¡£µäÐ͵ģ¬¿ÉÒÔίÍиøÒ»¸öAbstract FactoryÀ´ÊµÀý»¯¡£ÕâÑù£¬AbstractionÖ»ÐèÒªÖªµÀImplementorÕâ¸öInterface£¬ÕâÊÇÒ»¸öÁ¼ÐÔÒÀÀµ£¬ÔÚͼÖб»ÎÒ»³ÉÁËÂÌÉ«¡£
ET++ÖУ¬ºÍƽ̨Î޹صÄWindowÊÇÓÃºÍÆ½Ì¨Ïà¹ØµÄWindowPortʵÏֵ쬵«ºóÕßµÄʵÀý»¯ÊÇίÍÐ(delegate)WindowSystemÕâ¸öAbstract
FactoryÀ´Íê³ÉµÄ£º
class Window {
...
protected:
WindowImp* GetWindowImp(); ///////////////call Abstract Factory and return Implementor
private:
WindowImp* _imp; ////////////save GetWindowImp() ''s return value
...
};
WindowImp* Window::GetWindowImp () {
if (_imp == 0) {
_imp = WindowSystemFactory::Instance()->MakeWindowImp();////////////////////WindowSystemFactory is a Singleton
}
return _imp;
}
¡ñTip 4£ºÖ§³Ö±ä»¯¡£Putting the Window abstraction and its implementation in separate
class hierarchies¡£You can extend the Abstraction and Implementor hierarchies
independently¡£Í¼ÖеĻÆÉ«Class¾ÍÊǼÙÏëºóÀ´À©³äµÄ¡£
----------------------------------Proxy----------------------------
¡ñTip 1£º¹Ø¼ü×Ö¡£Placeholder£¬Control¡£
¡ñTip 2£ºÍ¼¡£

¿ÉÒÔ¿´µ½£¬ProxyºÍRealthingµÄ¶ÔÍâ½Ó¿ÚÊÇÏàͬµÄ¡£
¡ñTip 3£ºÊµÏÖºÍʹÓá£ÌÖÂÛÁ½¸ö¹Ø¼ü×Ö£º
Placeholder¡£¿ÉÒÔÊÇa direct reference to its real subject£¬±ÈÈçÔÚͬһ̨PCÉÏ(ÇÒÔÚͬһ¸öApplicationÄÚ)£¬DrawProxyÖ®¼äµ÷ÓÃDraw£»Ò²¿ÉÒÔÊÇonly
an indirect reference£¬±ÈÈç¿çÍøÂçµÄÓ¦Ó㬿ÉÄÜÖ»ÖªµÀ¡°host ID and local address on host¡±£¬¹þ¹þ£¬¾ÍÊÇ¡°IPµØÖ·+¶Ë¿ÚºÅ¡±¡£
Control¡£Ö®ËùÒÔProxy£¬¾ÍÊÇΪÁËÄÜControl£¬»òÕß˵ΪÁËÖÇÄÜ£ºÖÇÄܱ£»¤£¬ÖÇÄܾܾø£¬ÖÇÄÜ»ØÊÕ£¬ÖÇÄܽµµÍ¿ªÏú¡£
ÔÚCOMÖУ¬ÓÐÖÇÄÜÖ¸ÕëSmartPointer¡£.NetÖеÄSmartClient˵²»¶¨Ò²ÊÇ¡£¡£¡£
----------------------------------Facade----------------------------
¡ñTip 1£º¹Ø¼ü×Ö¡£Subsystem£¬Higher-level Interface¡£
¡ñTip 2£ºÍ¼¡£

¿ÉÒÔ¿´µ½£¬Facade·â×°Á˶à¸öClass¡£
¡ñTip 3£ºÊµÏÖºÍʹÓá£
ÔÚFacade ClassÉÏ»¹¿ÉÒÔ×öЩÎÄÕ£¬ÒÔ½øÒ»²½½µµÍñîºÏ¶È£¬±ÈÈçFacade±¾Éí¿ÉÒÔÅÉÉúSubclass£¬»òÕßÓÃίÍÐ(delegate)À´ÅäÖÃFacade¡£The
coupling between clients and the subsystem can be reduced even further
by making Facade an abstract class with concrete subclasses for different
implementations of a subsystem. Then clients can communicate with the
subsystem through the interface of the abstract Facade class. This abstract
coupling keeps clients from knowing which implementation of a subsystem
is used. An alternative to subclassing is to configure a Facade object
with different subsystem objects. To customize the facade, simply replace
one or more of its subsystem objects.
ʹÓÃÃû×ֿռ䡣A class encapsulates state and operations, while a subsystem encapsulates
classes. The C++ standardization committee added name spaces to the language
[], which will let you expose just the public subsystem classes.
ÔÚET++ÖУ¬Óиö³ÆÎªbrowsing toolsµÄSubsystem£¬ÆäÖеÄProgrammingEnvironment¾ÍÊÇFacade¡£ET++ÖеÄÏà¹ØÑо¿£¬Çë²Î¿¼±¾Õ¾(lcspace.nease.net)µÄFrameworkÀ¸Ä¿¡£
¡ñTip 4£ºÖ§³Ö±ä»¯¡£Lets you vary the components of the subsystem without affecting
its clients¡£
----------------------------------Proxy and Decorator----------------------------
¡ñTip 1£ºProxyÊÇDecoratorÌØÀý¡£
µ±Ò»¸öDecoratorģʽ£¬Decorator²»ÄܵݹéÐÞÊÎDecorator£¬¶øÇÒÒ²²»Òªbefore forwardingºÍafter
forwardingµÄ²Ù×÷ÁË£¬½ö½ö¾ÍÊÇforward£¬DecoratorģʽҲ¾ÍÍË»¯³ÉProxyģʽÁË¡£
----------------------------------Adapter£¬Bridge£¬Facade and Proxy----------------------------
Adapter£¬Bridge£¬Facade and ProxyÕâ4ÖÖģʽ£¬Æäʵ¶¼¿ÉÒÔ¹éΪ¡°Layer-likeģʽ¡±¡£ÏëÏë¿´£¬ËüÃÇȷʵ¶¼ÊÇ¡°Layer¼äµ¥Ïòµ÷Ó÷þÎñ¡±µÄ¡£
µ±È»£¬´ÓÂß¼ÉÏÀ´½²£¬Õâ4¸öģʽÓÖ·ÖΪ2×飺
Bridge and Facade©¤©¤Éæ¼°Âß¼²ã´Î¡°²»Í¬¡±µÄ2¸öLayer¡£
Adapter and Proxy©¤©¤Éæ¼°Âß¼²ã´Î¡°Ïàͬ¡±µÄ2¸öLayer¡£
ÏÂÃæÊǵäÐ͵ÄLayer-likeģʽµÄʾÒâͼ£º
|