分享
 
 
 

Pattern Tips 之一

王朝vc·作者佚名  2006-01-17
窄屏简体版  字體: |||超大  

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模式是非常典型的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模式的示意图:

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有