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("ming@yahoo.com.cn", "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("test@test.com");
infomation.setTelphone("888");
System.out.println(user.toString());
System.out.println(userCopy.toString());
System.out.println();
list = new ArrayList<Infomation>();
list.add(new Infomation("jiang@yahoo.com.cn", "123"));
list.add(new Infomation("jiang@163.com", "456"));
user.setInformations(list);
System.out.println(user.toString());
System.out.println(userCopy.toString());

}
}

 

结果:

写道
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial ming@yahoo.com.cn telphone 100 emial ming@hotmail telphone 120
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial ming@yahoo.com.cn telphone 100 emial ming@hotmail telphone 120

aget 20 Idcard sichuanidcard first+ wang lastname+ wu emial test@test.com telphone 888 emial ming@hotmail telphone 120
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial test@test.com telphone 888 emial ming@hotmail telphone 120

aget 20 Idcard sichuanidcard first+ wang lastname+ wu emial jiang@yahoo.com.cn telphone 123 emial jiang@163.com telphone 456
aget 20 Idcard sichuanidcard first+ zhang lastname+ san emial test@test.com telphone 888 emial ming@hotmail telphone 120

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值