Memento(备忘机制)

写道
package com.ijo.patterns;

public class Infomation {
private String emial;

private String telphone;

public Infomation() {
}

public Infomation(String emial, String telphone) {
this.emial = emial;
this.telphone = telphone;
}

public String getEmial() {
return emial;
}

public void setEmial(String emial) {
this.emial = emial;
}

public String getTelphone() {
return telphone;
}

public void setTelphone(String telphone) {
this.telphone = telphone;
}

public String toString() {
return "\temial\t" + emial + "\ttelphone\t" + telphone;
}
}

 

写道
package com.ijo.patterns;

public class Name {
private String fristName;
private String lastName;

public Name() {
super();
}

public Name(String fristName, String lastName) {
super();
this.fristName = fristName;
this.lastName = lastName;
}

public String getFristName() {
return fristName;
}

public void setFristName(String fristName) {
this.fristName = fristName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}


}

 

写道
package com.ijo.patterns;

import java.util.List;

public class User {
private int age;

private String idCard;

private Name name;

private List<Infomation> informations;

public User() {
}

public User(int age, String idCard, Name name, List<Infomation> informations) {
super();
this.age = age;
this.idCard = idCard;
this.name = name;
this.informations = informations;
}

public User(User user) {
this.age = user.getAge();
this.idCard = user.getIdCard();
this.name = user.getName();
this.informations = user.getInformations();
}

public List<Infomation> getInformations() {
return informations;
}

public void setInformations(List<Infomation> informations) {
this.informations = informations;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getIdCard() {
return idCard;
}

public void setIdCard(String idCard) {
this.idCard = idCard;
}

public Name getName() {
return name;
}

public void setName(Name name) {
this.name = name;
}

public String toString() {
String str = "aget\t" + age + "\tIdcard\t" + idCard + "\tfirst+\t"
+ name.getFristName() + "\tlastname+\t" + name.getLastName();
for (Infomation infomation : informations) {
str = str + infomation.toString();
}
return str;
}
}

  

写道
package com.ijo.patterns;

public class FactoryUser {
public static User getUser(User user){
return new User(user);
}
}

  

写道
package com.ijo.patterns;

import java.util.ArrayList;
import java.util.List;

public class Demo {
public static void main(String[] args) {
List<Infomation> list = new ArrayList<Infomation>();
list.add(new Infomation("[email protected]", "100"));
list.add(new Infomation("ming@hotmail", "120"));
User user = new User(20, "sichuanidcard", new Name("zhang", "san"),
list);
System.out.println(user.toString());
User userCopy = FactoryUser.getUser(user);
System.out.println(userCopy.toString());
System.out.println();
user.setName(new Name("wang", "wu"));
list = user.getInformations();
Infomation infomation = list.get(0);
infomation.setEmial("[email protected]");
infomation.setTelphone("888");
System.out.println(user.toString());
System.out.println(userCopy.toString());
System.out.println();
list = new ArrayList<Infomation>();
list.add(new Infomation("[email protected]", "123"));
list.add(new Infomation("[email protected]", "456"));
user.setInformations(list);
System.out.println(user.toString());
System.out.println(userCopy.toString());

}
}

 

结果:

写道
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial [email protected] telphone 100 emial ming@hotmail telphone 120
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial [email protected] telphone 100 emial ming@hotmail telphone 120

aget 20 Idcard sichuanidcard first+ wang lastname+ wu emial [email protected] telphone 888 emial ming@hotmail telphone 120
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial [email protected] telphone 888 emial ming@hotmail telphone 120

aget 20 Idcard sichuanidcard first+ wang lastname+ wu emial [email protected] telphone 123 emial [email protected] telphone 456
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial [email protected] telphone 888 emial ming@hotmail telphone 120

由结果看来,主要是是一个浅拷贝,不是深拷贝。 这种设计模式很容易出错的了

你可能感兴趣的:(设计模式,Yahoo)