分享
 
 
 

Pattern Tips 之四

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

Pattern Tips 之四

作者:温昱

感谢:《设计模式》一书的作者Gamma,Helm,Johnson和Vilssides,译者李英军等

----------------------------------说明----------------------------

Composite,Decorator。它们都是Structural Patterns,它们的关系如下图所示:

----------------------------------Composite----------------------------

●Tip 1:关键字。Uniform,Tree,Recursive。

●Tip 2:图。

可以看到,图中除了多个“泛化关系”之外,就只剩下一个“聚合关系”了(不是“组成关系”),而恰恰是这个“聚合关系”是至关重要的,它就是著名的“反向包含”(Reverse

Contain)。

●Tip 3:实现与使用。

对“Architect或Achitecture工程师”,应注意Composite Class有一个member var存放Children,而且这个member

var是个Container而不是个Manager,即单个Child的实例化工作Composite是不管的。

class CompositeEquipment : public Equipment {

public:

virtual ~CompositeEquipment();

virtual Watt Power();

virtual Currency NetPrice();

virtual Currency DiscountPrice();

virtual void Add(Equipment*);

virtual void Remove(Equipment*);

virtual Iterator* CreateIterator();

protected:

CompositeEquipment(const char*);

private:

List <Equipment * _equipment; /////////// note here, a list of children

};

所有Component的实例化工作将由“Application工程师”负责。

在MFC中,有OS维护的window tree就是最典型的例子。

●Tip 4:优点。Makes the client simple。Clients can treat composite structures

and individual objects uniformly,这就是抽象的力量,一个抽象节点Component带来的简单好处。当然,这当中,也不能忘了Recursive算法的贡献,Recursive算法一向以代码简短、逻辑清楚著称。

●Tip 5:支持变化。

Makes it easier to add new kinds of components. Newly defined Composite

or Leaf subclasses work automatically with existing structures and client

code. Clients don''t have to be changed for new Component classes.

图中的黄色Class是后来扩充的,你可以扩充New Leaf,也可以扩充New Composite。

●Tip 6:局限性。Maximizing the Component interface。

One of the goals of the Composite pattern is to make clients unaware

of the specific Leaf or Composite classes they''re using. To attain this

goal, the Component class should define as many common operations for

Composite and Leaf classes as possible. The Component class usually provides

default implementations for these operations, and Leaf and Composite subclasses

will override them.

Defining the child management interface at the root of the class hierarchy

gives you transparency, because you can treat all components uniformly.

It costs you safety, however, because clients may try to do meaningless

things like add and remove objects from leaves.

----------------------------------Decorator----------------------------

●Tip 1:关键字。forward,before forwarding,after forwarding。

●Tip 2:图。

●Tip 3:实现和使用。是一个退化的Composite,这一点充份表现在member var由一个Container退化成一个Node Pointer。

class Decorator : public VisualComponent {

public:

Decorator(VisualComponent*);

virtual void Draw();

virtual void Resize();

// ...

private:

VisualComponent* _component; /////// note here, a point to leaf or decorator

};

在ET++中,CStream。。。

●Tip 4:优点。

More flexibility than static inheritance.Furthermore, providing different

Decorator classes for a specific Component class lets you mix and match

responsibilities. Decorators also make it easy to add a property twice.

For example, to give a TextView a double border, simply attach two BorderDecorators.

Inheriting from a Border class twice is error-prone at best.

●Tip 5:局限性。When the Component class is intrinsically heavyweight, using

the Decorator pattern is too costly to apply.

----------------------------------Decorator and Composite----------------------------

●Tip 1:Composite : A decorator can be viewed as a degenerate composite

with only one component.

●Tip 2:Decorator is often used with Composite.

When decorators and composites are used together, they will usually have

a common parent class. So decorators will have to support the Component

interface with operations like Add, Remove, and GetChild.

However, a decorator adds additional responsibilities──it isn''t intended

for object aggregation.

----------------------------------Builder and Composite----------------------------

●Tip 1:A Composite is what the builder often builds。

很显然,Builder and Composite谁也不是谁的超集。证明:一方面,Composite提供Component的递归包含,而Builder不提供;另一方面,Builder提供create和assemble双重服务,

而Composite只提供assemble服务。

我个人的理解,使用Builder模式的Composite模式,就是提供了create和assemble双重服务,使得“Application工程师”不必担负“实例化Component”的任务。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有