Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Design Pattern & UML  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Decorator 与 Composite 模式?
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-19 10:51 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不知道是不是我理解的错,在实际应用中这两种模式很象.从定义上看,在Decorator中实际就包含Composite


"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
sanshaoye





发贴: 16
积分: 0
于 2004-11-19 12:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.



Nothing is impossible to a willing heart!
作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
emarket

亢龙有诲

CJSDN高级会员


发贴: 460
积分: 82
于 2004-11-21 14:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
一个具体的例子来 说明区别吧

在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。



个人Blog http://agilespeaking.blogspot.com/

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-22 09:17 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
composite pattern里面 有用的是 component

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



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
emarket

亢龙有诲

CJSDN高级会员


发贴: 460
积分: 82
于 2004-11-22 10:27 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
FileInputStream or ByteInputStream (component) 都是可以完成一定功能的inputstream, 而 BufferedInputStream (decorator)则不可以,必须和其他的inputstream联合使用才可以


个人Blog http://agilespeaking.blogspot.com/

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
wes109

以梦为马

CJSDN高级会员


发贴: 857
积分: 60
于 2004-11-22 10:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
其实Decorator最好理解了

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

至于composite嘛

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




作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-22 11:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
最好是一个事物两种方式比较,东一个西一个不理解

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



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
wes109

以梦为马

CJSDN高级会员


发贴: 857
积分: 60
于 2004-11-22 11:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
No!

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

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

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

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

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




作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-22 11:55 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
本质不变这可不好说呀.量变产生质变,装饰多了,出来的东西可能会有质变的.
如果不走极端,你的意思是 composite 组合后不会反映原来对象特性是个新的对象,而decorator装饰后,对象性质不变,RIGHT?



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
wes109

以梦为马

CJSDN高级会员


发贴: 857
积分: 60
于 2004-11-22 13:13 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
看上面

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

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

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




作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-11-22 13:26 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
量变产生质变 - 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.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
emarket

亢龙有诲

CJSDN高级会员


发贴: 460
积分: 82
于 2004-11-23 13:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jameszhang wrote:
最好是一个事物两种方式比较,东一个西一个不理解

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


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

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



个人Blog http://agilespeaking.blogspot.com/

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-02 11:24 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
通过各位的解答,写出下面代码:

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();
}
}


jameszhang edited on 2004-12-04 10:15

"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-02 16:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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;
}
}


jameszhang edited on 2004-12-03 08:02

"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-12-02 23:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:Decorator 与 Composite 模式? [Re:floater]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-03 08:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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


改这样


jameszhang edited on 2004-12-03 08:05

"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:Decorator 与 Composite 模式? [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-12-03 23:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
软件公司's owner is 部队.

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



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:Decorator 与 Composite 模式? [Re:floater]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-04 10:13 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
floater wrote:
软件公司's owner is 部队.

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

oK!, 呵呵



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

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