Skip to content

Instantly share code, notes, and snippets.

@YutoKashiwagi
Created June 20, 2021 10:09
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/d8d5290382aa2a94c372f9067737dfce to your computer and use it in GitHub Desktop.
Save YutoKashiwagi/d8d5290382aa2a94c372f9067737dfce to your computer and use it in GitHub Desktop.
デザインパターン: Adapterパターン
interface IPrint {
printWeak: () => void
printStrong: () => void
}
class Banner {
private text: string
constructor(text: string) {
this.text = text
}
showWithParan = (): void => {
console.log('(' + this.text + ')')
}
showWithAster = (): void => {
console.log('*' + this.text + '*')
}
}
/**
* Adapter
*/
class PrintBanner implements IPrint {
private banner: Banner
constructor(banner: Banner) {
this.banner = banner
}
printWeak = (): void => {
this.banner.showWithParan()
}
printStrong = (): void => {
this.banner.showWithAster()
}
}
/**
* MainPrinterクラスはAdapterパターンによって振る舞いを変えずに済む
*/
class MainPrinter {
constructor(print: IPrint) {
print.printWeak()
print.printStrong()
}
}
const banner = new Banner('sample text')
const printBanner = new PrintBanner(banner)
new MainPrinter(printBanner)
// (sample text)
// *sample text*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment