装饰者模式是一种为函数或类增添特性的技术,它可以让我们在不修改原来对象的基础上,为其增添新的能力和行为。它本质上也是一个函数(在javascipt中,类也只是函数的语法糖)。

我们来假设一个场景,一个自行车商店有几种型号的自行车,现在商店允许用户为每一种自行车提供一些额外的配件,比如前灯、尾灯、铃铛等。每选择一种或几种配件都会影响自行车的售价。

如果按照比较传统的创建子类的方式,就等于我们目前有一个自行车基类,而我们要为每一种可能的选择创建一个新的类。可是由于用户可以选择一种或者几种任意的配件,这就导致最终可能会生产几十上百个子类,这明显是不科学的。然而,对这种情况,我们可以使用装饰者模式来解决这个问题。

自行车的基类如下:

  1. class Bicycle {
  2. // 其它方法
  3. wash () {}
  4. ride () {}
  5. getPrice() {
  6. return 200;
  7. }
  8. }

那么我们可以先创建一个装饰者模式基类

  1. class BicycleDecotator {
  2. constructor(bicycle) {
  3. this.bicycle = bicycle;
  4. }
  5. wash () {
  6. return this.bicycle.wash();
  7. }
  8. ride () {
  9. return this.bicycle.ride();
  10. }
  11. getPrice() {
  12. return this.bicycle.getPrice();
  13. }
  14. }

这个基类其实没有做什么事情,它只是接受一个Bicycle实例,实现其对应的方法,并且将调用其方法返回而已。

有了这个基类之后,我们就可以根据我们的需求对原来的Bicycle类为所欲为了。比如我可以创建一个添加了前灯的装饰器以及添加了尾灯的装饰器:

  1. class HeadLightDecorator extends BicycleDecorator {
  2. constructor(bicycle) {
  3. super(bicycle);
  4. }
  5. getPrice() {
  6. return this.bicycle.getPrice() + 20;
  7. }
  8. }
  9. class TailLightDecorator extends BicycleDecorator {
  10. constructor(bicycle) {
  11. super(bicycle);
  12. }
  13. getPrice() {
  14. return this.bicycle.getPrice() + 20;
  15. }
  16. }

那么,接下来我们就可以来对其自由组合了:

  1. let bicycle = new Bicycle();
  2. console.log(bicycle.getPrice()); // 200
  3. bicycle = new HeadLightDecorator(bicycle); // 添加了前灯的自行车
  4. console.log(bicycle.getPrice()); // 220
  5. bicycle = new TailLightDecorator(bicycle); // 添加了前灯和尾灯的自行车
  6. console.log(bicycle.getPrice()); // 240

这样写的好处是什么呢?假设说我们有10个配件,那么我们只需要写10个配件装饰器,然后就可以任意搭配成不同配件的自行车并计算价格。而如果是按照子类的实现方式的话,10个配件可能就需要有几百个甚至上千个子类了。

从例子中我们可以看出装饰者模式的适用场合:

  1. 如果你需要为类增添特性或职责,可是从类派生子类的解决方法并不太现实的情况下,就应该使用装饰者模式。
  2. 在例子中,我们并没有对原来的Bicycle基类进行修改,因此也不会对原有的代码产生副作用。我们只是在原有的基础上增添了一些功能。因此,如果想为对象增添特性又不想改变使用该对象的代码的话,则可以采用装饰者模式。

装饰者模式除了可以应用在类上之外,还可以应用在函数上(其实这就是高阶函数)。比如,我们想测量函数的执行时间,那么我可以写这么一个装饰器:

  1. function func() {
  2. console.log('func');
  3. }
  4. function timeProfileDecorator(func) {
  5. return (...args) => {
  6. const startTime = new Date();
  7. func.call(this, ...args);
  8. const elapserdTime = (new Date()).getTime() - startTime.getTime();
  9. console.log(`该函数消耗了${elapserdTime}ms`);
  10. }
  11. }
  12. const newFunc = timeProfileDecorator(func);
  13. console.log(newFunc());

既然知道了装饰者模式可以在不修改原来代码的情况下为其增添一些新的功能,那么我们就可以来做一些有趣的事情。

