装饰者模式

"设计模式"

Posted by Mzx on March 24, 2017

装饰者模式

装饰者模式一句话总结就是俄罗套娃。装饰类具有与被装饰类完全相同的及基因即相同的入参相同的返回类型。

模式结构

角色

  • 抽象类(Component)抽象功能
  • 实现类 (ConcreteComponent) 功能的具体实现类
  • 抽象装饰类 (Decorator) 抽象装饰类,定义了需要被装饰的功能接口
  • 具体装饰类 (ConcreteDecoratorA) 对接口实现
  • 具体装饰类 (ConcreteDecoratorB)
//Java 实现
abstract class Component {

    public abstract void fly();
    public abstract void run();
}

class ConcreteComponent extends Component {
    public void fly () {
        System.out.print("fly");
    }

    public void run () {
        System.out.print("run");
    }
}

abstract class Decorator {
    Component component;
    public Decorator(Component component){
        this.component = component;
    }

    public abstract void fly();
    public abstract void run();
}

class ConcreteDecoratorA extends Decorator {

    public ConcreteDecoratorA(Component c) {
        super(c);
    }
    public void fly() {
        System.out.println("before fly");
        this.component.fly();
    }
    public void run () {
        System.out.println("before run");
        this.component.run();
    }
}

public class DecoratorDemo {
    public static void main(String args[]){
        Component c = new ConcreteComponent();
        Decorator d = new ConcreteDecoratorA(c);
        d.fly();
    }
}

//运行结果:
before fly
fly
Process finished with exit code 0
	
// js实现
'use strict'

let Request = function () {

}

Request.prototype.fly = () => {
    console.log('fly')
}

Request.prototype.run = () => {
    console.log('run')
}

let Decorator = function (baseRequest) {
    this.baseRequest = baseRequest
    let methods =  Object.keys(baseRequest.__proto__)
    methods.forEach( c => { //自动识别具体类的所有方法
        Decorator.prototype[c] = function () {
            console.log('-----this is Decorator------')
            return baseRequest[c].apply(baseRequest, arguments)
        }
    })
}

let d = new Decorator(new Request())
d.fly()
//运行结果
-----this is Decorator------
fly

Process finished with exit code 0


装饰者模式可以在不影响业务代码的情况下,从横向切入加入额外的逻辑。如缓存模块,日志模块等。