Skip to content

Instantly share code, notes, and snippets.

@AliN11
Last active October 26, 2021 12:28
Show Gist options
  • Save AliN11/eb10fcc82074b9c983dc5904415724cc to your computer and use it in GitHub Desktop.
Save AliN11/eb10fcc82074b9c983dc5904415724cc to your computer and use it in GitHub Desktop.
interface Vehicle {
setMode(mode);
move();
}
abstract class Delivery {
public abstract makeVehicle(): Vehicle;
public handle() {
const vehicle = this.makeVehicle();
vehicle.move();
}
}
class BikeDelivery extends Delivery {
public makeVehicle() {
const bike = new Bike();
bike.setMode('eco');
return bike;
}
}
class CarDelivery extends Delivery {
public makeVehicle() {
const car = new Car();
car.setColor('green');
return car;
}
}
class Bike implements Vehicle {
setMode(mode) {}
move() {
console.log('Moving by bike');
};
}
class Car implements Vehicle {
setMode(mode) {}
move() {
console.log('Moving by car');
}
setColor(color) {}
}
const init = new CarDelivery();
init.handle();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment