-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from GuoXiCheng/dev-c
update docs
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 模板方法模式 | ||
|
||
|
||
::: playground#ts 模板方法模式交互演示 | ||
|
||
@file index.ts | ||
|
||
```ts | ||
abstract class AbstractClass { | ||
// 这是模板方法 | ||
templateMethod(): void { | ||
this.getData(); | ||
this.processData(); | ||
this.saveData(); | ||
} | ||
|
||
protected abstract getData(): void; | ||
|
||
protected abstract processData(): void; | ||
|
||
protected abstract saveData(): void; | ||
} | ||
|
||
class HtmlReport extends AbstractClass { | ||
protected getData(): void { | ||
console.log("get html data"); | ||
} | ||
protected processData(): void { | ||
console.log("process html data"); | ||
} | ||
protected saveData(): void { | ||
console.log("save html data"); | ||
} | ||
} | ||
|
||
class PdfReport extends AbstractClass { | ||
protected getData(): void { | ||
console.log("get pdf data"); | ||
} | ||
protected processData(): void { | ||
console.log("process pdf data"); | ||
} | ||
protected saveData(): void { | ||
console.log("save pdf data"); | ||
} | ||
} | ||
|
||
const htmlReport = new HtmlReport(); | ||
htmlReport.templateMethod(); | ||
|
||
const pdfReport = new PdfReport(); | ||
pdfReport.templateMethod(); | ||
``` | ||
|
||
::: |