Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface VehicleFactory {
- createCar(): Car;
- createShip(): Ship;
- }
- class SportFactory implements VehicleFactory {
- public createCar(): Car {
- return new SportCar()
- }
- public createShip(): Ship {
- return new SportShip()
- }
- }
- class MiniFactory implements VehicleFactory {
- public createCar(): Car {
- return new MiniCar()
- }
- public createShip(): Ship {
- return new MiniShip()
- }
- }
- interface Car {
- info(): void;
- }
- class SportCar implements Car {
- public info(): void {
- console.log('SportCar')
- };
- }
- class MiniCar implements Car {
- public info(): void {
- console.log('MiniCar')
- };
- }
- interface Ship {
- info(): void;
- }
- class SportShip implements Ship {
- public info(): void {
- console.log('SportShip')
- }
- }
- class MiniShip implements Ship {
- public info(): void {
- console.log('MiniShip')
- }
- }
- function clientCode(factory: VehicleFactory) {
- const car = factory.createCar();
- const ship = factory.createShip();
- car.info()
- ship.info();
- }
- console.log('SportFactory type:');
- clientCode(new SportFactory());
- console.log('MiniFactory type:');
- clientCode(new MiniFactory());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement