行为型设计模式---备忘录模式和责任链模式

一、备忘录模式

       备忘录模式的使用场景为保存一个对象的某个状态,以便在适当的时候恢复对象。比如说操作系统的撤销,word文档的撤销以及IDE的撤销功能都可以使用备忘录模式。


       手记类:

	public class Article {
	    private String title;
	    private String content;
	    private String imgs;
	
	    public Article(String title, String content, String imgs) {
	        this.title = title;
	        this.content = content;
	        this.imgs = imgs;
	    }
	
	    public String getTitle() {
	        return title;
	    }
	
	    public void setTitle(String title) {
	        this.title = title;
	    }
	
	    public String getContent() {
	        return content;
	    }
	
	    public void setContent(String content) {
	        this.content = content;
	    }
	
	    public String getImgs() {
	        return imgs;
	    }
	
	    public void setImgs(String imgs) {
	        this.imgs = imgs;
	    }
	
	    public ArticleMemento saveToMemento() {
	        ArticleMemento articleMemento = new ArticleMemento(this.title, this.content, this.imgs);
	        return articleMemento;
	    }
	
	    public void undoFromMemento(ArticleMemento articleMemento) {
	        this.title = articleMemento.getTitle();
	        this.content = articleMemento.getContent();
	        this.imgs = articleMemento.getImgs();
	    }
	
	    @Override
	    public String toString() {
	        return "Article{" +
	                "title='" + title + '\'' +
	                ", content='" + content + '\'' +
	                ", imgs='" + imgs + '\'' +
	                '}';
	    }
	}

       手记存档类:

	public class ArticleMemento {
	    private String title;
	    private String content;
	    private String imgs;
	
	    public ArticleMemento(String title, String content, String imgs) {
	        this.title = title;
	        this.content = content;
	        this.imgs = imgs;
	    }
	
	    public String getTitle() {
	        return title;
	    }
	
	    public String getContent() {
	        return content;
	    }
	
	    public String getImgs() {
	        return imgs;
	    }
	
	    @Override
	    public String toString() {
	        return "ArticleMemento{" +
	                "title='" + title + '\'' +
	                ", content='" + content + '\'' +
	                ", imgs='" + imgs + '\'' +
	                '}';
	    }
	}

       手记管理类:

	public class ArticleMementoManager {
	    //存档内容
	    private final Stack<ArticleMemento> ARTICLE_MEMENTO_STACK = new Stack<>();
	
	    public ArticleMemento getMemento() {
	        return ARTICLE_MEMENTO_STACK.pop();
	    }
	
	    public void addMemento(ArticleMemento articleMemento) {
	        ARTICLE_MEMENTO_STACK.push(articleMemento);
	    }
	}

       测试类:

	public class MementoTest {
	    public static void main(String[] args) {
	        ArticleMementoManager articleMementoManager = new ArticleMementoManager();
	
	        Article article = new Article("如影随形的设计模式A", "手记内容A", "手记图片A");
	        ArticleMemento articleMemento = article.saveToMemento();
	
	        articleMementoManager.addMemento(articleMemento);
	        System.out.println("标题: " + article.getTitle() + "内容: " + article.getContent() + " 图片: " + article.getImgs() + " 暂存成功");
	        System.out.println("手记完整信息: " + article);
	
	        System.out.println("修改手记start");
	        article.setTitle("如影随形的设计模式B");
	        article.setContent("手记内容B");
	        article.setImgs("手记图片B");
	        System.out.println("修改手记end");
	        System.out.println("手记完整信息: " + article);
	        articleMemento = article.saveToMemento();
	        articleMementoManager.addMemento(articleMemento);
	
	        System.out.println("修改手记start");
	        article.setTitle("如影随形的设计模式C");
	        article.setContent("手记内容C");
	        article.setImgs("手记图片C");
	        System.out.println("修改手记end");
	        System.out.println("手记完整信息: " + article);
	        articleMemento = article.saveToMemento();
	        articleMementoManager.addMemento(articleMemento);
	
	        System.out.println("暂存回退start");
	        System.out.println("回退出栈一次");
	        articleMemento = articleMementoManager.getMemento();
	        article.undoFromMemento(articleMemento);
	        System.out.println("手记完整信息: " + article);
	        System.out.println("回退出栈二次");
	        articleMemento = articleMementoManager.getMemento();
	        article.undoFromMemento(articleMemento);
	        System.out.println("手记完整信息: " + article);
	        System.out.println("回退出栈三次");
	        articleMemento = articleMementoManager.getMemento();
	        article.undoFromMemento(articleMemento);
	        System.out.println("手记完整信息: " + article);
	        System.out.println("暂存回退end");
	    }
	}

       测试结果:

Alt


       此时的UML图如下:

Alt




二、责任链模式

       责任链模式可以为请求创建一个接受此次请求对象的链。在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。


       课程类:

	public class Course {
	    private String name;
	    private String article;
	    private String video;
	
	    public Course() {
	    }
	
	    public Course(String name, String article, String video) {
	        this.name = name;
	        this.article = article;
	        this.video = video;
	    }
	
	    public String getName() {
	        return name;
	    }
	
	    public void setName(String name) {
	        this.name = name;
	    }
	
	    public String getArticle() {
	        return article;
	    }
	
	    public void setArticle(String article) {
	        this.article = article;
	    }
	
	    public String getVideo() {
	        return video;
	    }
	
	    public void setVideo(String video) {
	        this.video = video;
	    }
	}

       抽象的处理类:

	public abstract class ApproveHandler {
	    protected ApproveHandler approveHandler;
	
	    public void setNextHandler(ApproveHandler approveHandler) {
	        this.approveHandler = approveHandler;
	    }
	
	    public abstract void deploy(Course course);
	}

       手记处理类(整条处理器链中的一环):

	public class ArticleApproveHandler extends ApproveHandler {
	    @Override
	    public void deploy(Course course) {
	        if (course != null && (!"".equals(course.getArticle()) && course.getArticle() != null)) {
	            System.out.println(course.getName() + "含有手记, 批准");
	            if (approveHandler != null) {
	                approveHandler.deploy(course);
	            }
	        } else {
	            System.out.println(course.getName() + "不含手记, 不批准, 流程结束");
	        }
	    }
	}

       视频处理类(整条处理器链中的一环):

	public class VideoApproveHandler extends ApproveHandler {
	    @Override
	    public void deploy(Course course) {
	        if (course != null && (!"".equals(course.getVideo()) && course.getVideo() != null)) {
	            System.out.println(course.getName() + "含有视频, 批准");
	            if (approveHandler != null) {
	                approveHandler.deploy(course);
	            }
	        } else {
	            System.out.println(course.getName() + "不含视频, 不批准, 流程结束");
	        }
	    }
	}

       测试类:

	public class ChainOfResponsibilityTest {
	    public static void main(String[] args) {
	        ApproveHandler articleHandler = new ArticleApproveHandler();
	        ApproveHandler videoHandler = new VideoApproveHandler();
	        Course course = new Course();
	        course.setName("Java设计模式精讲");
	        course.setArticle("Java设计模式精讲的手记");
	        course.setVideo("Java设计模式精讲的视频");
	
	        articleHandler.setNextHandler(videoHandler);
	        articleHandler.deploy(course);
	    }
	}

       测试结果:

Alt


       UML图如下,非常清晰:

Alt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值