Skip to content

Commit

Permalink
feat:上传备忘录模式例子
Browse files Browse the repository at this point in the history
  • Loading branch information
maxy19 committed Mar 17, 2019
1 parent b7f8ab0 commit d258e23
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* ENUM模式(最安全) 已完成
* 静态内部类模式 已完成

15.备忘录模式 - 整理中
15.备忘录模式 - 已完成

16.组合模式 - 整理中

Expand Down
19 changes: 19 additions & 0 deletions src/main/com/mxy/design/memento/Memento.java
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;
}

}
31 changes: 31 additions & 0 deletions src/main/com/mxy/design/memento/OrderState.java
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());
}

}
17 changes: 17 additions & 0 deletions src/main/com/mxy/design/memento/OrderStateProcess.java
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;
}
}
60 changes: 60 additions & 0 deletions src/test/com/mxy/design/memento/MementoTest.java
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;
}

}

0 comments on commit d258e23

Please sign in to comment.