设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> Java >> decorator模式使用中遭遇继承与聚合的冲突问题
精品推荐
Java点击TOP10
·java笔试题
·《Thinking in Java》读书笔记
·JSP的mysql_jdbc驱动程序
·异常java.sql.SQLException: Io exception:The Network Adapter could not establish connection
·Java Coder 常用软件下载地址
·org.apache.commons.dbcp.SQLNestedException解决办法
·Java图形界面开发:SWT全接触
·如何使用Java POI生成Excel表文件 !
·功能强大的在线网页编辑器
·一些非常不错的Struts 例子下载
编程开发点击TOP10
·ASP.NET 程序中常用的三十三种代码
·利用ASP.NET构建网上考试系统
·C#版的网站新闻发布系统
·(转)23种设计模式汇集
·设计ASP.NET新闻管理系统
·深山红叶袖珍PE工具箱V16正式版
·我的.NET书架 (入门篇)
·java笔试题
·网页打印问题,打印设置,打印预览,打印分页,纵打,横打及页面的边距
·.NET:是什么?将走向哪里?
精选专题

decorator模式使用中遭遇继承与聚合的冲突问题

作者: 来源:网络文章 时间:2005-12-14 17:24:14

decorator模式使用中遭遇继承与聚合的冲突问题(1)

一:背景:Decorator
*Decorator 常被翻译成"装饰",我觉得翻译成"油漆工"更形象点,油漆工(decorator)是用来
刷油漆的,那么被刷油漆的对象我们称decoratee.这两种实体在Decorator 模式中是必须
的.

*Decorator 定义:
动态给一个对象添加一些额外的职责,就象在墙上刷油漆.使用Decorator 模式相比用生成
子类方式达到功能的扩充显得更为灵活.

*为什么使用Decorator?
我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势
必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓
展功能,这些功能是编译时就确定了,是静态的.
使用Decorator 的理由是:这些功能需要由用户动态决定加入的方式和时机.Decorator 提供
了"即插即用"的方法,在运行期间决定何时增加何种功能.

*对于该模式,初步归纳为
1.基本功能为接口
2.Decorator参数为接口本身也为接口以便为下一个Decorator的参数
3.基本功能类实现接口 并作为Decorator构造函数的参数,以便在此基础上添加新功能
4.额外功能由Decorator中的数据结构处理

二:问题:
这是一段Decorator设计模式的实现例子如下

基本功能:Counter类
需要添加的功能
1:上限控制
2:下限控制


import Java.io.*;
class Counter{
private int value;
public Counter(int v){
 System.out.println("init me here in The Counter with value!");
 value=v;}
public Counter(Counter cc){
 System.out.println("init me here in The Counter with class!");
 value=cc.value;}
public int read_value(){
    System.out.println("read me here The value is:"+value);
 System.out.println("read me here in The Counter!");
 
 return value;}
public void increment(){
 System.out.println("increment me here in The Counter !");
 value++;}
public void decrement(){
 System.out.println("decrement me here in The Counter !");
                        value--;}
}

class Decorator extends Counter
{
 Counter counter;
 public Decorator(Counter c)
 {
  super(c); 
  System.out.println("init me here  with class Decorator!");
 }
}

共4页 9 7 [1] [2] [3] [48 :>

decorator模式使用中遭遇继承与聚合的冲突问题 相关文章:
decorator模式使用中遭遇继承与聚合的冲突问题 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com