Last active
September 7, 2018 05:12
-
-
Save tom76kimo/e8b4106393a0915256e2646de864a28a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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