Created
July 1, 2018 19:20
-
-
Save drenther/bd99413b2e4a2efa62a5925d1c286223 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
// encapsulation | |
class Commute { | |
travel(transport) { | |
return transport.travelTime(); | |
} | |
} | |
class Vehicle { | |
travelTime() { | |
return this._timeTaken; | |
} | |
} | |
// strategy 1 | |
class Bus extends Vehicle { | |
constructor() { | |
super(); | |
this._timeTaken = 10; | |
} | |
} | |
// strategy 2 | |
class Taxi extends Vehicle { | |
constructor() { | |
super(); | |
this._timeTaken = 5; | |
} | |
} | |
// strategy 3 | |
class PersonalCar extends Vehicle { | |
constructor() { | |
super(); | |
this._timeTaken = 3; | |
} | |
} | |
// usage | |
const commute = new Commute(); | |
console.log(commute.travel(new Taxi())); // 5 | |
console.log(commute.travel(new Bus())); // 10 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment