mituri

Wzorzec Factory

Feb 13th, 2021
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // jedynym celem Fabryki jest dostarczanie obiektow jakichs tam klas
  2.  
  3. class Car {
  4.     constructor(options) {
  5.     this.doors = options.doors || 4;
  6.     this.state = options.state || 'brand new';
  7.     this.color = options.color || 'white';
  8.   }
  9. }
  10.  
  11. class Truck {
  12.     constructor(options) {
  13.     this.doors = options.doors || 4;
  14.     this.state = options.state || 'used';
  15.     this.color = options.color || 'black';
  16.   }
  17. }
  18.  
  19. class VechicleFactory {
  20.     createVechicle(options) {
  21.     switch(options.vechicleType) {
  22.         case "car": return new Car(options);
  23.       case "truck": return new Truck(options);
  24.     }
  25.   }
  26. }
  27.  
  28. const factory = new VechicleFactory();
  29. const car = factory.createVechicle({
  30.     vechicleType: "car",
  31.   doors: 4,
  32.   color: "silver",
  33.   state: "crashed"
  34. })
  35. const truck = factory.createVechicle({
  36.     vechicleType: "truck",
  37.   doors: 2,
  38.   color: "red",
  39.   state: "new"
  40. })
  41.  
  42. console.log('car',car)
Advertisement
Add Comment
Please, Sign In to add comment