Topic: Decorator 与 Composite 模式?

  Print this page

1.Decorator 与 Composite 模式? Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-19 10:51

不知道是不是我理解的错,在实际应用中这两种模式很象.从定义上看,在Decorator中实际就包含Composite

2.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: sanshaoye
Posted on: 2004-11-19 12:16

I don't think so.

The first,consider the purpose of those two pattern

The Composite Design pattern allows a client object to treat both single components and collections of components identically.

If you read the junit code, you can found TestSuite composite a collections of TestCase,they implements the common interface Test,so you can treat TestSuite and TestCase identically.If you run a TestSuite will run all TestCase of that include.

The Decorator Pattern is used for adding additional functionality to a particular object as opposed to a class of objects.

Consider,we will and some function for your class exist already. It is easy to add functionality to an entire class of objects by subclassing an object, but it is impossible to extend a single object this way. With the Decorator Pattern, you can add functionality to a single object and leave others like it unmodified.

3.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: emarket
Posted on: 2004-11-21 14:18

一个具体的例子来 说明区别吧

在composite pattern里面,能独立完成功能的是 component. 当然,有两种componet 一种是leaf component, 一种是 composite componet.

在Decorator pattern里面,能够独立完成功能的是 concreteComponent, decorator不可以单独完成功能。例如JDK 里面 IO 库的 设计, BufferredInputStream 实际上是不可以单独完成 InputStream 的功能的。

也就是说, composite pattern里面 有用的是 component, 而 Decroator pattern 里面 有用的是 concreteComponent。

还有一个区别就是multiplicity, 在 decorator pattern里面, decorator 和 component的对应关系 是 一个 decorator 对应一个 componnet。 而 composite pattern里面 一个composite可以对应多个componnet。

4.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-22 09:17

composite pattern里面 有用的是 component

这句话不明白,有用没用是怎样界定的?

5.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: emarket
Posted on: 2004-11-22 10:27

FileInputStream or ByteInputStream (component) 都是可以完成一定功能的inputstream, 而 BufferedInputStream (decorator)则不可以,必须和其他的inputstream联合使用才可以

6.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: wes109
Posted on: 2004-11-22 10:56

其实Decorator最好理解了

jameszhang穿西服是jameszhang
穿夹克是jameszhang
不穿衣服,仍然是jameszhang Wink (绝对不可能变成nothing,hehe)

至于composite嘛

拉登在我QQ上面,小布什也在我的QQ上面
我可以单独给他们发消息
后来觉得麻烦,建了一个群
现在只要发一个(群组)消息,他们两个都收到了
一个用户也好,一个群也好
对我来说都是一个聊天对象
我跟他们沟通的方式都是一样的
(比喻的不太好,换个角度成observer了 Embaressed)

7.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-22 11:28

最好是一个事物两种方式比较,东一个西一个不理解

衣服也是对象,和JAMES组合在一起 不就是COMPOSITE吗?哈哈

8.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: wes109
Posted on: 2004-11-22 11:46

No!

composite模式组合的对象必须在概念上是一样的东西,譬如TestCase和TestSuite都是Test

当然,如果你说
衣服是东西,james也是。。
放一起,还是。。。。。。偶也没办法 Wink

Decorator存在一个主体,也就是被修饰的对象,不管怎么修饰,被修饰对象的本质不能变

如果james穿上夹克就成了nothing,可就麻烦了

除非...................穿的是马甲,呵呵 Tounge

9.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-22 11:55

本质不变这可不好说呀.量变产生质变,装饰多了,出来的东西可能会有质变的.
如果不走极端,你的意思是 composite 组合后不会反映原来对象特性是个新的对象,而decorator装饰后,对象性质不变,RIGHT?

10.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: wes109
Posted on: 2004-11-22 13:13

看上面

The Composite Design pattern allows a client object to treat both single components and collections of components identically.

是客户端不需要区别对待单个对象还是组合对象

我认为,如果发生质变,就不再是decorator了
或者说是decorator的误用

11.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-11-22 13:26

量变产生质变 - In the composite patterns, the meaning that we treat component and composite the same is when we call them in a particular way, the api is the same, i.e., composite.run() and component.run() have the same run(), we don't need to distinguish them like this:
if (isComposite()) ((Composite)this).run1();
else (isComponent()) ((Component)this).run2();
Another example is that in web, we can treat an action and a chain of actions to be the same via Composite, we set http req/res to the action and call execute().

