美文网首页
装饰者模式

装饰者模式

作者: wbpailxt | 来源:发表于2019-11-27 14:45 被阅读0次
    • v1

    煎饼类

    package com.geely.design.pattern.structural.decorator.v1;
    
    /**
     * Created by geely
     */
    public class Battercake {
        protected String getDesc(){
            return "煎饼";
        }
        protected int cost(){
            return 8;
        }
    }
    

    加鸡蛋的煎饼类:

    package com.geely.design.pattern.structural.decorator.v1;
    
    /**
     * Created by geely
     */
    public class BattercakeWithEgg extends Battercake {
        @Override
        public String getDesc() {
            return super.getDesc()+" 加一个鸡蛋";
        }
    
        @Override
        public int cost() {
            return super.cost()+1;
        }
    }
    

    加香肠的煎饼类:

    package com.geely.design.pattern.structural.decorator.v1;
    
    /**
     * Created by geely
     */
    public class BattercakeWithEggSausage extends BattercakeWithEgg {
        @Override
        public String getDesc() {
            return super.getDesc()+ " 加一根香肠";
        }
    
        @Override
        public int cost() {
            return super.cost()+2;
        }
    }
    

    测试类:

    package com.geely.design.pattern.structural.decorator.v1;
    
    /**
     * Created by geely
     */
    public class Test {
        public static void main(String[] args) {
            Battercake battercake = new Battercake();
            System.out.println(battercake.getDesc()+" 销售价格:"+battercake.cost());
    
            Battercake battercakeWithEgg = new BattercakeWithEgg();
            System.out.println(battercakeWithEgg.getDesc()+" 销售价格:"+battercakeWithEgg.cost());
    
            Battercake battercakeWithEggSausage = new BattercakeWithEggSausage();
            System.out.println(battercakeWithEggSausage.getDesc()+" 销售价格:"+battercakeWithEggSausage.cost());
        }
    }
    

    结果:


    图片.png

    存在问题:
    这个时候我需要煎饼加两个鸡蛋,一根香肠。难道我们又再创建一个类BattercakeWithTwoEggsSausage()吗?这样会导致类爆炸。
    在这种情况下我们引入装饰着模式。

    • v2


      图片.png

      结果:


      图片.png
      这样就实现了煎饼加多少个鸡蛋,多少根香肠都用添加类了,不至于类爆炸了。
      煎饼类:
    package com.geely.design.pattern.structural.decorator.v4;
    
    /**
     * Created by geely
     */
    public class Battercake {
    
        protected String getDesc() {
            return "煎饼";
        }
    
        protected int cost() {
            return 8;
        }
    }
    

    加了鸡蛋的煎饼类:

    package com.geely.design.pattern.structural.decorator.v4;
    
    /**
     * Created by geely
     */
    public class EggDecorator extends Battercake{
        private Battercake battercake;
        public EggDecorator(Battercake battercake) {
            this.battercake = battercake;
        }
    
        @Override
        protected String getDesc() {
            return this.battercake.getDesc()+" 加一个鸡蛋";
        }
    
        @Override
        protected int cost() {
            return this.battercake.cost()+1;
        }
    }
    

    加了香肠的煎饼类:

    package com.geely.design.pattern.structural.decorator.v4;
    
    /**
     * Created by geely
     */
    public class SausageDecorator  extends Battercake{
        private Battercake battercake;
        public SausageDecorator(Battercake aBattercake) {
            this.battercake = aBattercake;
        }
    
        @Override
        protected String getDesc() {
            return this.battercake.getDesc()+" 加一根香肠";
        }
    
        @Override
        protected int cost() {
            return this.battercake.cost()+2;
        }
    }
    

    测试类:

    package com.geely.design.pattern.structural.decorator.v4;
    
    /**
     * Created by geely
     */
    public class SausageDecorator  extends Battercake{
        private Battercake battercake;
        public SausageDecorator(Battercake aBattercake) {
            this.battercake = aBattercake;
        }
    
        @Override
        protected String getDesc() {
            return this.battercake.getDesc()+" 加一根香肠";
        }
    
        @Override
        protected int cost() {
            return this.battercake.cost()+2;
        }
    }
    

    问题:new的时候没有统一接口。

    • v3 --煎饼类、加了鸡蛋的煎饼类、加了香肠的煎饼类抽象出一个抽象类来。煎饼和加了鸡蛋的煎饼不再认为是在煎饼的基础上加鸡蛋,而认为是属于煎饼大类(ABatterCake)下的不同样式,但加了鸡蛋的煎饼的原材料依旧是煎饼。


      图片.png

      问题:
      假如你想规定加了鸡蛋的煎饼类和加了香肠的煎饼类实现某些特定的动作,可以将加了鸡蛋的煎饼类和加了香肠的煎饼类再抽象出一个抽象类。

    • v4 -- 再抽象出一个抽象类


      图片.png

    相关文章

      网友评论

          本文标题:装饰者模式

          本文链接:https://www.haomeiwen.com/subject/usxpwctx.html