Memorandum Design Model

Source: Internet
Author: User

What is the purpose of such a model? According to the online recruitment unit, it is a "good object-oriented programming ideology". Yes, when I first came into contact with the design model, I was surprised that the design model is the highest level of object-oriented, good, so far, I have made an exclamation point, high, and really high. It is really essential to improve my logic thinking and write code with high reusability and maintainability.

The memorandum mode, in the gof statement, captures the internal state of an object and saves the object outside of the object, in fact, in the Java field, there is one thing that reflection can be obtained regardless of private, but does it destroy encapsulation,

Public class caretaker {
Private memento mement;

Public memento getmement (){
Return mement;
}

Public void setmement (memento mement ){
This. mement = mement;
}
}

 

Public class memento {
Private string name;

Public memento (string name, string sex, int age ){
Super ();
This. Name = Name;
This. Sex = sex;
This. Age = age;
}

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public String getsex (){
Return sex;
}

Public void setsex (string sex ){
This. Sex = sex;
}

Public int getage (){
Return age;
}

Public void setage (INT age ){
This. Age = age;
}

Private string sex;
Private int age;
}

Public class person {
Private string name;
Private string sex;
Private int age;
Public Person (string name, string sex, int age ){
Super ();
This. Name = Name;
This. Sex = sex;
This. Age = age;
}
Public Person (){
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public String getsex (){
Return sex;
}
Public void setsex (string sex ){
This. Sex = sex;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Public void display (){
System. Out. println ("person [name =" + name + ", sex =" + sex + ", age =" + age + "]");
}
// Create a backup Based on the object-oriented design principles
Public memento creatememento (){
Return new memento (name, sex, age );
}
// Restore
Public void setmemento (memento mementto ){
This. Age = mementto. getage ();
This. Name = mementto. getname ();
This. Sex = mementto. getsex ();
}
}

Public class person {
Private string name;
Private string sex;
Private int age;
Public Person (string name, string sex, int age ){
Super ();
This. Name = Name;
This. Sex = sex;
This. Age = age;
}
Public Person (){
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public String getsex (){
Return sex;
}
Public void setsex (string sex ){
This. Sex = sex;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Public void display (){
System. Out. println ("person [name =" + name + ", sex =" + sex + ", age =" + age + "]");
}
// Create a backup Based on the object-oriented design principles
Public memento creatememento (){
Return new memento (name, sex, age );
}
// Restore
Public void setmemento (memento mementto ){
This. Age = mementto. getage ();
This. Name = mementto. getname ();
This. Sex = mementto. getsex ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.