auto commit
This commit is contained in:
parent
5f07dd9040
commit
96ebee3dda
|
@ -633,7 +633,95 @@ public class Client {
|
|||
|
||||
### 意图
|
||||
|
||||
定义算法框架,并将一些步骤的实现延迟到子类。通过模板方法,子类可以重新定义算法的某些步骤,而不用改变算法的结构。
|
||||
定义算法框架,并将一些步骤的实现延迟到子类。
|
||||
|
||||
通过模板方法,子类可以重新定义算法的某些步骤,而不用改变算法的结构。
|
||||
|
||||
### 类图
|
||||
|
||||
<div align="center"> <img src="../pics//c3c1c0e8-3a78-4426-961f-b46dd0879dd8.png"/> </div><br>
|
||||
|
||||
### 实现
|
||||
|
||||
冲咖啡和冲茶都有类似的流程,但是某些步骤会有点不一样,要求复用那些相同步骤的代码。
|
||||
|
||||
<div align="center"> <img src="../pics//11236498-1417-46ce-a1b0-e10054256955.png"/> </div><br>
|
||||
|
||||
```java
|
||||
public abstract class CaffeineBeverage {
|
||||
|
||||
final void prepareRecipe() {
|
||||
boilWater();
|
||||
brew();
|
||||
pourInCup();
|
||||
addCondiments();
|
||||
}
|
||||
|
||||
abstract void brew();
|
||||
|
||||
abstract void addCondiments();
|
||||
|
||||
void boilWater() {
|
||||
System.out.println("boilWater");
|
||||
}
|
||||
|
||||
void pourInCup() {
|
||||
System.out.println("pourInCup");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class Coffee extends CaffeineBeverage{
|
||||
@Override
|
||||
void brew() {
|
||||
System.out.println("Coffee.brew");
|
||||
}
|
||||
|
||||
@Override
|
||||
void addCondiments() {
|
||||
System.out.println("Coffee.addCondiments");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class Tea extends CaffeineBeverage{
|
||||
@Override
|
||||
void brew() {
|
||||
System.out.println("Tea.brew");
|
||||
}
|
||||
|
||||
@Override
|
||||
void addCondiments() {
|
||||
System.out.println("Tea.addCondiments");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class Client {
|
||||
public static void main(String[] args) {
|
||||
CaffeineBeverage caffeineBeverage = new Coffee();
|
||||
caffeineBeverage.prepareRecipe();
|
||||
System.out.println("-----------");
|
||||
caffeineBeverage = new Tea();
|
||||
caffeineBeverage.prepareRecipe();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```html
|
||||
boilWater
|
||||
Coffee.brew
|
||||
pourInCup
|
||||
Coffee.addCondiments
|
||||
-----------
|
||||
boilWater
|
||||
Tea.brew
|
||||
pourInCup
|
||||
Tea.addCondiments
|
||||
```
|
||||
|
||||
### JDK
|
||||
|
||||
|
|
BIN
pics/11236498-1417-46ce-a1b0-e10054256955.png
Normal file
BIN
pics/11236498-1417-46ce-a1b0-e10054256955.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
BIN
pics/c3c1c0e8-3a78-4426-961f-b46dd0879dd8.png
Normal file
BIN
pics/c3c1c0e8-3a78-4426-961f-b46dd0879dd8.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
Loading…
Reference in New Issue
Block a user