Decorator
动态的给对象添加一些额外的属性或行为。相比于使用继承,装饰者模式更加灵活。
整个Demo结构,easy
上代码
- 装饰者和被装饰者共同的父类(接口或者抽象类均可), 后文均称为通用父类
package com.example.demo.service.decorator;
import java.math.BigDecimal;
/**
* @program: demo
* @description: 装饰者和被装饰者共同的父类, 是一个接口或者抽象类均可, 定义基本行为
* @author: Jiliang.Lee
* @create: 2019-06-20 10:00
**/
public interface CommonInterface {
/**
* 描述参与者
*/
String description = "CommonPart ";
/**
* 基本行为,用于计算结果
* @return
*/
BigDecimal calculatorResult();
String getDescription();
}
- 被装饰者,实现通用父类,实现通用父类中的方法
package com.example.demo.service.decorator;
import java.math.BigDecimal;
/**
* @program: demo
* @description: 被装饰者A
* @author: Jiliang.Lee
* @create: 2019-06-20 10:03
**/
public class ByDecoratorA implements CommonInterface {
@Override
public BigDecimal calculatorResult() {
return BigDecimal.ONE;
}
@Override
public String getDescription() {
return description+", A";
}
}
package com.example.demo.service.decorator;
import java.math.BigDecimal;
/**
* @program: demo
* @description: 被装饰者A
* @author: Jiliang.Lee
* @create: 2019-06-20 10:03
**/
public class ByDecoratorB implements CommonInterface {
@Override
public BigDecimal calculatorResult() {
return BigDecimal.TEN;
}
@Override
public String getDescription() {
return description+", B";
}
}
- 装饰者抽象父类(接口或者抽象类), 可扩展通用父类的行为
package com.example.demo.service.decorator;
/**
* @program: demo
* @description: 装饰者抽象类
* @author: Jiliang.Lee
* @create: 2019-06-20 10:12
**/
public interface Decorator extends CommonInterface {
}
- 装饰者,实现装饰者抽象父类,调用被装饰者行为,并 修饰 其结果,这里就是装饰者模式的精华所在了.
package com.example.demo.service.decorator;
import java.math.BigDecimal;
/**
* @program: demo
* @description: 装饰者A
* @author: Jiliang.Lee
* @create: 2019-06-20 10:12
**/
public class DecoratorA implements Decorator {
CommonInterface commonInterface;
public DecoratorA(CommonInterface commonInterface) {
this.commonInterface = commonInterface;
}
@Override
public BigDecimal calculatorResult() {
return commonInterface.calculatorResult().add(BigDecimal.ONE);
}
@Override
public String getDescription() {
return commonInterface.getDescription() + ", DA";
}
}
package com.example.demo.service.decorator;
import java.math.BigDecimal;
/**
* @program: demo
* @description: 装饰者A
* @author: Jiliang.Lee
* @create: 2019-06-20 10:12
**/
public class DecoratorB implements Decorator {
CommonInterface commonInterface;
public DecoratorB(CommonInterface commonInterface) {
this.commonInterface = commonInterface;
}
@Override
public BigDecimal calculatorResult() {
return commonInterface.calculatorResult().add(BigDecimal.TEN);
}
@Override
public String getDescription() {
return commonInterface.getDescription() + ", DB";
}
}
- 测试类
package com.example.demo.service.decorator;
import lombok.extern.slf4j.Slf4j;
/**
* @program: demo
* @description:
* @author: Jiliang.Lee
* @create: 2019-06-20 10:19
**/
@Slf4j
public class DecoratorTest {
public static void main(String[] args) {
CommonInterface commonInterface = new ByDecoratorA();
log.info(commonInterface.getDescription());
log.info(commonInterface.calculatorResult().toString());
//
commonInterface = new DecoratorA(commonInterface);
log.info(commonInterface.getDescription());
log.info(commonInterface.calculatorResult().toString());
commonInterface = new DecoratorA(commonInterface);
log.info(commonInterface.getDescription());
log.info(commonInterface.calculatorResult().toString());
commonInterface = new DecoratorB(commonInterface);
log.info(commonInterface.getDescription());
log.info(commonInterface.calculatorResult().toString());
}
}
- 运行结果:
总结: 对修改关闭, 对扩展开发, 同时也展现了组合和继承的用法. 多谢各路神仙指正.