|
| 1 | +/** |
| 2 | + * The Abstract Factory interface declares a set of methods that return |
| 3 | + * different abstract products. These products are called a family and are |
| 4 | + * related by a high-level theme or concept. Products of one family are usually |
| 5 | + * able to collaborate among themselves. A family of products may have several |
| 6 | + * variants, but the products of one variant are incompatible with products of |
| 7 | + * another. |
| 8 | + */ |
| 9 | +interface AbstractFactory { |
| 10 | + createProductA(): AbstractProductA; |
| 11 | + |
| 12 | + createProductB(): AbstractProductB; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Concrete Factories produce a family of products that belong to a single |
| 17 | + * variant. The factory guarantees that resulting products are compatible. Note |
| 18 | + * that signatures of the Concrete Factory's methods return an abstract product, |
| 19 | + * while inside the method a concrete product is instantiated. |
| 20 | + */ |
| 21 | +class ConcreteFactory1 implements AbstractFactory { |
| 22 | + public createProductA(): AbstractProductA { |
| 23 | + return new ConcreteProductA1(); |
| 24 | + } |
| 25 | + |
| 26 | + public createProductB(): AbstractProductB { |
| 27 | + return new ConcreteProductB1(); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Each Concrete Factory has a corresponding product variant. |
| 33 | + */ |
| 34 | +class ConcreteFactory2 implements AbstractFactory { |
| 35 | + public createProductA(): AbstractProductA { |
| 36 | + return new ConcreteProductA2(); |
| 37 | + } |
| 38 | + |
| 39 | + public createProductB(): AbstractProductB { |
| 40 | + return new ConcreteProductB2(); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Each distinct product of a product family should have a base interface. All |
| 46 | + * variants of the product must implement this interface. |
| 47 | + */ |
| 48 | +interface AbstractProductA { |
| 49 | + usefulFunctionA(): string; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * These Concrete Products are created by corresponding Concrete Factories. |
| 54 | + */ |
| 55 | +class ConcreteProductA1 implements AbstractProductA { |
| 56 | + public usefulFunctionA(): string { |
| 57 | + return 'The result of the product A1.'; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class ConcreteProductA2 implements AbstractProductA { |
| 62 | + public usefulFunctionA(): string { |
| 63 | + return 'The result of the product A2.'; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Here's the the base interface of another product. All products can interact |
| 69 | + * with each other, but proper interaction is possible only between products of |
| 70 | + * the same concrete variant. |
| 71 | + */ |
| 72 | +interface AbstractProductB { |
| 73 | + /** |
| 74 | + * Product B is able to do its own thing... |
| 75 | + */ |
| 76 | + usefulFunctionB(): string; |
| 77 | + |
| 78 | + /** |
| 79 | + * ...but it also can collaborate with the ProductA. |
| 80 | + * |
| 81 | + * The Abstract Factory makes sure that all products it creates are of the |
| 82 | + * same variant and thus, compatible. |
| 83 | + */ |
| 84 | + anotherUsefulFunctionB(collaborator: AbstractProductA): string; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * These Concrete Products are created by corresponding Concrete Factories. |
| 89 | + */ |
| 90 | +class ConcreteProductB1 implements AbstractProductB { |
| 91 | + public usefulFunctionB(): string { |
| 92 | + return 'The result of the product B1.'; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * The variant, Product B1, is only able to work correctly with the variant, |
| 97 | + * Product A1. Nevertheless, it accepts any instance of AbstractProductA as |
| 98 | + * an argument. |
| 99 | + */ |
| 100 | + public anotherUsefulFunctionB(collaborator: AbstractProductA): string { |
| 101 | + const result = collaborator.usefulFunctionA(); |
| 102 | + return `The result of the B1 collaborating with the (${result})`; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +class ConcreteProductB2 implements AbstractProductB { |
| 107 | + public usefulFunctionB(): string { |
| 108 | + return 'The result of the product B2.'; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * The variant, Product B2, is only able to work correctly with the variant, |
| 113 | + * Product A2. Nevertheless, it accepts any instance of AbstractProductA as |
| 114 | + * an argument. |
| 115 | + */ |
| 116 | + public anotherUsefulFunctionB(collaborator: AbstractProductA): string { |
| 117 | + const result = collaborator.usefulFunctionA(); |
| 118 | + return `The result of the B2 collaborating with the (${result})`; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * The client code works with factories and products only through abstract |
| 124 | + * types: AbstractFactory and AbstractProduct. This lets you pass any factory or |
| 125 | + * product subclass to the client code without breaking it. |
| 126 | + */ |
| 127 | +function clientCode(factory: AbstractFactory) { |
| 128 | + const productA = factory.createProductA(); |
| 129 | + const productB = factory.createProductB(); |
| 130 | + |
| 131 | + console.log(productB.usefulFunctionB()); |
| 132 | + console.log(productB.anotherUsefulFunctionB(productA)); |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * The client code can work with any concrete factory class. |
| 137 | + */ |
| 138 | +console.log('Client: Testing client code with the first factory type...'); |
| 139 | +clientCode(new ConcreteFactory1()); |
| 140 | + |
| 141 | +console.log(''); |
| 142 | + |
| 143 | +console.log( |
| 144 | + 'Client: Testing the same client code with the second factory type...' |
| 145 | +); |
| 146 | +clientCode(new ConcreteFactory2()); |
0 commit comments