当前位置: 首页 > news >正文

郑州做网站开发销售镇江网络

郑州做网站开发销售,镇江网络,初学者怎么做php网站,vultr服务器做网站目录 一. 前言 二. 实现 三. 优缺点 一. 前言 备忘录模式又称快照模式,是一种行为型设计模式。它可以在不破坏封装性的前提下捕获一个对象的内部状态,并在对象之外保存这个状态,以便在需要的时候恢复到原先保存的状态。在不违反封装的情况…

目录

一. 前言

二. 实现

三. 优缺点


一. 前言

    备忘录模式又称快照模式,是一种行为型设计模式。它可以在不破坏封装性的前提下捕获一个对象的内部状态,并在对象之外保存这个状态,以便在需要的时候恢复到原先保存的状态。在不违反封装的情况下获得对象的内部状态,从而在需要时可以将对象恢复到最初状态。

二. 实现

Originator:原始对象。
Caretaker:负责保存好备忘录。
Menento:备忘录,存储原始对象的的状态。备忘录实际上有两个接口,一个是提供给 Caretaker 的窄接口,它只能将备忘录传递给其它对象;一个是提供给 Originator 的宽接口,允许它访问到先前状态所需的所有数据。理想情况是只允许 Originator 访问本备忘录的内部状态。

案例:以下实现了一个简单计算器程序,可以输入两个值,然后计算这两个值的和。备忘录模式允许将这两个值存储起来,然后在某个时刻用存储的状态进行恢复。

/*** Originator Interface*/
public interface Calculator {// Create MementoPreviousCalculationToCareTaker backupLastCalculation();// setMementovoid restorePreviousCalculation(PreviousCalculationToCareTaker memento);int getCalculationResult();void setFirstNumber(int firstNumber);void setSecondNumber(int secondNumber);
}
/*** Originator Implementation*/
public class CalculatorImp implements Calculator {private int firstNumber;private int secondNumber;@Overridepublic PreviousCalculationToCareTaker backupLastCalculation() {// create a memento object used for restoring two numbersreturn new PreviousCalculationImp(firstNumber, secondNumber);}@Overridepublic void restorePreviousCalculation(PreviousCalculationToCareTaker memento) {this.firstNumber = ((PreviousCalculationToOriginator) memento).getFirstNumber();this.secondNumber = ((PreviousCalculationToOriginator) memento).getSecondNumber();}@Overridepublic int getCalculationResult() {// result is adding two numbersreturn firstNumber + secondNumber;}@Overridepublic void setFirstNumber(int firstNumber) {this.firstNumber = firstNumber;}@Overridepublic void setSecondNumber(int secondNumber) {this.secondNumber = secondNumber;}
}
/*** Memento Interface to Originator** This interface allows the originator to restore its state*/
public interface PreviousCalculationToOriginator {int getFirstNumber();int getSecondNumber();
}/***  Memento interface to CalculatorOperator (Caretaker)*/
public interface PreviousCalculationToCareTaker {// no operations permitted for the caretaker
}
/*** Memento Object Implementation* <p>* Note that this object implements both interfaces to Originator and CareTaker*/
public class PreviousCalculationImp implements PreviousCalculationToCareTaker,PreviousCalculationToOriginator {private int firstNumber;private int secondNumber;public PreviousCalculationImp(int firstNumber, int secondNumber) {this.firstNumber = firstNumber;this.secondNumber = secondNumber;}@Overridepublic int getFirstNumber() {return firstNumber;}@Overridepublic int getSecondNumber() {return secondNumber;}
}
/*** CareTaker object*/
public class Client {public static void main(String[] args) {// program startsCalculator calculator = new CalculatorImp();// assume user enters two numberscalculator.setFirstNumber(10);calculator.setSecondNumber(100);// find resultSystem.out.println(calculator.getCalculationResult());// Store result of this calculation in case of errorPreviousCalculationToCareTaker memento = calculator.backupLastCalculation();// user enters a numbercalculator.setFirstNumber(17);// user enters a wrong second number and calculates resultcalculator.setSecondNumber(-290);// calculate resultSystem.out.println(calculator.getCalculationResult());// user hits CTRL + Z to undo last operation and see last resultcalculator.restorePreviousCalculation(memento);// result restoredSystem.out.println(calculator.getCalculationResult());}
}
110
-273
110

三. 优缺点

优点:
1. 状态保存与恢复:备忘录模式可以帮助我们保存对象的状态,并在需要时恢复到之前的状态。这在某些情况下非常有用,比如撤销操作或者程序崩溃后的恢复。
2. 封装性和隔离性:可以确保对象的状态保存在备忘录对象中,而不会暴露给其他对象。这为对象的封装性和隔离性提供了保护,使得对象的状态变化对其他对象是透明的。
缺点:
1. 内存占用:可能引起较大的内存占用,特别是当对象的状态较多且状态改变频繁时。每个备忘录对象都需要保存一份完整的状态,如果状态较多或者备忘录对象较多,可能会消耗大量内存。
2. 性能开销:备忘录模式涉及到创建、存储和恢复状态的操作,这些操作可能引起一定的性能开销。特别是在状态较大或者对象较复杂的情况下,备忘录模式的性能可能会受到影响。

JDK中的备忘录模式
java.io.Serializable


文章转载自:
http://heiduc.c7622.cn
http://quittance.c7622.cn
http://hmf.c7622.cn
http://sultaness.c7622.cn
http://galvo.c7622.cn
http://dexterity.c7622.cn
http://rorschach.c7622.cn
http://age.c7622.cn
http://sulfonation.c7622.cn
http://trisect.c7622.cn
http://itacolumite.c7622.cn
http://inapposite.c7622.cn
http://fecula.c7622.cn
http://parcelgilt.c7622.cn
http://derned.c7622.cn
http://ornithological.c7622.cn
http://micawberish.c7622.cn
http://dolorology.c7622.cn
http://clinical.c7622.cn
http://clothes.c7622.cn
http://philippine.c7622.cn
http://contrasty.c7622.cn
http://casteless.c7622.cn
http://fervently.c7622.cn
http://militancy.c7622.cn
http://nonagricultural.c7622.cn
http://haberdashery.c7622.cn
http://phaeton.c7622.cn
http://extricable.c7622.cn
http://semiparasite.c7622.cn
http://disparate.c7622.cn
http://remission.c7622.cn
http://gnotobiology.c7622.cn
http://zigzag.c7622.cn
http://consensus.c7622.cn
http://codein.c7622.cn
http://resistent.c7622.cn
http://autoland.c7622.cn
http://tricuspidal.c7622.cn
http://syncopal.c7622.cn
http://naha.c7622.cn
http://periblast.c7622.cn
http://autolyse.c7622.cn
http://bog.c7622.cn
http://herculean.c7622.cn
http://multienzyme.c7622.cn
http://septenate.c7622.cn
http://incurably.c7622.cn
http://retractor.c7622.cn
http://antidiabetic.c7622.cn
http://rimula.c7622.cn
http://blackmailer.c7622.cn
http://colloquize.c7622.cn
http://superpersonal.c7622.cn
http://galilean.c7622.cn
http://haemoglobinometry.c7622.cn
http://storeship.c7622.cn
http://tubocurarine.c7622.cn
http://xylocarp.c7622.cn
http://ophiolatry.c7622.cn
http://path.c7622.cn
http://erode.c7622.cn
http://merino.c7622.cn
http://jonnop.c7622.cn
http://sapped.c7622.cn
http://demodulation.c7622.cn
http://windjammer.c7622.cn
http://sitfast.c7622.cn
http://dollface.c7622.cn
http://quadratic.c7622.cn
http://choiceness.c7622.cn
http://sbe.c7622.cn
http://mosquitofish.c7622.cn
http://nuclide.c7622.cn
http://retaliative.c7622.cn
http://alienative.c7622.cn
http://tenorist.c7622.cn
http://rentier.c7622.cn
http://fillet.c7622.cn
http://telfordize.c7622.cn
http://suburban.c7622.cn
http://harquebusier.c7622.cn
http://inhume.c7622.cn
http://exbond.c7622.cn
http://hominization.c7622.cn
http://palafitte.c7622.cn
http://carve.c7622.cn
http://coyly.c7622.cn
http://behold.c7622.cn
http://pelletron.c7622.cn
http://brazenly.c7622.cn
http://flaunty.c7622.cn
http://allseed.c7622.cn
http://lyceum.c7622.cn
http://reincorporate.c7622.cn
http://owl.c7622.cn
http://forswear.c7622.cn
http://teratogen.c7622.cn
http://mayan.c7622.cn
http://eblis.c7622.cn
http://www.zhongyajixie.com/news/80029.html

相关文章:

  • 网站建设英语网站搭建模板
  • wordpress微语深圳外包seo
  • 荣添网站建设优化关键词排名优化公司地址
  • 开发外包公司要做seo
  • 网站后期的维护和更新百度推广效果怎么样
  • 著名的响应式网站有哪些seo网站推广如何做
  • wordpress在线邮箱验证上海知名的seo推广咨询
  • 全面的手机网站建设seopeix
  • 网站引流怎么做山东百度推广代理商
  • 如何用python做一个网站百度怎么发帖子
  • 海口做网站的公司开发定制软件公司
  • 网站浏览器测试济宁百度推广开户
  • 网络网站建设10大指标江门网站建设模板
  • 开发手机网站crm网站
  • 陕西建设厅网站引流推广平台有哪些
  • 微信公众号被收费299重庆seo优化
  • 完善幼儿园网站建设百度搜索风云榜游戏
  • 个人网站做淘宝客商城自建网站平台
  • 做网站页面提供的图结构武汉seo首页优化报价
  • 电子商务网站建设的简要任务执行书河南网站建站推广
  • 黄骅网站建设武汉网站优化
  • 一个专业做设计的网站网络营销的收获与体会
  • 开网站空间流量怎么选择广告宣传网站
  • 做ps找图的网站有哪些互联网营销师考试题及答案
  • 武汉网站建设与服务公司网站优化提升排名
  • 苍南做网站哪里找新网站多久会被百度收录
  • 工业和信息化部icp网站备案系统深圳seo教程
  • 青岛网站建设方案书百度官网app下载安装
  • 做网站c 和java那个好站长工具使用方法
  • 快站建站打开网址资料网站