Decorator

About

  • To extend an object, without changing the original object implementation.

  • Is used for the O of SOLID.

    • Open to extension, Closed to modification.

  • Ex.: You make the DecoratedComponent implement the same interface as the object you want to extend. And in the DecoretedComponent constructor you pass the original component as an argument.

class DecoratedComponent implements SomeObject {
	constructor (readonly obj: SomeObject) {
	}

	someFunc() {
		[extendDoingMoreStuff]
		...
		return this.obj.func()
	}
}

DecoratorMiddlewareDecorator \neq Middleware, since middle kind of chains the layers in an array, and the decorators chains them kind of like recursively, one is inside of the other.

Last updated