Advertisement
sdfxs

Untitled

May 11th, 2021
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. interface VehicleFactory {
  2.     createCar(): Car;
  3.     createShip(): Ship;
  4. }
  5.  
  6. class SportFactory implements VehicleFactory {
  7.     public createCar(): Car {
  8.         return new SportCar()
  9.     }
  10.     public createShip(): Ship {
  11.         return new SportShip()
  12.     }
  13. }
  14.  
  15. class MiniFactory implements VehicleFactory {
  16.     public createCar(): Car {
  17.         return new MiniCar()
  18.     }
  19.     public createShip(): Ship {
  20.         return new MiniShip()
  21.     }
  22. }
  23.  
  24. interface Car {
  25.     info(): void;
  26. }
  27.  
  28. class SportCar implements Car {
  29.     public info(): void {
  30.         console.log('SportCar')
  31.     };
  32. }
  33.  
  34. class MiniCar implements Car {
  35.     public info(): void {
  36.         console.log('MiniCar')
  37.     };
  38. }
  39.  
  40. interface Ship {
  41.     info(): void;
  42. }
  43.  
  44. class SportShip implements Ship {
  45.     public info(): void {
  46.         console.log('SportShip')
  47.     }
  48. }
  49.  
  50. class MiniShip implements Ship {
  51.     public info(): void {
  52.         console.log('MiniShip')
  53.     }
  54. }
  55.  
  56. function clientCode(factory: VehicleFactory) {
  57.     const car = factory.createCar();
  58.     const ship = factory.createShip();
  59.     car.info()
  60.     ship.info();
  61. }
  62.  
  63. console.log('SportFactory type:');
  64. clientCode(new SportFactory());
  65.  
  66. console.log('MiniFactory type:');
  67. clientCode(new MiniFactory());
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement