Topic: 为什么要用装饰模式? |
Print this page |
1.为什么要用装饰模式? | Copy to clipboard |
Posted by: struggle Posted on: 2003-10-10 16:11 《java与模式》中“装饰模式”一章我没怎么看明白。其类之间有这样的继承关系有什么好 处呢?如果有哪位大位能举出一个好的实例来(书上的孙悟空例子不太明白),那更是感激不尽! |
2.Re:为什么要用装饰模式? [Re: struggle] | Copy to clipboard |
Posted by: neilyoung Posted on: 2003-10-10 17:24 when customer has the method such as void setConnection(Component* com) (Component :abstract base class or interface in the inheritance class hierarchy), the advantage is explicit . because of the same type, we can replace input paramater in the client code with other decorator object pointer to enhance the code function. for example, if original component is a text view, we can add a scroll bar upon it , forward the original text view the request of handling the text. (how to forward??? because the decorator know its responsibility is to enhance which component, just like scroll bar know text view.) |
3.Re:为什么要用装饰模式? [Re: struggle] | Copy to clipboard |
Posted by: yamakasy Posted on: 2003-10-11 10:19 举个例子: 我现在是某software公司,我对外号称,我什么项目都能做(实现了interface),比如,能够CRM,SCM,MRP,ERP(implements ICRMFactory,ISCMFactory,IMRPFactory,IERPFactory)等等,而实际上我们公司现有员工只能做mrp,erp(却是提供了直接的实现),而对于其他的CRM,SCM等,没有直接实现,而是派遣给另外一个公司,而该公司实现了CRM,SCM的接口(implements ICRMFactory,ISCMFactory),就是一个包装的过程,我只要是有该公司的接口,我就可以包装该公司。另外我还多出了MRP,ERP的功能,特点是你可以向其他CRM公司进行一样的操作来让我们生产CRM产品,一样的接口,在不改变被包装对象的情况下添加新的功能。
|
4.Re:为什么要用装饰模式? [Re: yamakasy] | Copy to clipboard |
Posted by: neilyoung Posted on: 2003-10-13 16:57 yamakasy wrote: 1.此软件公司(假设为neilyoung)知道另外的作crm/scm的公司(假设为nirvana),而做crm/scm的nirvana不用知道neilyoung. 2.向neilyoung的有关crm/scm的请求都被正确delegate 给nirvana. nirvana无权向neilyoung分派什麽,他对neilyoung不了解,也就无从分派。 3.对neilyoung来讲,将请求分派是一种资源的调配,是策略(strategy); 对nirvana来讲,在所有的请求来到之前进行相关处理,做一些enhancement,是包装(decorator). |
5.Re:为什么要用装饰模式? [Re: struggle] | Copy to clipboard |
Posted by: yamakasy Posted on: 2003-10-13 20:35 agree! Java中有相当多的Decorator,比如,Swing,IO等等,可以说了解Decorator模式,能够更好的指导你了解和利用Java的API。 |
6.Re:为什么要用装饰模式? [Re: struggle] | Copy to clipboard |
Posted by: hitaco Posted on: 2003-10-15 16:30 我受不了那本书的不专业,老举这种例子 struggle wrote: |
7.Re:为什么要用装饰模式? [Re: struggle] | Copy to clipboard |
Posted by: floater Posted on: 2003-10-15 23:06 GoF's book explains this well. |
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 |