So we use composite to maintain the original api signature, while add more functionalities. Decorators are extra addons to the original objects through composition(Note that composite and composition are completely different in this comtext), and thus the api of the originals are not necessarily shown in the decorators.

The logic are completely different in these two cases.

12.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: emarket
Posted on: 2004-11-23 13:46

jameszhang wrote:
最好是一个事物两种方式比较,东一个西一个不理解

衣服也是对象,和JAMES组合在一起 不就是COMPOSITE吗?哈哈


James 是人,可以干活,衣服不可以干活,穿上了衣服的 James(decorator后的component) 比较暖和,干活比较卖劲。

另外从multiplicity的角度理解,一个衣服同时只能被一个James穿:) (decorator只可以修饰一个component)

13.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-02 11:24

通过各位的解答,写出下面代码:

Decorator:


public class Client
{
public Client()
{
}
public static void main(String args[])
{
Worker james = new James();
SoldierDecorator jamesSoldier = new SoldierDecorator(james);
System.out.println(jamesSoldier.getJob());
}

}


interface Worker
{
public String getJob();
}


class James
implements Worker
{
public String getJob()
{
return "打工的";
}
}


class SoldierDecorator implements Worker
{
private Worker man;
public SoldierDecorator(Worker man)
{
this.man = man;
}

public String getJob()
{
return "在部队" + this.man.getJob();
}

}


class DeveloperDecorator implements Worker
{
private Worker man;
public DeveloperDecorator(Worker man)
{
this.man = man;
}
public String getJob()
{
return "在软件公司" +this.man.getJob();
}
}

14.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-02 16:03

Composite :

public class ClientA
{
public ClientA()
{
}
public static void main(String args[])
{
PourTeam cjsdn = new PourTeam();
Nothing nothing = new Nothing();
Wes109 wes109 = new Wes109();
Jameszhang james = new Jameszhang();

cjsdn.addPourMan(wes109);
cjsdn.addPourMan(nothing);
cjsdn.addPourMan(james);

System.out.println(cjsdn.getWisdom());

}

}
interface PourMan
{
public String getWisdom();
}


class Wes109 implements PourMan
{
public String getWisdom()
{
return "一个人走";
}

}

class Nothing implements PourMan
{
public String getWisdom()
{
return " 我等待..";
}

}

class Jameszhang implements PourMan
{
public String getWisdom()
{
return "灌...灌...灌...灌";
}
}

class PourTeam implements PourMan
{
private java.util.ArrayList pourManList = new java.util.ArrayList();
public void addPourMan(PourMan pourman)
{
pourManList.add(pourman);
}
public String getWisdom()
{
String result="";
for(int i=0;i<pourManList.size();i++)
{
result+=((PourMan)pourManList.get(i)).getWisdom();
}
return result;
}
}

15.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-12-02 23:33

Decorator:
Change Human to Worker for a better name, because you have getJob().
SoldierDecorator and DeveloperDecorator should implement Human interface too.
Then we could do a chain decoration to return something like 在 部队 软件公司 打工的
SoldierDecorator jamesSoldier = new SoldierDecorator(new DeveloperDecorator(james));

Change Cjsdn_pour to CjsdnPourTeam, for
1. java naming convention
2. PourMan vs PourTeam

16.Re:Decorator 与 Composite 模式? [Re: floater] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-03 08:01

floater wrote:
Decorator:
Change Human to Worker for a better name, because you have getJob().
SoldierDecorator and DeveloperDecorator should implement Human interface too.
Then we could do a chain decoration to return something like 在 部队 软件公司 打工的
SoldierDecorator jamesSoldier = new SoldierDecorator(new DeveloperDecorator(james));

哈!我就是怕出现这个,呵呵,现实中怎么可能两个都有



Change Cjsdn_pour to CjsdnPourTeam, for
1. java naming convention
2. PourMan vs PourTeam


改这样

17.Re:Decorator 与 Composite 模式? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-12-03 23:08

软件公司's owner is 部队.

I've had several times when I need to chain them together. Otherwise, there would be duplicated code.

18.Re:Decorator 与 Composite 模式? [Re: floater] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-04 10:13

floater wrote:
软件公司's owner is 部队.

I've had several times when I need to chain them together. Otherwise, there would be duplicated code.

oK!, 呵呵


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923