欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > >内容正文

asp.net

设计模式-凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 14 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 设计模式-原型模式(prototype) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
  • specify the kinds of objects to create using a prototypical instance,and create new objects bycopying this prototype.(用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。)简单说明就是不通过new关键字产生对象,而是通过对象复制来实现就叫做原型模式。

原型模式的核心就是一个clone()方法,通过该方法进行对象拷贝,java底层提供了一个cloneable接口来标示这个对象是可拷贝的,这个借口只是一个标记作用,在jvm中有这个标记的对象才能被拷贝。

原型模式注意事项

1.构造函数不会被执行。我们举例说明为什么不会执行构造函数。

/*** @author shuliangzhao* @title: car* @projectname design-parent* @description: todo* @date 2019/5/31 22:57*/ public class car implements cloneable{public car() {system.out.println("我是一辆跑车...");}@overridepublic car clone() {car car = null;try{car = (car)super.clone();}catch (exception e) {e.printstacktrace();}return car;} }

客户端

/*** @author shuliangzhao* @title: carclient* @projectname design-parent* @description: todo* @date 2019/5/31 22:59*/ public class carclient {public static void main(string[] args) {car car = new car();car clone = car.clone();} }

运行结果

 

 

从运行结果我们可以看出只打印一句话 说明构造函数没执行。object类的clone方法的原理是从内存中(具体地说就是堆内存)以二进制流的方式进行拷贝,重新分配一个内存块,那构造函数没有被执行也是非常正常的了。

浅拷贝和深拷贝

浅拷贝例子

/*** 浅拷贝* @author shuliangzhao* @title: thing* @projectname design-parent* @description: todo* @date 2019/5/31 22:26*/ public class shallowthing implements cloneable {private list list = new arraylist();@overridepublic shallowthing clone() {shallowthing thing = null;try {thing = (shallowthing) super.clone();}catch (exception e) {e.printstacktrace();}return thing;}public void setlist(string s) {this.list.add(s);}public list getlist() {return this.list;}}

客户端client

/*** @author shuliangzhao* @title: client* @projectname design-parent* @description: todo* @date 2019/5/31 22:30*/ public class client {public static void main(string[] args) {//浅拷贝shallowthing thing = new shallowthing();thing.setlist("张三");shallowthing clone = thing.clone();clone.setlist("李四");system.out.println(thing.getlist());//深拷贝/* deepthing deepthing = new deepthing();deepthing.setlist("王五");deepthing clone1 = deepthing.clone();clone1.setlist("张柳");system.out.println(deepthing.getlist());system.out.println(clone1.getlist());*/} }

执行结果

 

 

 

执行结果是两个,因为java做了一个偷懒的动作,clone方法只拷贝对象,对象内的数组和引用对象等都不拷贝,这就是浅拷贝。两个对象共享一个变量是一种非常不安全的方式。但是string字符串比较特殊,通过字符串池(stringpool)在需要的时候才在内存中创建新的字符串。所以使用string时候可以当做基本类型就可以。

深拷贝例子

/*** @author shuliangzhao* @title: deepthing* @projectname design-parent* @description: todo* @date 2019/5/31 22:32*/ public class deepthing implements cloneable{private arraylist list = new arraylist();@overridepublic deepthing clone() {deepthing thing = null;try {thing = (deepthing) super.clone();this.list = (arraylist)this.list.clone();}catch (exception e) {e.printstacktrace();}return thing;}public void setlist(string s) {this.list.add(s);}public list getlist() {return this.list;}}

客户端client

/*** @author shuliangzhao* @title: client* @projectname design-parent* @description: todo* @date 2019/5/31 22:30*/ public class client {public static void main(string[] args) {//浅拷贝/*shallowthing thing = new shallowthing();thing.setlist("张三");shallowthing clone = thing.clone();clone.setlist("李四");system.out.println(thing.getlist());*///深拷贝deepthing deepthing = new deepthing();deepthing.setlist("王五");deepthing clone1 = deepthing.clone();clone1.setlist("张柳");system.out.println(deepthing.getlist());system.out.println(clone1.getlist());} }

执行结果

 

 

从上图可以看出第一个执行结果只有一个值。这就是深拷贝。

原型模式优点

1.性能好,因为是二进制拷贝,比new一个对象性能好。
2.不依赖构造函数

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的设计模式-原型模式(prototype)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图