-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #446 from sivaganeshrk/Creational/AbstractFactory_…
…typescript Creational/abstract factory typescript
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
Creational/Abstract Factory/typescript/abstract_factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |