您的位置:首页 > 编程语言 > Java开发

java设计模式_原型模式

2016-07-25 21:51 555 查看

一、什么是原型模式

Prototype模式是一种对象创建型模式,它采 取复制原型对象的方法来创建对象的实例。使用

Prototype模式创建的实例,具有与原型一样的 数据。

二、原型模式的特点

由原型对象自身创建目标对象。也就是说,对 象创建这一动作发自原型对象本身。

2.目标对象是原型对象的一个克隆。也就是说, 通过Prototype模式创建的对象,不仅仅与原型 对象具有相同的结构,还与原型对象具有相同的 值。

3.根据对象克隆深度层次的不同,有浅度克隆与 深度克隆。

三、原型模式应用场景

在创建对象的时候,我们不只是希望被创建的对象继承 其基类的基本结构,还希望继承原型对象的数据。

- 希望对目标对象的修改不影响既有的原型对象(深度克 隆的时候可以完全互不影响)。

- 隐藏克隆操作的细节。很多时候,对对象本身的克隆需 要涉及到类本身的数据细节。

//示例1:
public class Person implements Cloneable{

//姓名
private String name;
//年龄
private int age;
//性别
private String sex;

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

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

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@Override
protected Person clone() throws CloneNotSupportedException {
return (Person)super.clone();
}

@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}

}
public class MainClass {

public static void main(String[] args) throws Exception {
Person person1 = new Person();
person1.setName("lifengxin");
person1.setAge(30);
person1.setSex("男");

Person person2 = person1.clone();
person1.setName("haha");
person1.setAge(22);
person1.setSex("niu");

System.out.println(person1);
System.out.println(person2);

}

}

//Person [name=haha, age=22, sex=niu]
//Person [name=lifengxin, age=30, sex=男]

//示例2:
import java.util.ArrayList;
import java.util.List;
public class Person implements Cloneable{

//姓名
private String name;
//年龄
private int age;
//性别
private String sex;

//朋友
private List<String> friends;

public void setFriends(List<String> friends) {
this.friends = friends;
}

public List<String> getFriends() {
return friends;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

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

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@Override
protected Person clone() throws CloneNotSupportedException {
return (Person)super.clone();
}

@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex
+ ", friends=" + friends + "]";
}

}

public class MainClass {

public static void main(String[] args) throws Exception {
Person person1 = new Person();
List<String> friends = new ArrayList<>();
friends.add("James");
friends.add("Yao");

person1.setFriends(friends);
Person person2 = person1.clone();

friends.add("mike");
person1.setFriends(friends);

System.out.println(person1);
System.out.println(person2);

}

}

//Person [name=null, age=0, sex=null, friends=[James, Yao, mike]]
//Person [name=null, age=0, sex=null, friends=[James, Yao, mike]]
//*这里有一个问题,person1中的List 修改后会影响clone的person2对象中的List,这说明clone的不彻底.

//需修改Person类的clone方法

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

public class Person implements Cloneable{

//姓名
private String name;
//年龄
private int age;
//性别
private String sex;

//朋友
private List<String> friends;

public void setFriends(List<String> friends) {
this.friends = friends;
}

public List<String> getFriends() {
return friends;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

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

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

@Override
protected Person clone() throws CloneNotSupportedException {
Person person = (Person) super.clone();
List<String> friends = new ArrayList<String>();
for(String friend : this.friends)
{
friends.add(friend);
}
person.setFriends(friends);
return person;
}

@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex
+ ", friends=" + friends + "]";
}

}

public class MainClass {

public static void main(String[] args) throws Exception {
Person person1 = new Person();
List<String> friends = new ArrayList<>();
friends.add("James");
friends.add("Yao");

person1.setFriends(friends);
Person person2 = person1.clone();

friends.add("mike");
person1.setFriends(friends);

System.out.println(person1);
System.out.println(person2);

}

}

//Person [name=null, age=0, sex=null, friends=[James, Yao, mike]]
//Person [name=null, age=0, sex=null, friends=[James, Yao]]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java