装饰者模式

Posted linbq1911

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装饰者模式相关的知识,希望对你有一定的参考价值。

1.定义:在不改变原有对象的基础之上,将功能附加到对象上;
      提供了比继承更有弹性的替代方案(扩展原有对象功能)。

2.类型:结构型

3.适用场景:扩展一个类的功能或给一个类添加附加职责;
    动态的给一个对象添加功能,这些功能可以再动态的撤销。

4.优点:继承的有力补充,比继承灵活,不改变原有对象的情况下给一个对象扩展功能;
     通过使用不同装饰类以及这些装饰类的排列组合,可以实现不同效果;
     符合开闭原则。

5.缺点:会出现更多的代码,更多的类,增加程序复杂性;
      动态装饰时,多层装饰时会更复杂。

6.相关设计模式:代理模式,适配器模式。

7.实例目录package

技术分享图片

8.实例UML类图

技术分享图片

9.代码

 1 package com.geely.design.pattern.structural.decorator.v1;
 2 
 3 public class Battercake {
 4     public String getDesc(){
 5         return "煎饼";
 6     }
 7     public int cost(){
 8         return 8;
 9     }
10 }
 1 package com.geely.design.pattern.structural.decorator.v1;
 2 
 3 public class BattercakeWithEgg extends Battercake {
 4     @Override
 5     public String getDesc() {
 6         return super.getDesc() + " 加一个鸡蛋";
 7     }
 8 
 9     @Override
10     public int cost() {
11         return super.cost() + 1;
12     }
13 }
 1 package com.geely.design.pattern.structural.decorator.v1;
 2 
 3 public class BattercakeWithEggSausage extends BattercakeWithEgg {
 4     @Override
 5     public String getDesc() {
 6         return super.getDesc() + "加一根香肠";
 7     }
 8 
 9     @Override
10     public int cost() {
11         return super.cost()+2;
12     }
13 }
 1 package com.geely.design.pattern.structural.decorator.v1;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         Battercake battercake = new Battercake();
 6         System.out.println(battercake.getDesc()+ "销售价格:" + battercake.cost());
 7 
 8         BattercakeWithEgg battercakeWithEgg = new BattercakeWithEgg();
 9         System.out.println(battercakeWithEgg.getDesc()+ "销售价格:" + battercakeWithEgg.cost());
10 
11         BattercakeWithEggSausage battercakeWithEggSausage = new BattercakeWithEggSausage();
12         System.out.println(battercakeWithEggSausage.getDesc()+ "销售价格:" + battercakeWithEggSausage.cost());
13     }
14 }
1 package com.geely.design.pattern.structural.decorator.v2;
2 
3 public abstract class ABattercake {
4     public abstract String getDesc();
5     public abstract int cost();
6 }
 1 package com.geely.design.pattern.structural.decorator.v2;
 2 
 3 public class Battercake extends ABattercake {
 4     @Override
 5     public String getDesc() {
 6         return "煎饼";
 7     }
 8 
 9     @Override
10     public int cost() {
11         return 8;
12     }
13 }
 1 package com.geely.design.pattern.structural.decorator.v2;
 2 
 3 public abstract class AbstractDecorator extends ABattercake {
 4     private ABattercake aBattercake;
 5 
 6     public AbstractDecorator(ABattercake aBattercake) {
 7         this.aBattercake = aBattercake;
 8     }
 9 
10     protected abstract void doSomething();
11 
12     @Override
13     public String getDesc() {
14         return aBattercake.getDesc();
15     }
16 
17     @Override
18     public int cost() {
19         return aBattercake.cost();
20     }
21 }
 1 package com.geely.design.pattern.structural.decorator.v2;
 2 
 3 public class EggDecorator extends AbstractDecorator {
 4     public EggDecorator(ABattercake aBattercake) {
 5         super(aBattercake);
 6     }
 7 
 8     @Override
 9     protected void doSomething() {
10 
11     }
12 
13     @Override
14     public String getDesc() {
15         return super.getDesc()+" 加一个鸡蛋";
16     }
17 
18     @Override
19     public int cost() {
20         return super.cost() + 1;
21     }
22 }
 1 package com.geely.design.pattern.structural.decorator.v2;
 2 
 3 public class SausageDecorator extends AbstractDecorator {
 4     public SausageDecorator(ABattercake aBattercake) {
 5         super(aBattercake);
 6     }
 7 
 8     @Override
 9     protected void doSomething() {
10 
11     }
12 
13     @Override
14     public String getDesc() {
15         return super.getDesc()+" 加一根香肠";
16     }
17 
18     @Override
19     public int cost() {
20         return super.cost()+2;
21     }
22 }
 1 package com.geely.design.pattern.structural.decorator.v2;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         ABattercake aBattercake;
 6         aBattercake = new Battercake();
 7         aBattercake = new EggDecorator(aBattercake);
 8         aBattercake = new EggDecorator(aBattercake);
 9         aBattercake = new SausageDecorator(aBattercake);
10 
11         System.out.println(aBattercake.getDesc()+ "销售价格:" + aBattercake.cost());
12     }
13 }

 






以上是关于装饰者模式的主要内容,如果未能解决你的问题,请参考以下文章

Java设计模式之装饰者模式

设计模式整理_装饰者模式

设计模式-装饰者模式(Go语言描述)

设计模式-装饰者模式(Go语言描述)

装饰者模式

设计模式 之 装饰者模式