装饰器

装饰器

如果我们想为一个类增加一些功能,但是又不通过继承的方式就可以使用装饰器
使用方法:创建一个装饰类,来包装原有的类
通过一个例子来解释

class Polygin {
  describe () {
    console.log('我是一个多边形')
  }
}
class Triangle extends Polygin {
  describe () {
    super.describe()
    console.log('我也是三角形')
  }
}
class Quadrilateral extends Polygin {
  describe () {
    super.describe()
    console.log('我也是四角形')
  }
}
class FiveAngleShape extends Polygin {
  describe () {
    super.describe()
    console.log('我也是五角形')
  }
} 

上面例子定义了一个多边形和它的三个子类三角形、四边形和五边形,现在我想给三角形、四边形和五边形画上红色的背景,使用继承实现

class RedTriangle extends Triangle {
  describe () {
    super.describe()
    console.log('绘制红色的背景')
  }
}
class RedQuadrilateral extends Quadrilateral{
  describe () {
    super.describe()
    console.log('绘制红色的背景')
  }
}
class RedFiveAngleShape extends FiveAngleShape{
  describe () {
    super.describe()
    console.log('绘制红色的背景')
  }
}
var aRedTriangle = new RedTriangle()
var aRedQuadrilateral = new RedQuadrilateral()
var aRedFiveAngleShape = new RedFiveAngleShape()
aRedTriangle.describe()
aRedQuadrilateral.describe()
aRedFiveAngleShape.describe()

使用装饰器实现,只需要定义一个多边形的装饰器

class PolyginDecorator {
  constructor (polygin) {
    this.polygin = polygin
  }
  describe() {
    this.polygin.describe()
    console.log("我是红色的")
  }
}
var aRedTriangle = new PolyginDecorator(new Triangle())
var aRedQuadrilateral = new PolyginDecorator(new Quadrilateral())
var aRedFiveAngleShape = new PolyginDecorator(new FiveAngleShape())
aRedTriangle.describe()
aRedQuadrilateral.describe()
aRedFiveAngleShape.describe()

PolyginDecorator是一个装饰器,给多边形增加了一个新功能:绘制红色的背景,然后通过传入实例,实现给特定种类的多边形绘制背景色的功能,可以看出

  1. 创造了一个装饰类(PolyginDecorator),来包装原有类(Polygin)
  2. 装饰器与被装饰对象有一个同名的方法
  3. 该方法必须实现原有类的功能
  4. 该方法还提供了额外的功能

现在我想给这些多边形绘制其他的颜色,比如黄色、蓝色等等,如果使用继承来实现,那么对于每一个多边形,继续定义YellowTriangle、BlueTriangle。。。,子类就会变得非常多,代码会非常臃肿。考虑使用装饰器,通过组合的方式实现功能扩展

class PolyginDecorator {
  constructor (polygin, backgroundColor) {
    this.polygin = polygin
    this.backgroundColor = backgroundColor
  }
  describe() {
    this.polygin.describe()
    this.backgroundColor.draw()
  }
}
class BackgroundColor {
  constructor(color) {
    this.color = color
  }
  draw () {
    console.log('绘制' + this.color + '的背景')
  }
}
var aRedTriangle = new PolyginDecorator(new Triangle(), new BackgroundColor('蓝色'))
var aRedQuadrilateral = new PolyginDecorator(new Quadrilateral(), new BackgroundColor('黄色'))
var aRedFiveAngleShape = new PolyginDecorator(new FiveAngleShape(), new BackgroundColor('白色'))
aRedTriangle.describe()
aRedQuadrilateral.describe()
aRedFiveAngleShape.describe()

通过实例看出,装饰器相对于子类的优点

  1. 减少了子类的增加,避免因功能扩展激增而造成子类膨胀代码臃肿
  2. 装饰器相对于子类更加灵活,可以通过组合的方式增加一些额外的功能
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,576评论 25 709
  • 定义 装饰器模式又名包装(Wrapper)模式。装饰器模式以对客户端透明的方式拓展对象的功能,是继承关系的一种替代...
    步积阅读 36,287评论 0 38
  • 前言 本文为译文,初次翻译,如有误导,请多多包含,阅读英文,可直接戳底下原文链接即可作者:joseph Zimme...
    itclanCoder阅读 4,463评论 0 1
  • 装饰器模式 装饰器模式是一种旨在提升代码复用率的结构性模式。有点类似于混入模式,它被认为是一种可以替代子类的可行方...
    pws019阅读 2,865评论 0 0
  • 前两天我在朋友圈里发的信息是,亲们,放假了,你们都安全到家了吗?有三十多个同学跟我回复。之所以发这条信息,也是因为...
    赛北熊在重庆阅读 6,680评论 0 0

友情链接更多精彩内容