pattern-design-decorator

Decorator

动态的给对象添加一些额外的属性或行为。相比于使用继承,装饰者模式更加灵活。

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());
	}
}
  • 运行结果:
    运行结果
    总结: 对修改关闭, 对扩展开发, 同时也展现了组合和继承的用法. 多谢各路神仙指正.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值