首 页 ┆ 源码下载 ┆ IT学院 ┆ 字体下载 ┆ 模板下载 ┆ 源码发布 ┆ 广告合作 ┆ 网站地图 ┆ 虚拟主机 ┆ 中文域名
► 设为首页
► 加入收藏
► 联系我们
源码下载 >> ASP源码 | PHP源码 | ASP.net源码 | JSP源码 | CGI源码 | VC/C++源码 | VB源码 | Delphi源码 | Flash源码
文章学院 >> 网络编程 | 网页设计 | 图形图象 | 数据库 | 服务器 | 网络媒体 | 网络安全 | 操作系统 | 办公软件 | 软件开发 | 黑客知识
字体下载 >> 精制字体 | 非英字体 | 艺术字体 | 著名字体 | 哥特式 | 简单字体 | 手写体 | 节假日 | 图案字体 | 精度像素 | 中文字体
模板下载 >> 企业门户 | 数码网络 | 休闲娱乐 | 影视音乐 | 旅游名胜 | 文化艺术 | 电子商务 | 个性展示 | 登陆导航 | Flash模板
►►您当前的位置:源码园 → IT学院 → 软件开发 → Java编程 → 文章内容

Decorator模式中遭遇继承与聚合

作者:M_D_R  来源:网上收集  发布时间:2005-11-15 10:24:10
  一:背景: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!");
  } 
  }

  class UpperLimit extends Decorator//上限控制
  {
  public UpperLimit(Counter c)
  {
  super(c);
  counter=c;
  System.out.println("init me here with class UpperLimit!");
  }
  public void increment()
  {
  if(counter.read_value()>20)
  {
   System.out.println("Too High");
  }
  else
  {
   System.out.println("increment me here with class UpperLimit!");
   counter.increment();
  }
  }
  /*public void decrement()
  {
  counter.decrement();
  }
  public int read_value()
  {
  return counter.read_value();
  }*/

  }

  class LowerLimit extends Decorator//下限控制
  {
  public LowerLimit(Counter c)
  {
  super(c);
  counter=c;
  System.out.println("init me here in The Counter with class LowerLimit!");
  }
  public void decrement()
  {
  System.out.println("Class value :"+read_value());
  System.out.println("Dec value :"+counter.read_value());
  if(counter.read_value()<=0)
  {
   System.out.println(counter.read_value());
   System.out.println("Too Low");
  }
  else
  {
   System.out.println("decrement me here in The Counter with class LowerLimit!");
   counter.decrement();
  }
  }
  /*public void increment()
  {
  counter.increment();
  }
  public int read_value()
  {
  return counter.read_value();
  }*/
  }

  class CounterFactory
  {
  public static Counter createCounter(int value,int op)
  {
  switch(op)
  {
   case 1:
   {
    return new Counter(value);
   }
   case 2:
   {
    return new UpperLimit(new Counter(value));
   }
   case 3:
   {
    return new LowerLimit(new Counter(value));
   }
   default:
   {
    return new UpperLimit(new LowerLimit(new Counter(value)));
   }
  }
  }

  }
  class Console
  {
  private static BufferedReader read=new BufferedReader(new InputStreamReader(System.in));

  public static int readInt(String index){
  System.out.println(index);
  try{
   return Integer.parseInt(read.readLine());
  }
  catch(Exception e){
   return 0;
  }
  }
  }

  public class Q1s{
  public static void main(String[] args){
  System.out.println("Counter Type:");
  System.out.println("1: Normal");
  System.out.println("2: Upper Limit");
  System.out.println("3: Lower Limit");
  System.out.println("4: Upper & Lower Limit");
  int option=Console.readInt("Enter Choice:");
  Counter c = CounterFactory.createCounter(6,option);
  int choice=1;
  while(choice!=4){
   System.out.println("1: Increment");
   System.out.println("2: Decrement");
   System

[1] [2]  下一页

[] [返回上一页] [打 印]
  • 上一篇文章:EJB 3.0 开发指南之依赖值对象
  • 下一篇文章:EJB 3.0 开发指南之实体Bean

  • 相关文章:
  • Decorator模式中遭遇继承与聚合
  • Linux 指令篇:编码压缩打包--uudecode
  • Linux指令篇编码压缩打包--uudecode
  • Oracle中Decode()函数使用技巧
  • Decode 函数的用法
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 源码发布
Copyright © 2003-2009 Ymyasp.Com. All Rights Reserved .
备案序号:粤ICP备07029071号