SHARE
TWEET

Untitled

a guest Apr 20th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. abstract class Creator {
  2.     public abstract factoryMethod(): Product;
  3.  
  4.     public someOperation(): string {
  5.         const product = this.factoryMethod();
  6.  
  7.         return `Creator: The same has just worked with ${product.operation()}`;
  8.     }
  9. }
  10.  
  11. class ConcreteCreator1 extends Creator {
  12.     public factoryMethod(): Product {
  13.         return new ConcreteProduct1();
  14.     }
  15. }
  16.  
  17. class ConcreteCreator2 extends Creator {
  18.     public factoryMethod(): Product {
  19.         return new ConcreteProduct2();
  20.     }
  21. }
  22.  
  23. class ConcreteProduct1 implements Product {
  24.     public operation(): string {
  25.         return `Result of the ConcreteProduct1`;
  26.     }
  27. }
  28.  
  29. class ConcreteProduct2 implements Product {
  30.     public operation(): string {
  31.         return `Result of the ConcreteProduct2`;
  32.     }
  33. }
  34.  
  35. interface Product {
  36.     operation(): string;
  37. }
  38.  
  39.  
  40. /**
  41.  *
  42.  */
  43.  
  44. function clientCode(creator: Creator) {
  45.     console.log(`ClientCode: I'm not aware of the creator's class but it still works!`);
  46.     console.log(creator.someOperation());
  47. }
  48.  
  49. console.log(`App launched with the ConcreteCreator1`);
  50. clientCode(new ConcreteCreator1());
  51.  
  52. console.log(`App launched with the ConcreteCreator2`);
  53. clientCode(new ConcreteCreator2());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top