小白学习设计模式之装饰者模式

2018-06-18 01:54:17来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

装饰者模式:
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());

}
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:安卓开发JAVA基础之初识JAVA

下一篇:Java基础学习(六)-- 递归以及文件I/O流基础详解