设计模式:原型模式
来源:互联网 发布:in 软件 编辑:程序博客网 时间:2024/06/10 18:30
原型模式:
Prototype模式是一种对象创建型模式,它采取复制原型对象的方法来创建对象实例。使用Prototype模式创建的实例,具有与原型一样的数据。
特点:
1.由原型对象自身创建目标对象。也就是说,对象创建这一动作发自原型对象本身。
2.目标对象是原型对象的一个克隆。也就是说,通过Prototype模式创建的对象,不仅仅与原型对象具有相同的结构,还与原型对象具有相同的值。
3.根据对象克隆深度层次的不同,有浅度克隆与深度克隆。
应用场景:
在创建对象的时候,我们不只是希望被创建的对象继承其基类的基本结构,还希望继承原型对象的数据。
希望对目标对象的修改不影响既有的原型对象(深度克隆的时候可以完全互不影响)。
隐藏克隆操作的细节。很多时候,对对象本身的克隆需要设计到类本身的数据细节。
浅度克隆:
/**
* 人
* @author guosheng
*
*/
public class Person implements Cloneable{
private int age;//年龄
private String name;//姓名
public Person(int age,String name){
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 克隆方法
*/
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
System.out.println(this.name+":"+this.age);
return "";
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Person pone = new Person(18,"张三");
Person ptwo = (Person)pone.clone();
pone.toString();
ptwo.toString();
}
}
结果:
张三:18
张三:18
深度克隆:
import java.util.ArrayList;
import java.util.List;
/**
* 人
* @author guosheng
*
*/
public class Person implements Cloneable{
private int age;//年龄
private String name;//姓名
private List<String> friends;//朋友姓名
public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public Person(int age,String name){
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 克隆方法
*/
protected Object clone() throws CloneNotSupportedException {
Person pclone = (Person)super.clone();
List<String> list = new ArrayList<String>();
for (String str : pclone.getFriends()) {
list.add(str);
}
pclone.setFriends(list);
return pclone;
}
@Override
public String toString() {
System.out.println(this.name+":"+this.age+":"+this.friends.toString());
return "";
}
}
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException{
Person pone = new Person(18,"张三");
List<String> list = new ArrayList<String>();
list.add("西门庆");
list.add("武大郎");
list.add("武松");
list.add("潘安");
pone.setFriends(list);
Person ptwo = (Person)pone.clone();
ptwo.getFriends().add("周星驰");
pone.toString();
ptwo.toString();
}
}
//深浅即值传递和引用传递的区别
- 设计模式:原型模式
- 设计模式------原型模式
- 设计模式 原型模式
- 设计模式-【原型模式】
- 设计模式-原型模式
- 设计模式-原型模式
- 设计模式-原型模式
- 设计模式-原型模式
- 设计模式- 原型模式
- 设计模式:原型模式
- 设计模式 - 原型模式
- 设计模式-原型模式
- 【设计模式】原型模式
- 设计模式-原型模式
- 设计模式-原型模式
- 【设计模式】原型模式
- 设计模式----原型模式
- 设计模式------原型模式
- okhttp二次封装+单例模式+拦截器
- java中根据手机号获取手机号归属地
- 删除和添加索引,为什么要先加后删
- QWidget 设置背景色
- 【上海皿鎏软件股份有限公司】满足比例的无重复的9位数字
- 设计模式:原型模式
- 解决vue-cli npm run build之后vendor.js文件过大的问题
- 唯快不破:TCP/IP详解--滑动窗口与内核缓冲区之间的关系
- 浏览器提示正在下载代理脚本问题解决
- 【leetcode】最长连续序列(unordered_map的使用)
- 散列(hash)
- DataNode的流式接口
- 线程的生命周期
- crontab命令参数详解