You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * The base Component interface defines operations that can be altered by * decorators. */interfaceComponent{operation(): string;}/** * Concrete Components provide default implementations of the operations. There * might be several variations of these classes. */classConcreteComponentimplementsComponent{publicoperation(): string{return'ConcreteComponent';}}/** * The base Decorator class follows the same interface as the other components. * The primary purpose of this class is to define the wrapping interface for all * concrete decorators. The default implementation of the wrapping code might * include a field for storing a wrapped component and the means to initialize * it. */classDecoratorimplementsComponent{protectedcomponent: Component;constructor(component: Component){this.component=component;}/** * The Decorator delegates all work to the wrapped component. */publicoperation(): string{returnthis.component.operation();}}/** * Concrete Decorators call the wrapped object and alter its result in some way. */classConcreteDecoratorAextendsDecorator{/** * Decorators may call parent implementation of the operation, instead of * calling the wrapped object directly. This approach simplifies extension * of decorator classes. */publicoperation(): string{return`ConcreteDecoratorA(${super.operation()})`;}}/** * Decorators can execute their behavior either before or after the call to a * wrapped object. */classConcreteDecoratorBextendsDecorator{publicoperation(): string{return`ConcreteDecoratorB(${super.operation()})`;}}/** * The client code works with all objects using the Component interface. This * way it can stay independent of the concrete classes of components it works * with. */functionclientCode(component: Component){// ...console.log(`RESULT: ${component.operation()}`);// ...}/** * This way the client code can support both simple components... */constsimple=newConcreteComponent();console.log('Client: I\'ve got a simple component:');clientCode(simple);console.log('');/** * ...as well as decorated ones. * * Note how decorators can wrap not only simple components but the other * decorators as well. */constdecorator1=newConcreteDecoratorA(simple);constdecorator2=newConcreteDecoratorB(decorator1);console.log('Client: Now I\'ve got a decorated component:');clientCode(decorator2);
The text was updated successfully, but these errors were encountered:
常用到的设计模式,ts/js 中已经提供了 decorator 支持该设计模式的使用,并且提供了非常丰富的功能:参数/属性/方法装饰器等
The text was updated successfully, but these errors were encountered: