Skip to content

Commit

Permalink
Merge pull request #95 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update docs
  • Loading branch information
GuoXiCheng authored Oct 31, 2023
2 parents 7435673 + c8b1a46 commit 0bea696
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/design-pattern/behavioral/template-method.md
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();
```

:::

0 comments on commit 0bea696

Please sign in to comment.