装饰器模式
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.after;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 17:04
|
||||
*/
|
||||
public class ABattercake extends AbstractBattercake {
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return "煎饼";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cost() {
|
||||
return 8;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.after;
|
||||
|
||||
/**
|
||||
* 装饰器模式按照业务场景一般有两层抽象,
|
||||
* 一层是所有类的抽象,一层是所有装饰器的抽象,
|
||||
* 所有装饰器的抽象可以为实现类提供实现额外功能的能力
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 17:02
|
||||
*/
|
||||
public abstract class AbstractBattercake {
|
||||
public abstract String getDesc();
|
||||
|
||||
public abstract int cost();
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.after;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 17:05
|
||||
*/
|
||||
public abstract class AbstractBattercakeDecorator extends AbstractBattercake {
|
||||
|
||||
private AbstractBattercake abstractBattercake;
|
||||
|
||||
/**
|
||||
* 通过构造器注入待装饰对象
|
||||
*
|
||||
* @param abstractBattercake
|
||||
*/
|
||||
public AbstractBattercakeDecorator(AbstractBattercake abstractBattercake) {
|
||||
this.abstractBattercake = abstractBattercake;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现类可以提供额外能力
|
||||
*/
|
||||
protected abstract void doSomething();
|
||||
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return this.abstractBattercake.getDesc();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cost() {
|
||||
return this.abstractBattercake.cost();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.after;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 17:08
|
||||
*/
|
||||
@Slf4j
|
||||
public class EggBattercake extends AbstractBattercakeDecorator {
|
||||
|
||||
public EggBattercake(AbstractBattercake abstractBattercake) {
|
||||
super(abstractBattercake);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doSomething() {
|
||||
log.info("EggBattercake doSomething...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return super.getDesc() + " 加一个鸡蛋";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cost() {
|
||||
return super.cost() + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.after;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 17:10
|
||||
*/
|
||||
@Slf4j
|
||||
public class SausageBattercake extends AbstractBattercakeDecorator {
|
||||
public SausageBattercake(AbstractBattercake abstractBattercake) {
|
||||
super(abstractBattercake);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doSomething() {
|
||||
log.info("SausageBattercake doSomething...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return super.getDesc() + " 加一根香肠";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cost() {
|
||||
return super.cost() + 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.before;
|
||||
|
||||
/**
|
||||
* 装饰器模式:允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。
|
||||
* 这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
|
||||
* 优点:继承的有力补充,比继承灵活,不改变原有对象的情况下给一个对象扩展功能
|
||||
* 通过使用不同装饰器排列组合实现不同效果
|
||||
* 符合开闭原则
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 16:56
|
||||
*/
|
||||
public class Battercake {
|
||||
protected String getDesc() {
|
||||
return "煎饼";
|
||||
}
|
||||
|
||||
protected int cost() {
|
||||
return 8;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.before;
|
||||
|
||||
/**
|
||||
* 不使用装饰器则只能通过类继承扩展功能,可能引起类爆炸
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 16:59
|
||||
*/
|
||||
public class BattercakeWithEgg extends Battercake {
|
||||
@Override
|
||||
protected String getDesc() {
|
||||
return super.getDesc() + " 加一个鸡蛋";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int cost() {
|
||||
return super.cost() + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.before;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-09-27 17:00
|
||||
*/
|
||||
public class BattercakeWithEggSausage extends BattercakeWithEgg {
|
||||
@Override
|
||||
protected String getDesc() {
|
||||
return super.getDesc() + " 加一根香肠";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int cost() {
|
||||
return super.cost() + 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package top.fjy8018.designpattern.pattern.structural.decorator.before;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import top.fjy8018.designpattern.pattern.structural.decorator.after.ABattercake;
|
||||
import top.fjy8018.designpattern.pattern.structural.decorator.after.AbstractBattercake;
|
||||
import top.fjy8018.designpattern.pattern.structural.decorator.after.EggBattercake;
|
||||
import top.fjy8018.designpattern.pattern.structural.decorator.after.SausageBattercake;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@Slf4j
|
||||
class BattercakeTest {
|
||||
|
||||
/**
|
||||
* 通过继承扩展灵活度低,无法自由组合
|
||||
*/
|
||||
@Test
|
||||
void before() {
|
||||
Battercake battercake = new Battercake();
|
||||
log.info(battercake.getDesc() + " 销售价格:" + battercake.cost());
|
||||
|
||||
Battercake battercakeWithEgg = new BattercakeWithEgg();
|
||||
log.info(battercakeWithEgg.getDesc() + " 销售价格:" + battercakeWithEgg.cost());
|
||||
|
||||
|
||||
Battercake battercakeWithEggSausage = new BattercakeWithEggSausage();
|
||||
log.info(battercakeWithEggSausage.getDesc() + " 销售价格:" + battercakeWithEggSausage.cost());
|
||||
}
|
||||
|
||||
@Test
|
||||
void after() {
|
||||
// 自由组合装饰
|
||||
AbstractBattercake battercake = new EggBattercake(new SausageBattercake(new ABattercake()));
|
||||
log.info(battercake.getDesc() + " 销售价格:" + battercake.cost());
|
||||
|
||||
AbstractBattercake battercake2 = new EggBattercake((new ABattercake()));
|
||||
log.info(battercake2.getDesc() + " 销售价格:" + battercake2.cost());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user