From c6461a37ef140a96674a94a7092831954795a72d Mon Sep 17 00:00:00 2001 From: sivaganeshrk Date: Wed, 12 Oct 2022 22:58:54 +0530 Subject: [PATCH] Added Creational Abstract Factory for Typescript --- .../typescript/abstract_factory.ts | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Creational/Abstract Factory/typescript/abstract_factory.ts diff --git a/Creational/Abstract Factory/typescript/abstract_factory.ts b/Creational/Abstract Factory/typescript/abstract_factory.ts new file mode 100644 index 00000000..1df3e888 --- /dev/null +++ b/Creational/Abstract Factory/typescript/abstract_factory.ts @@ -0,0 +1,87 @@ +interface AbstractProductA { + usefulFunctionA(): string; +} + +interface AbstractProductB { + usefulFunctionB(): string; + + anotherUsefulFunctionB(collaborator: AbstractProductA): string; +} + +interface AbstractFactory { + createProductA(): AbstractProductA; + + createProductB(): AbstractProductB; +} + +class ConcreteProductA1 implements AbstractProductA { + usefulFunctionA(): string { + return "The result of the product A1."; + } +} + +class ConcreteProductA2 implements AbstractProductA { + usefulFunctionA(): string { + return "The result of the product A2."; + } +} + +class ConcreteProductB1 implements AbstractProductB { + usefulFunctionB(): string { + return "The result of the product B1."; + } + + anotherUsefulFunctionB(collaborate: AbstractProductA): string { + const result = collaborate.usefulFunctionA(); + return `The result of the B1 collaborating with the ${result}`; + } +} + +class ConcreteProductB2 implements AbstractProductB { + usefulFunctionB(): string { + return "The result of the product B2."; + } + + anotherUsefulFunctionB(collaborate: AbstractProductA): string { + const result = collaborate.usefulFunctionA(); + return `The result of the B2 collaborating with the ${result}`; + } +} + +class ConcreteFactory1 implements AbstractFactory { + public createProductA(): AbstractProductA { + return new ConcreteProductA1(); + } + + public createProductB(): AbstractProductB { + return new ConcreteProductB1(); + } +} + +class ConcreteFactory2 implements AbstractFactory { + public createProductA(): AbstractProductA { + return new ConcreteProductA2(); + } + + public createProductB(): AbstractProductB { + return new ConcreteProductB2(); + } +} + +function consumer(factory: AbstractFactory) { + const productA = factory.createProductA(); + const productB = factory.createProductB(); + + console.log(productB.usefulFunctionB()); + console.log(productB.anotherUsefulFunctionB(productA)); +} + +console.log("Client: Testing client code with the first factory type..."); +consumer(new ConcreteFactory1()); + +console.log(""); + +console.log( + "Client: Testing the same client code with the second factory type..." +); +consumer(new ConcreteFactory2());