Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // structure pattern that wraps an object with unique internals and customize it for use in an already standardized class system
- // it adapts its specific properties and method to those already used which allows an objecy with incompatible interfaces to work together
- class Engine2 {
- simpleInterface() {
- console.log('engine 2.0 tr-tr-tr')
- }
- }
- class EngineV8 {
- complecatedInterface() {
- console.log('engine v8! - wroom wroom!')
- }
- }
- class EngineV8Adapter {
- constructor(engine) {
- this.engine = engine;
- }
- simpleInterface() {
- this.engine.complecatedInterface();
- }
- }
- class Auto {
- startEngine(engine) {
- engine.simpleInterface()
- }
- }
- // Engine 2.0
- const myCar = new Auto();
- const oldEngine = new Engine2()
- myCar.startEngine(oldEngine) // "Engine 1.0 tr-tr-tr"
- // Engine V8 with adapter
- const myCar = new Auto();
- const engineAdapter = new EngineV8Adapter(new EngineV8())
- myCar.startEngine(engineAdapter) // "Engine V8 wroom wroom!"
- // Engine v8 without adapter
- const myCar = new Auto();
- const engineAdapter = new EngineV8()
- myCar.startEngine(engineAdapter) // Error
Advertisement
Add Comment
Please, Sign In to add comment