• 装饰者模式视频示例


    package com.java.jikexueyuan.coffeebar;

    public abstract class Drink {
    public String description="";
    private float price=0f;;


    public void setDescription(String description)
    {
    this.description=description;
    }

    public String getDescription()
    {
    return description+"-"+this.getPrice();
    }
    public float getPrice()
    {
    return price;
    }
    public void setPrice(float price)
    {
    this.price=price;
    }
    public abstract float cost();

    }

    package com.java.jikexueyuan.coffeebar.coffee;

    import com.java.jikexueyuan.coffeebar.Drink;

    public class Coffee extends Drink {

    @Override
    public float cost() {
    // TODO Auto-generated method stub
    return super.getPrice();
    }


    }

    package com.java.jikexueyuan.coffeebar.coffee;

    public class Decaf extends Coffee {
    public Decaf()
    {
    super.setDescription("Decaf");
    super.setPrice(3.0f);
    }
    }

    package com.java.jikexueyuan.coffeebar.coffee;

    public class Espresso extends Coffee{

    public Espresso()
    {
    super.setDescription("Espresso");
    super.setPrice(4.0f);
    }

    }

    package com.java.jikexueyuan.coffeebar.coffee;

    public class LongBlack extends Coffee{

    public LongBlack()
    {
    super.setDescription("LongBlack");
    super.setPrice(6.0f);
    }

    }

    package com.java.jikexueyuan.coffeebar.coffee;

    public class ShortBlack extends Coffee{

    public ShortBlack()
    {
    super.setDescription("ShortBlack");
    super.setPrice(5.0f);
    }

    }

    package com.java.jikexueyuan.coffeebar.decorator;

    import com.java.jikexueyuan.coffeebar.Drink;

    public class Decorator extends Drink {
    private Drink Obj;

    public Decorator(Drink Obj){
    this.Obj=Obj;
    };


    @Override
    public float cost() {
    // TODO Auto-generated method stub

    return super.getPrice()+Obj.cost();
    }

    @Override
    public String getDescription()
    {
    return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription();
    }

    }

    package com.java.jikexueyuan.coffeebar.decorator;

    import com.java.jikexueyuan.coffeebar.Drink;

    public class Milk extends Decorator {

    public Milk(Drink Obj) {
    super(Obj);
    // TODO Auto-generated constructor stub
    super.setDescription("Milk");
    super.setPrice(2.0f);
    }

    }

    package com.java.jikexueyuan.coffeebar.decorator;

    import com.java.jikexueyuan.coffeebar.Drink;

    public class Chocolate extends Decorator {

    public Chocolate(Drink Obj) {
    super(Obj);
    // TODO Auto-generated constructor stub
    super.setDescription("Chocolate");
    super.setPrice(3.0f);
    }

    }

    package com.java.jikexueyuan.coffeebar.decorator;

    import com.java.jikexueyuan.coffeebar.Drink;

    public class Soy extends Decorator {

    public Soy(Drink Obj) {
    super(Obj);
    // TODO Auto-generated constructor stub
    super.setDescription("Soy");
    super.setPrice(1.5f);
    }

    }

    package com.java.jikexueyuan.coffeebar;

    import com.java.jikexueyuan.coffeebar.coffee.Decaf;
    import com.java.jikexueyuan.coffeebar.coffee.LongBlack;
    import com.java.jikexueyuan.coffeebar.decorator.Chocolate;
    import com.java.jikexueyuan.coffeebar.decorator.Milk;

    public class CoffeeBar {


    public static void main(String[] args) {

    Drink order;
    order=new Decaf();
    System.out.println("order1 price:"+order.cost());
    System.out.println("order1 desc:"+order.getDescription());

    System.out.println("****************");
    order=new LongBlack();
    order=new Milk(order);
    order=new Chocolate(order);
    order=new Chocolate(order);
    System.out.println("order2 price:"+order.cost());
    System.out.println("order2 desc:"+order.getDescription());

    }


    }

  • 相关阅读:
    vue路由懒加载
    Git文档
    Redis启动多个实例,并以windows服务方式运行
    windwos service安装命令
    sqlserver随机查询
    Redis 主从配置
    Unity3D 学习资料
    MAC常用终端命令
    服务器证书安装配置指南(IIS7)
    sql Exists与in 的区别
  • 原文地址:https://www.cnblogs.com/wadmwz/p/8907161.html
Copyright © 2020-2023  润新知