Last active
May 26, 2018 10:03
-
-
Save impulsenok/9eeb88f71242b17ca28a02725c6c588b to your computer and use it in GitHub Desktop.
Factory pattern TypeScript example.
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
import Button from "../buttons/button.interface"; | |
export default abstract class Dialog { | |
constructor() {} | |
public renderWindow(): void { | |
const okButton: Button = this.createButton(); | |
okButton.render(); | |
} | |
public abstract createButton(): Button; | |
} |
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
import Dialog from "./factory/AbstractDialog"; | |
import HtmlDialog from "./factory/HtmlDialog"; | |
import JavaDialog from "./factory/JavaDialog"; | |
export default class RunDemo { | |
constructor(private dialog: Dialog, | |
private dialogType: 'HTML' | 'JAVA') {} | |
private configure(): void { | |
if (this.dialogType === 'HTML') { | |
this.dialog = new HtmlDialog(); | |
} else { | |
this.dialog = new JavaDialog(); | |
} | |
} | |
private runBusinessLogic(): void { | |
this.dialog.renderWindow(); | |
} | |
public mainProcess(): void { | |
this.configure(); | |
this.runBusinessLogic(); | |
} | |
} |
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
import AbstractDialog from "./AbstractDialog"; | |
import HtmlButton from "../buttons/html_button"; | |
import Button from "../buttons/button.interface"; | |
export default class HtmlDialog extends AbstractDialog { | |
constructor() { super() } | |
public createButton(): Button { | |
return new HtmlButton(); | |
} | |
} |
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
import AbstractDialog from "./AbstractDialog"; | |
import JavaButton from "../buttons/java_button"; | |
import Button from "../buttons/button.interface"; | |
export default class JavaDialog extends AbstractDialog { | |
constructor() { super() } | |
public createButton(): Button { | |
return new JavaButton(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment