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() {
console.log('origin opration');
}
}
abstract 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.addedBehavior();
}
public addedBehavior(): void {
console.log('add behavior');
// do something
}
}
class ConcreteDecorator2 extends Decorator {
public constructor(component: Component) {
super(component);
}
public operation():void {
super.operation();
}
}
const component: Component = new ConcreteComponent();
const componentDecorated: Component = new ConcreteDecorator(component);
const componentDecorated2: Component = new ConcreteDecorator2(component);
componentDecorated.operation();
componentDecorated2.operation();
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.