备忘录模式
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.memento;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文章,字段不宜过多,否则导致过大内存占用
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2020/3/6 17:00
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Article {
|
||||
|
||||
private String title;
|
||||
|
||||
private String content;
|
||||
|
||||
private String imgs;
|
||||
|
||||
/**
|
||||
* 保存
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ArticleMemento saveToMemento() {
|
||||
ArticleMemento articleMemento = new ArticleMemento(this.title, this.content, this.imgs);
|
||||
return articleMemento;
|
||||
}
|
||||
|
||||
/**
|
||||
* 撤销
|
||||
*
|
||||
* @param articleMemento
|
||||
*/
|
||||
public void undoFromMemento(ArticleMemento articleMemento) {
|
||||
this.title = articleMemento.getTitle();
|
||||
this.content = articleMemento.getContent();
|
||||
this.imgs = articleMemento.getImgs();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.memento;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import org.springframework.binding.message.StateManageableMessageContext;
|
||||
|
||||
/**
|
||||
* 文章备忘录(快照)
|
||||
* 快照不需要set方法,只能通过构造器注入
|
||||
* <p>
|
||||
* Spring工作流中源码 {@link StateManageableMessageContext#createMessagesMemento()}
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2020/3/6 17:03
|
||||
*/
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
public class ArticleMemento {
|
||||
|
||||
private String title;
|
||||
|
||||
private String content;
|
||||
|
||||
private String imgs;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.memento;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* 保存一个对象的某个状态,以便在适当的时候恢复对象
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2020/3/6 17:04
|
||||
*/
|
||||
public class ArticleMementoManager {
|
||||
|
||||
/**
|
||||
* 使用栈特性实现动作撤销
|
||||
*/
|
||||
private final Stack<ArticleMemento> ARTICLE_MEMENTO_STACK = new Stack<ArticleMemento>();
|
||||
|
||||
public ArticleMemento getMemento() {
|
||||
ArticleMemento articleMemento = ARTICLE_MEMENTO_STACK.pop();
|
||||
return articleMemento;
|
||||
}
|
||||
|
||||
public void addMemento(ArticleMemento articleMemento) {
|
||||
ARTICLE_MEMENTO_STACK.push(articleMemento);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user