Xiaobai learns the decorator pattern of design patterns

Decorator pattern: 
1. Dynamically attach new functions to objects, it is more flexible than inheritance in terms of object function expansion
2. One base class, multiple branches

self-understanding:
take people as an example, people have people from various countries , This is a branch, and the other branch is the wearing branch. When it is cold in winter, you need to add clothes (this Gigi Lai is not particularly suitable)

. The example in the video:
using drinks as an example, the coffer has various kinds of coffee, and the main coffer is selected. You can also select accessories, continue to add accessories, and finally calculate the total price of 0.
Below is my code example, and the example in the video is placed in the next chapter
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));
}


}













Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326353438&siteId=291194637