Created
June 20, 2021 10:09
-
-
Save YutoKashiwagi/d8d5290382aa2a94c372f9067737dfce to your computer and use it in GitHub Desktop.
デザインパターン: Adapterパターン
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
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