码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-22 14:49:02      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:ring   ret   描述   public   继承   auth   rac   分支   show   

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

}
}

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

标签:ring   ret   描述   public   继承   auth   rac   分支   show   

原文地址:https://www.cnblogs.com/wadmwz/p/8907149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!