Skip to content

Commit 32deed8

Browse files
committed
设计模式-适配器模式,备忘录模式
1 parent ed61257 commit 32deed8

File tree

9 files changed

+207
-0
lines changed

9 files changed

+207
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.design.adapter;
2+
3+
/**
4+
* 银行适配器
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/1 10:40
8+
*/
9+
public class BankAdapter {
10+
11+
public Rmb changeRmbMoney(Dollar dollar) {
12+
return new Rmb(dollar.getAmount() * 7);
13+
}
14+
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.design.adapter;
2+
3+
/**
4+
* 美元
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/1 10:40
8+
*/
9+
public class Dollar {
10+
11+
private int amount;
12+
13+
public Dollar(int amount) {
14+
this.amount = amount;
15+
}
16+
17+
public int getAmount() {
18+
return amount;
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.design.adapter;
2+
3+
/**
4+
* 适配器模式是作为两个不兼容的接口之间的桥梁
5+
* 将一个类的接口转换为期望的另一个接口,使原本不兼容的类可以一起工作
6+
*
7+
* @author wliduo[i@dolyw.com]
8+
* @date 2023/2/1 10:37
9+
*/
10+
public class Main {
11+
12+
public static void main(String[] args) {
13+
// 商店购买只能使用人民币
14+
Shop shop = new Shop();
15+
Rmb rmb = new Rmb(100);
16+
// 购买成功
17+
shop.buy(rmb);
18+
System.out.println("------------");
19+
// 现在有美元无法购买
20+
Dollar dollar = new Dollar(10);
21+
// shop.buy(dollar);
22+
// 使用银行适配器完成
23+
BankAdapter bankAdapter = new BankAdapter();
24+
shop.buy(bankAdapter.changeRmbMoney(dollar));
25+
}
26+
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.design.adapter;
2+
3+
/**
4+
* 人民币
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/1 10:40
8+
*/
9+
public class Rmb {
10+
11+
private int amount;
12+
13+
public Rmb(int amount) {
14+
this.amount = amount;
15+
}
16+
17+
public int getAmount() {
18+
return amount;
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.design.adapter;
2+
3+
/**
4+
* 商店
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/1 11:08
8+
*/
9+
public class Shop {
10+
11+
public void buy(Rmb rmb) {
12+
System.out.println("购买成功,消费" + rmb.getAmount() + "元人民币");
13+
}
14+
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.design.memento;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 游戏记录管理
8+
*
9+
* @author wliduo[i@dolyw.com]
10+
* @date 2023/2/2 10:45
11+
*/
12+
public class GameCareTaker {
13+
14+
private List<GameMemento> gameMementoList = new ArrayList<>();
15+
16+
public void add(GameMemento gameMemento){
17+
gameMementoList.add(gameMemento);
18+
}
19+
20+
public GameMemento get(int index){
21+
return gameMementoList.get(index);
22+
}
23+
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.design.memento;
2+
3+
/**
4+
* 游戏记录
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 10:45
8+
*/
9+
public class GameMemento {
10+
11+
private String state;
12+
13+
public GameMemento(String state){
14+
this.state = state;
15+
}
16+
17+
public String getState(){
18+
return state;
19+
}
20+
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.design.memento;
2+
3+
/**
4+
* 当前游戏状态
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 10:45
8+
*/
9+
public class GameOriginator {
10+
11+
private String state;
12+
13+
public GameOriginator(String state) {
14+
this.state = state;
15+
}
16+
17+
public void setState(String state){
18+
this.state = state;
19+
}
20+
21+
public String getState(){
22+
return state;
23+
}
24+
25+
public GameMemento saveMemento(){
26+
return new GameMemento(state);
27+
}
28+
29+
public void getFromMemento(GameMemento gameMemento){
30+
state = gameMemento.getState();
31+
}
32+
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.design.memento;
2+
3+
/**
4+
* 备忘录模式 - 游戏存档,浏览器历史记录
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2023/2/2 10:45
8+
*/
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
GameCareTaker gameCareTaker = new GameCareTaker();
13+
// 开始游戏
14+
GameOriginator gameOriginator = new GameOriginator("1# 你出生了,你正在新手村出生位置");
15+
System.out.println(gameOriginator.getState());
16+
// 游戏存档
17+
gameCareTaker.add(gameOriginator.saveMemento());
18+
gameOriginator.setState("2# 去找村长对话");
19+
System.out.println(gameOriginator.getState());
20+
// 游戏存档
21+
gameCareTaker.add(gameOriginator.saveMemento());
22+
gameOriginator.setState("3# 获得任务,杀死史莱姆");
23+
System.out.println(gameOriginator.getState());
24+
// 游戏读取存档
25+
gameOriginator.getFromMemento(gameCareTaker.get(1));
26+
System.out.println(gameOriginator.getState());
27+
// 游戏读取存档
28+
gameOriginator.getFromMemento(gameCareTaker.get(0));
29+
System.out.println(gameOriginator.getState());
30+
}
31+
32+
}

0 commit comments

Comments
 (0)