Skip to content

Instantly share code, notes, and snippets.

@tom76kimo
Last active September 7, 2018 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom76kimo/e8b4106393a0915256e2646de864a28a to your computer and use it in GitHub Desktop.
Save tom76kimo/e8b4106393a0915256e2646de864a28a to your computer and use it in GitHub Desktop.
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