Created
June 20, 2021 10:14
-
-
Save YutoKashiwagi/c8a7d549c16dfe2210144c437afe459e to your computer and use it in GitHub Desktop.
デザインパターン: テンプレートメソッドパターン
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
abstract class AbstractDisplay { | |
abstract open: () => void | |
abstract print: () => void | |
abstract close: () => void | |
display = (): void => { | |
this.open() | |
for (let index = 0; index < 4; index++) { | |
this.print() | |
} | |
this.close() | |
} | |
} | |
class CharDisplay extends AbstractDisplay { | |
private char: string | |
constructor(char: string) { | |
super() | |
this.char = char | |
} | |
open = () => { | |
process.stdout.write('<<<') | |
} | |
print = () => { | |
process.stdout.write(this.char) | |
} | |
close = () => { | |
process.stdout.write('>>>' + '\n') | |
} | |
} | |
class StringDisplay extends AbstractDisplay { | |
private text: string | |
private width: number | |
constructor(text: string) { | |
super() | |
this.text = text | |
this.width = text.length | |
} | |
open = () => { | |
this.pringLine() | |
} | |
print = () => { | |
console.log('|' + this.text + '|') | |
} | |
close = () => { | |
this.pringLine() | |
} | |
private pringLine = (): void => { | |
process.stdout.write('+') | |
for (let index = 0; index < this.text.length; index++) { | |
process.stdout.write('-') | |
} | |
process.stdout.write('+' + '\n') | |
} | |
} | |
// TemplateMethodパターンを用いることで、charDisplayとstringDisplayで振る舞いを共通化できた | |
const charDisplay = new CharDisplay('H') | |
charDisplay.display() | |
const stringDisplay = new StringDisplay('Hello, World') | |
stringDisplay.display() | |
// <<<HHHH>>> | |
// +------------+ | |
// |Hello, World| | |
// |Hello, World| | |
// |Hello, World| | |
// |Hello, World| | |
// +------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment