装饰者模式

装饰者模式应用场合:当需要为一个特定实物加上各式各样职责的时候,可能未来还有更多的职责要附加在该实物身上,为了符合设计模式中的一个原则---开放关闭原则,在不去修改类、只要增加一些类的情况下,便可达到这种目的。

比如:1.为一个人进行装饰,衣服、裤子、鞋子、帽子等、一个人每天都有不同的装饰,这是无法去预测的。

            2.为饮料中加入不同的调料,每个人的口味不同,这也是无法预测的。

为了解决这类问题,使用在运行时进行组合方式能够很好的解决这类问题。---装饰者模式~~

装饰者模式的基本结构图:

 

以一个场景模拟装饰者模式:一个人早上起来要想今天出门应该怎么打扮才好。~~~

组件接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
/**
  * 组件接口,接口中有些方法
 
  * @author ChenST
  *
  * @create 2010-3-9
  */
public interface Component {
      
     /**  打扮 */
     public void dress();
}

组件接口的实现类:一个人

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
  * 人的一个类,人需要打扮
 
  * @author Administrator
  *
  * @create 2010-3-9
  */
public class Human implements Component {
  
     /*
      * 开始打扮
      * (non-Javadoc)
      * @see com.shine.model.component.Component#dress()
      */
     public void dress() {
         System.out.print( "我今天要穿:" );
     }
}

装饰者抽象类:实现了Component接口,目的是为了和被装饰者类型保持一致性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
  * 装饰者接口,通用装饰器
 
  * @author ChenST
  *
  * @create 2010-3-9
  */
public abstract class Decorator implements Component {
  
     /*
      * 打扮
      * (non-Javadoc)
      * @see com.shine.model.component.Component#dress()
      */
     public abstract void dress();
}

具体装饰者1:--衣服。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
  * 装饰物-衣服
  * @author Administrator
  *
  * @create 2010-3-9
  */
public class Clothes extends Decorator {
  
     /** 被装饰物(装饰目标)*/
     private Component component;
      
     public Clothes(Component component){
         this .component = component;
     }
      
     @Override
     public void dress() {
         this .component.dress();
         //在原始打扮上加上该装饰-衣服
         System.out.print( "衣服" );
     }
}

具体装饰者2:--裤子

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
  * 装饰对象--裤子
 
  * @author ChenST
  *
  * @create 2010-3-9
  */
public class Trousers extends Decorator {
  
     /** 被装饰物(装饰目标)*/
     private Component component;
      
     public Trousers(Component component){
         this .component = component;
     }
      
     @Override
     public void dress() {
         this .component.dress();
         //在原始打扮上加上该装饰-裤子
         System.out.print( " 裤子 " );
     }
}

具体装饰者3:--鞋子

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
  * 装饰对象--鞋子
 
  * @author ChenST
  *
  * @create 2010-3-9
  */
public class Shoes extends Decorator {
  
     /** 被装饰物(装饰目标)*/
     private Component component;
      
     public Shoes(Component component){
         this .component = component;
     }
      
     @Override
     public void dress() {
         this .component.dress();
         //在原始打扮上加上该装饰-鞋子
         System.out.print( " 鞋子 " );
     }
}

测试类:这个人开始打扮啦~~~~~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
  * 测试装饰者模式(模拟场景:一个人出门要装扮)
 
  * @author ChenST
  *
  * @create 2010-3-9
  */
public class Test {
  
     public static void main(String[] args) {
         //描述一个人
         Component human= new Human();
         //这个人要穿衣服,用衣服去装饰,得到穿衣服后的人
         human= new Clothes(human);
         human.dress();
         System.out.println();
         //这个人要穿裤子,用裤子去装饰,得到穿衣服和裤子后的人
         human= new Trousers(human);
         human.dress();
         System.out.println();
         //这个人要穿鞋子,用鞋子去装饰,得到穿衣服、裤子和鞋子后的人
         human= new Shoes(human);
         human.dress();
     }
}

原文网址:http://cst.is-programmer.com/posts/15854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值