Skip to content

Commit

Permalink
Merge pull request #88 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update docs
  • Loading branch information
GuoXiCheng authored Oct 24, 2023
2 parents d76d1c3 + 8a511d0 commit 1defe26
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/.vuepress/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default hopeTheme({
cachePic: true,
appendBase: true,
themeColor: "#000000",
update: 'disable',
update: 'force',
apple: {
icon: "/assets/icon/apple-icon-152.png",
statusBarColor: "black",
Expand Down
58 changes: 58 additions & 0 deletions src/design-pattern/behavioral/strategy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 策略模式

::: playground#ts 策略模式交互演示

@file index.ts

```ts
interface TaxStrategy {
caculateTax(amount: number): number;
}

class USATaxStrategy implements TaxStrategy {
caculateTax(amount: number) {
return amount * 0.2;
}
}

class ChinaTaxStrategy implements TaxStrategy {
caculateTax(amount: number) {
return amount * 0.1;
}
}

class TaxCalculate {
private strategy: TaxStrategy;

constructor(taxStrategy: TaxStrategy) {
this.strategy = taxStrategy;
}

calculate(amount: number): number {
return this.strategy.caculateTax(amount);
}
}

function calculateByCounty(country: "USA" | "China", amount: number) {
let strategy: TaxStrategy;
switch(country) {
case "USA":
strategy = new USATaxStrategy();
break;
case "China":
strategy = new ChinaTaxStrategy();
break;
default:
throw new Error("not found");
}
return new TaxCalculate(strategy).calculate(amount);
}

const calculateByUSA = calculateByCounty("USA", 10);
console.log(calculateByUSA);

const calculateByChina = calculateByCounty("China", 10);
console.log(calculateByChina);
```

:::

0 comments on commit 1defe26

Please sign in to comment.