zoukankan      html  css  js  c++  java
  • 小白学习设计模式之装饰者模式

    装饰者模式:
    1.动态的将新功能附加到对象上,在对象功能扩展方面,他比继承更具有弹性
    2.一个基类,多个分支

    自我理解:
    用人来举例,人有各个国家的人,这是一个分支,另一个分支就是穿着分支,冬季冷了添衣服就要添加衣服(这个黎姿不是特别合适)

    视频中的例子:
    使用饮品来举例,coffer有各个品种的咖啡,选定主coffer还可以选定辅料,继续添加辅料,最后计算总价0
    下面是我的代码例子,视频中的例子放在下一章
    package com.wz.decorate;

    import java.math.BigDecimal;

    /**
    * people类,描述和计算人衣服总价
    *
    * @author Administrator
    * @create 2018-04-22 13:47
    */
    public abstract class PeopleMain {

    private String description;

    private BigDecimal money = new BigDecimal(0);

    public String getDescription() {
    return description;
    }

    public BigDecimal getMoney() {
    return money;
    }

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

    public void setMoney(BigDecimal money) {
    this.money = money;
    }

    public abstract BigDecimal cost();
    }
    package com.wz.decorate;

    import java.math.BigDecimal;

    /**
    * 国家人的分类
    *
    * @author Administrator
    * @create 2018-04-22 13:49
    */
    public class People extends PeopleMain{

    @Override
    public BigDecimal cost() {
    return super.getMoney();
    }
    }
    package com.wz.decorate;

    /**
    * 中国人
    *
    * @author Administrator
    * @create 2018-04-22 13:54
    */
    public class China extends People {

    public China() {
    super.setDescription("China");
    }
    }
    package com.wz.decorate;

    /**
    * 英国人
    *
    * @author Administrator
    * @create 2018-04-22 13:54
    */
    public class Englisher extends People {

    public Englisher() {
    super.setDescription("Englisher");
    }
    }
    package com.wz.decorate;

    import java.math.BigDecimal;

    /**
    * 衣服类
    *
    * @author Administrator
    * @create 2018-04-22 13:58
    */
    public class Clothing extends PeopleMain {

    private PeopleMain pm ;

    public Clothing(PeopleMain pm) {
    this.pm = pm;
    }

    @Override
    public String getDescription() {
    return super.getDescription() + "-" +pm.getDescription();
    }

    @Override
    public BigDecimal cost() {
    return super.getMoney().add(pm.cost());
    }
    }

    package com.wz.decorate;

    import java.math.BigDecimal;

    /**
    * 裤子
    *
    * @author Administrator
    * @create 2018-04-22 14:08
    */
    public class Pants extends Clothing {

    public Pants(PeopleMain pm) {
    super(pm);
    super.setDescription("Pants");
    super.setMoney( new BigDecimal(150));
    }
    }

    package com.wz.decorate;

    import java.math.BigDecimal;

    /**
    * 鞋
    *
    * @author Administrator
    * @create 2018-04-22 14:06
    */
    public class Shoe extends Clothing {

    public Shoe(PeopleMain pm) {
    super(pm);
    super.setDescription("Show");
    super.setMoney(new BigDecimal(600));
    }
    }
    package com.wz.decorate;

    import java.math.BigDecimal;

    /**
    * 外套
    *
    * @author Administrator
    * @create 2018-04-22 14:03
    */
    public class Outerwear extends Clothing {

    public Outerwear(PeopleMain pm) {
    super(pm);
    super.setDescription("Outerwear");
    super.setMoney( new BigDecimal(300));
    }


    }
    package com.wz.decorate;

    /**
    * 测试类
    *
    * @author Administrator
    * @create 2018-04-22 14:10
    */
    public class TestMain {

    public static void main(String[] args) {
    PeopleMain pm ;
    pm = new Englisher();
    System.out.println(pm.getDescription());
    System.out.println(pm.cost());

    System.out.println("==========================================");

    pm = new China();
    pm = new Outerwear(pm);
    pm = new Pants(pm);
    pm = new Shoe(pm);
    System.out.println(pm.getDescription());
    System.out.println(pm.cost());

    }
    }
  • 相关阅读:
    Django组件——分页器
    Django与Ajax
    Python常用模块——包&跨模块代码调用
    Python常用模块——正则表达式re模块
    Python常用模块——文件复制模块shutil
    Python常用模块——hashlib加密
    git小乌龟配置
    设计模式学习(27)- MVC模式
    设计模式学习(26)- 访问者模式
    设计模式学习(25)- 模板模式
  • 原文地址:https://www.cnblogs.com/wadmwz/p/8907149.html
Copyright © 2011-2022 走看看