Skip to content

Instantly share code, notes, and snippets.

@drenther
Created July 1, 2018 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drenther/bd99413b2e4a2efa62a5925d1c286223 to your computer and use it in GitHub Desktop.
Save drenther/bd99413b2e4a2efa62a5925d1c286223 to your computer and use it in GitHub Desktop.
// 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