-
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.
- Loading branch information
Showing
5 changed files
with
128 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -42,7 +42,7 @@ | |
* ENUM模式(最安全) 已完成 | ||
* 静态内部类模式 已完成 | ||
|
||
15.备忘录模式 - 整理中 | ||
15.备忘录模式 - 已完成 | ||
|
||
16.组合模式 - 整理中 | ||
|
||
|
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,19 @@ | ||
package com.mxy.design.memento; | ||
|
||
public class Memento { | ||
|
||
private String state; | ||
|
||
public Memento(String state){ | ||
this.state = state; | ||
} | ||
|
||
public String getState() { | ||
return state; | ||
} | ||
|
||
public void setState(String state) { | ||
this.state = state; | ||
} | ||
|
||
} |
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,31 @@ | ||
package com.mxy.design.memento; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* 订单状态 | ||
*/ | ||
public class OrderState { | ||
|
||
/** | ||
* 利用LIFO 后进先出 | ||
*/ | ||
private Stack<String> stateList = new Stack<>(); | ||
|
||
public Stack<String> getStateList() { | ||
return stateList; | ||
} | ||
|
||
public void setStateList(Stack<String> stateList) { | ||
this.stateList = stateList; | ||
} | ||
|
||
/** | ||
* 存储当前添加的状态 | ||
* @param memento | ||
*/ | ||
public void restoreMemento(Memento memento){ | ||
this.stateList.add(memento.getState()); | ||
} | ||
|
||
} |
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,17 @@ | ||
package com.mxy.design.memento; | ||
|
||
/** | ||
* 订单流程处理 | ||
*/ | ||
public class OrderStateProcess { | ||
|
||
private Memento memento; | ||
|
||
public OrderStateProcess(Memento memento) { | ||
this.memento = memento; | ||
} | ||
|
||
public Memento rollbackLastState() { | ||
return memento; | ||
} | ||
} |
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,60 @@ | ||
package com.mxy.design.memento; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* 备忘录模式:备忘录模式可以使系统恢复到某一历史时期的状态,又称标记模式 | ||
* 场景:模拟下单流程,出现异常回滚到上个状态。 | ||
*/ | ||
public class MementoTest { | ||
|
||
@Test | ||
public void logicTest() { | ||
|
||
Memento memento1 = new Memento("待下单"); | ||
OrderState state = new OrderState(); | ||
state.restoreMemento(memento1); | ||
//赋值当前订单状态 | ||
OrderStateProcess orderStateProcess = new OrderStateProcess(memento1); | ||
|
||
System.out.println("正在生成订单....."); | ||
if (rollbackState(orderStateProcess)){ | ||
return; | ||
} | ||
|
||
Memento memento2 = new Memento("生成订单"); | ||
state.restoreMemento(memento2); | ||
|
||
System.out.println("已生成订单准备支付"); | ||
|
||
orderStateProcess = new OrderStateProcess(memento2); | ||
Memento memento3 = new Memento("待支付"); | ||
state.restoreMemento(memento3); | ||
|
||
|
||
orderStateProcess = new OrderStateProcess(memento3); | ||
|
||
System.out.println("正在支付....."); | ||
if (rollbackState(orderStateProcess)){ | ||
return; | ||
} | ||
|
||
Memento memento4 = new Memento("已支付"); | ||
System.out.println("支付成功:当前订单状态:"+memento4.getState()); | ||
state.restoreMemento(memento4); | ||
|
||
} | ||
|
||
private boolean rollbackState(OrderStateProcess orderStateProcess) { | ||
//模拟场景 如果异常问题则回滚 | ||
if((new Random().nextBoolean())) { | ||
orderStateProcess.rollbackLastState(); | ||
System.out.println("出现异常回滚到订单状态:"+orderStateProcess.rollbackLastState().getState()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |