


1. Definition
Capture the internal state of an object and save this state outside the object without destroying encapsulation. This allows you to later restore the object to its original saved state.
2. Reason for use
Want to restore the original state of the object at a certain time.
3. Examples of applicable situations
There are many applications of memo mode, but we have seen them before, but did not think carefully about the use of memo mode. Here are a few examples:
eg1. The use of memo in jsp+javabean:
When adding an account in a system, you need to fill in the user name, password, contact number, address and other information in the form. If some fields are not filled in or filled in incorrectly, when the user clicks " When clicking the "Submit" button, the options entered by the user need to be saved on the new page and the wrong options will be prompted. This is achieved by using the scope="request" or scope="session" characteristics of JavaBean, that is, using the memo mode.
eg2. When repairing the car's brakes. First remove the baffles on both sides to expose the left and right brake pads. Only one piece can be removed, then the other piece serves as a reminder of how the brakes are installed. After the repair of this piece is completed, the other piece can be removed. When the second piece comes off, the first piece becomes a memo.
eg3. It is said that there is no regret medicine to buy in life. We are all paying the price for what we have done, but in the soft world there is "regret medicine". After I change some state of something, as long as we have done it before After saving a certain state of the thing, we can restore the state of the thing through the memo mode. In fact, this is not a "moonlight treasure box" that can turn back time. It is always "magical".
4. Class diagram structure and description
(1) The class diagram is as follows:
(i ) Memento: Memento role, mainly responsible for the following tasks:
Store the internal state of the initiator object;
can protect its content from being read by any object other than the initiator (Originator) object.
(ii) Originator: The initiator role mainly completes the following tasks:
Create a memo object containing the current internal state;
Use the memo object to store its internal state.
(iii) Caretaker: Responsible person role, completes the work as follows:
is responsible for saving the memo object;
does not save the contents of the memo object.
/** * 数据对象 */ public class DataState { private String action; public void setAction(String action) { this.action = action; } public String getAction() { return action; }
/** * 一个保存另外一个对象内部状态拷贝 的对象.这样以后就可以将该对象恢复到原先保存的状态. */ import java.io.File; import java.io.Serializable; public class Memento implements Serializable { /*private int number; private File file = null; public Memento(Originator o) { this.number = o.getNumber(); this.file = o.getFile(); } public int getNumber() { return this.number; } public void setNumber(int number) { this.number = number; } public File getFile() { return this.file; } public void setFile(File file) { this.file = file; } */ private DataState state; public Memento(Originator o) { this.state = o.getState(); } public DataState getState() { return state; } public void setState(DataState state) { this.state = state; } }
public class Originator { /* private int number; private File file = null; public Originator() { } // 创建一个Memento,将自身作为参数传入 public Memento getMemento() { return new Memento(this); } // 从Memento中取出保存的数据,恢复为原始状态 public void setMemento(Memento m) { number = m.getNumber(); file = m.getFile(); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public File getFile() { return file; } public void setFile(File file) { this.file = file; }*/ private DataState state; public Originator() { } public Originator(DataState state) { this.state = state; } // 创建一个Memento,将自身作为参数传入 public Memento getMemento() { return new Memento(this); } // 从Memento中取出保存的数据,恢复为原始状态 public void setMemento(Memento m) { /* * getMemento() 创建的对象,保存在某个容器里, * 当需要恢复时,将其传入当前方法, 再使用getState(),得出 */ this.state = m.getState(); } public DataState getState() { return state; } public void setState(DataState state) { this.state = state; } }
/* * Originator用于 加载数据, 建立Memento对象,及通过Memento恢复原始数据 */ public class Test { public static void main(String[] args) { // Originator originator = new Originator(); // originator.setNumber(8); // // Memento memento = originator.getMemento(); // System.out.println(memento.getNumber()); DataState state = new DataState(); state.setAction("copy a character"); Originator originator = new Originator(); System.out.println("创建原始数据"); originator.setState(state); System.out.println("创建备忘录对象, 保存原始数据状态"); Memento memento = originator.getMemento(); System.out.println("创建了一个新数据"); originator.setState(new DataState()); System.out.println("创建新数据后:" + originator.getState().getAction()); /* * memento 需要保存在某地,需要时取出,以恢复它内部所保存的数据 */ System.out.println("创建新数据后,恢复原数据"); originator.setMemento(memento); System.out.println(originator.getState().getAction()); } }Print:
创建原始数据 创建备忘录对象, 保存原始数据状态 创建了一个新数据 创建新数据后:null 创建新数据后,恢复原数据 copy a characterMore details For articles related to the memo pattern and its implementation in Java design pattern programming, please pay attention to the PHP Chinese website!

JVM handles operating system API differences through JavaNativeInterface (JNI) and Java standard library: 1. JNI allows Java code to call local code and directly interact with the operating system API. 2. The Java standard library provides a unified API, which is internally mapped to different operating system APIs to ensure that the code runs across platforms.

modularitydoesnotdirectlyaffectJava'splatformindependence.Java'splatformindependenceismaintainedbytheJVM,butmodularityinfluencesapplicationstructureandmanagement,indirectlyimpactingplatformindependence.1)Deploymentanddistributionbecomemoreefficientwi

BytecodeinJavaistheintermediaterepresentationthatenablesplatformindependence.1)Javacodeiscompiledintobytecodestoredin.classfiles.2)TheJVMinterpretsorcompilesthisbytecodeintomachinecodeatruntime,allowingthesamebytecodetorunonanydevicewithaJVM,thusfulf

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),whichexecutesbytecodeonanydevicewithaJVM.1)Javacodeiscompiledintobytecode.2)TheJVMinterpretsandexecutesthisbytecodeintomachine-specificinstructions,allowingthesamecodetorunondifferentp

Platform independence in JavaGUI development faces challenges, but can be dealt with by using Swing, JavaFX, unifying appearance, performance optimization, third-party libraries and cross-platform testing. JavaGUI development relies on AWT and Swing, which aims to provide cross-platform consistency, but the actual effect varies from operating system to operating system. Solutions include: 1) using Swing and JavaFX as GUI toolkits; 2) Unify the appearance through UIManager.setLookAndFeel(); 3) Optimize performance to suit different platforms; 4) using third-party libraries such as ApachePivot or SWT; 5) conduct cross-platform testing to ensure consistency.

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
