Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
interface Component {
operation(): void;
}
class ConcreteComponent implements Component {
public operation() {
}
}
class Decorator implements Component {
private component: Component
public constructor(component: Component) {
this.component = component;
}
public operation() {
this.component.operation();
}
}
class ConcreteDecorator extends Decorator {
public constructor(component: Component) {
super(component);
}
public operation():void {
super.operation();
this.addedBehavior1();
}
public addedBehavior1(): void {
console.log('add behavior');
// do something
}
}
class ConcreteDecorator2 extends Decorator {
public constructor(component: Component) {
super(component);
}
public operation():void {
super.operation();
this.addedBehavior2();
}
public addedBehavior2(): void {
console.log('add behavior2');
// do something
}
}
const component: ConcreteComponent = new ConcreteComponent();
const more: ConcreteDecorator = new ConcreteDecorator(component);
const hi: ConcreteDecorator2 = new ConcreteDecorator2(more);
hi.addedBehavior2();
hi.addedBehavior1();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.