Skip to content

Commit

Permalink
Merge pull request #446 from sivaganeshrk/Creational/AbstractFactory_…
Browse files Browse the repository at this point in the history
…typescript

Creational/abstract factory typescript
  • Loading branch information
ZoranPandovski authored Oct 12, 2022
2 parents 9edc877 + c6461a3 commit 7bbe09a
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Creational/Abstract Factory/typescript/abstract_factory.ts
Original file line number Diff line number Diff line change
@@ -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());

0 comments on commit 7bbe09a

Please sign in to comment.