我们可以为一个类的方法提供性能分析的功能。

  1. class TimeProfileDecorator {
  2. constructor(component, keys) {
  3. this.component = component;
  4. this.timers = {};
  5. const self = this;
  6. for (let i in keys) {
  7. let key = keys[i];
  8. if (typeof component[key] === 'function') {
  9. this[key] = function(...args) {
  10. this.startTimer(key);
  11. // 解决this引用错误问题
  12. component[key].call(component, ...args);
  13. this.logTimer(key);
  14. }
  15. }
  16. }
  17. }
  18. startTimer(namespace) {
  19. this.timers[namespace] = new Date();
  20. }
  21. logTimer(namespace) {
  22. const elapserdTime = (new Date()).getTime() - this.timers[namespace].getTime();
  23. console.log(`该函数消耗了${elapserdTime}ms`);
  24. }
  25. }
  26. // example
  27. class Test {
  28. constructor() {
  29. this.name = 'cjg';
  30. this.age = 22;
  31. }
  32. sayName() {
  33. console.log(this.name);
  34. }
  35. sayAge() {
  36. console.log(this.age);
  37. }
  38. }
  39. let test1 = new Test();
  40. test1 = new TimeProfileDecorator(test1, ['sayName', 'sayAge']);
  41. console.log(test1.sayName());
  42. console.log(test1.sayAge());

节流函数or防抖函数

  1. function throttle(func, delay) {
  2. const self = this;
  3. let tid;
  4. return function(...args) {
  5. if (tid) return;
  6. tid = setTimeout(() => {
  7. func.call(self, ...args);
  8. tid = null;
  9. }, delay);
  10. }
  11. }
  12. function debounce(func, delay) {
  13. const self = this;
  14. let tid;
  15. return function(...args) {
  16. if (tid) clearTimeout(tid);
  17. tid = setTimeout(() => {
  18. func.call(self, ...args);
  19. tid = null;
  20. }, delay);
  21. }
  22. }

缓存函数返回值

  1. // 缓存函数结果,对于一些计算量比较大的函数效果比较明显。
  2. function memorize(func) {
  3. const cache = {};
  4. return function (...args) {
  5. const key = JSON.stringify(args);
  6. if (cache[key]) {
  7. console.log('缓存了');
  8. return cache[key];
  9. }
  10. const result = func.call(this, ...args);
  11. cache[key] = result;
  12. return result;
  13. };
  14. }
  15. function fib(num) {
  16. return num < 2 ? num : fib(num - 1) + fib(num - 2);
  17. }
  18. const enhanceFib = memorize(fib);
  19. console.log(enhanceFib(40));
  20. console.log(enhanceFib(40));
  21. console.log(enhanceFib(40));
  22. console.log(enhanceFib(40));

构造React高阶组件,为组件增加额外的功能,比如为组件提供shallowCompare功能:

  1. import React from 'react';
  2. const { Component } = react;
  3. const ShadowCompareDecorator = (Instance) => class extends Component {
  4. shouldComponentUpdate(nextProps, nextState) {
  5. return !shallowCompare(this.props, nextProps) ||
  6. !shallowCompare(this.state, nextState);
  7. }
  8. render() {
  9. return (
  10. <Instance {...this.props} />
  11. );
  12. }
  13. };
  14. export default ShadowCompareDecorator;

当然,你如果用过react-redux的话,你肯定也用过connect。其实connect也是一种高阶组件的方式。它通过装饰者模式,从Provider的context里拿到全局的state,并且将其通过props的方式传给原来的组件。

使用装饰者模式可以让我们为原有的类和函数增添新的功能,并且不会修改原有的代码或者改变其调用方式,因此不会对原有的系统带来副作用。我们也不用担心原来系统会因为它而失灵或者不兼容。就我个人而言,我觉得这是一种特别好用的设计模式。

一个好消息就是,js的装饰器已经加入了es7的草案里啦。它让我们可以更加优雅的使用装饰者模式,如果有兴趣的可以添加下babel的plugins插件提前体验下。阮一峰老师的这个教程http://es6.ruanyifeng.com/#docs/decorator也十分浅显易懂

参考文献:

**Javascript设计模式

版权声明:本文为chenjg原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chenjg/p/9616616.html