Skip to content

Instantly share code, notes, and snippets.

@YutoKashiwagi
Created June 20, 2021 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YutoKashiwagi/c8a7d549c16dfe2210144c437afe459e to your computer and use it in GitHub Desktop.
Save YutoKashiwagi/c8a7d549c16dfe2210144c437afe459e to your computer and use it in GitHub Desktop.
デザインパターン: テンプレートメソッドパターン
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