特色:html
/** * 組件類接口 */ public interface IComponent { void operation(); } /** * 具體組件類的具體業務邏輯實現 */ public class Component implements IComponent { @Override public void operation() { System.out.println("component operation!"); } } /** * 裝飾器的抽象類 */ public abstract class ADecorator implements IComponent { /** * 關鍵在於這個組合組件接口對象 */ private IComponent component; public ADecorator(IComponent component) { this.component = component; } @Override public void operation () { component.operation(); } } /** * 裝飾器具體實現1 */ public class Decorator1 extends ADecorator { public Decorator1(IComponent component) { super(component); } @Override public void operation() { super.operation(); System.out.println("decorator1"); } } /** * 裝飾器具體實現2 */ public class Decorator2 extends ADecorator { public Decorator2(IComponent component) { super(component); } @Override public void operation() { super.operation(); System.out.println("decorator2"); } }
/** * 應用與測試類 */ public class Test { public static void main(String[] args) { //應用類 IComponent component; //初始化 component = new Component(); //裝飾 component = new Decorator1(component); component = new Decorator2(component); //具體方法的調用 component.operation(); } }
component operation! decorator1 decorator2
裝飾着模式中的各組件:java
咱們買煎餅果子能夠選擇加香腸,仍是加雞蛋。煎餅果子就是咱們的組件類,而加雞蛋加培根則是裝飾者的具體實現類.
/** * 煎餅的抽象類,也就是組建類 */ public abstract class ABattercake { /** * 最後的描述 * @return */ protected abstract String getDesc (); /** * 總共的價格 * @return */ protected abstract int cost (); } /** * 煎餅的具體實現 */ public class Battercake extends ABattercake { @Override protected String getDesc() { return "煎餅"; } @Override protected int cost() { return 8; } } /** * 裝飾着的抽象類 */ public abstract class AbstractDecorator extends ABattercake { //組合的煎餅對象 private ABattercake aBattercake; public AbstractDecorator(ABattercake aBattercake) { this.aBattercake = aBattercake; } protected abstract void doSomething(); @Override protected String getDesc() { return aBattercake.getDesc(); } @Override protected int cost() { return aBattercake.cost(); } } /** * 裝飾者的具體實現 */ public class EggDecorator extends AbstractDecorator { public EggDecorator(ABattercake aBattercake) { super(aBattercake); } @Override protected void doSomething() { } @Override protected String getDesc() { return super.getDesc() + " 加一個雞蛋"; } @Override protected int cost() { return super.cost() + 1; } } /** * 裝飾者的具體實現 */ public class SausageDecorator extends AbstractDecorator { public SausageDecorator(ABattercake aBattercake) { super(aBattercake); } @Override protected void doSomething() { } @Override protected String getDesc() { return super.getDesc() + " 加一根香腸"; } @Override protected int cost() { return super.cost() + 2; } }
public class Test { public static void main(String[] args) { ABattercake aBattercake; aBattercake = new Battercake(); aBattercake = new EggDecorator(aBattercake); aBattercake = new EggDecorator(aBattercake); aBattercake = new SausageDecorator(aBattercake); System.out.println(aBattercake.getDesc() + "銷售價格:" + aBattercake.cost()); } }
煎餅 加一個雞蛋 加一個雞蛋 加一根香腸銷售價格:12
裝飾者模式和代理模式git
裝飾着模式和適配器模式github
設計模式之裝飾着模式
: https://github.com/sigmako/design-pattern/tree/master/decorator編程
慕課網設計模式精講
: https://coding.imooc.com/class/270.html 《JAVA與模式》之裝飾模式
: https://www.cnblogs.com/java-my-life/archive/2012/04/20/2455726.html