Created
August 11, 2021 19:45
-
-
Save Babatunde50/abe22712df57f1647d5bde8a84cdb1ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| abstract class Call { | |
| /** | |
| * Note that the Creator may also provide some default implementation of the | |
| * factory method. | |
| */ | |
| public abstract factoryMethod(): Product; // what a factoryMethod returns is known as a product | |
| /** | |
| * Also note that, despite its name, the Creator's primary responsibility is | |
| * not creating products. Usually, it contains some core business logic that | |
| * relies on Product objects, returned by the factory method. Subclasses can | |
| * indirectly change that business logic by overriding the factory method | |
| * and returning a different type of product from it. | |
| */ | |
| public someOperation(): string { | |
| // Call the factory method to create a Product object. | |
| const product = this.factoryMethod(); | |
| // Now, use the product. | |
| return `Creator: The same creator's code has just worked with ${product.makeCall()}`; | |
| } | |
| } | |
| /** | |
| * Concrete Creators override the factory method in order to change the | |
| * resulting product's type. | |
| */ | |
| class VideoCall extends Call { | |
| /** | |
| * Note that the signature of the method still uses the abstract product | |
| * type, even though the concrete product is actually returned from the | |
| * method. This way the Creator can stay independent of concrete product | |
| * classes. | |
| */ | |
| public factoryMethod(): Product { | |
| return new VideoCallProduct(); | |
| } | |
| } | |
| class VoiceCall extends Call { | |
| public factoryMethod(): Product { | |
| return new VoiceCallProduct(); | |
| } | |
| } | |
| /** | |
| * The Product interface declares the operations that all concrete products must | |
| * implement. | |
| */ | |
| interface Product { | |
| makeCall(): void; | |
| } | |
| /** | |
| * Concrete Products provide various implementations of the Product interface. | |
| */ | |
| class VideoCallProduct implements Product { | |
| public makeCall() { | |
| return '{Connects to some external api and make a video call}'; | |
| } | |
| } | |
| class VoiceCallProduct implements Product { | |
| public makeCall() { | |
| return '{Make a voice call with a plugin}'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment