当前位置: 首页 > news >正文

网站开发实用技术网上营销新观察网

网站开发实用技术,网上营销新观察网,阿里云部署网站教程,收费的电影网站怎么做1 基本概念 1.1 大佬文章 设计模式是什么鬼(原型) 详解设计模式:原型模式-腾讯云开发者社区-腾讯云 1.2 知识汇总 (1)原型模式:先 new 一个实例,该实例符合需求,之后再根据这个实…

1 基本概念

1.1 大佬文章

设计模式是什么鬼(原型)

详解设计模式:原型模式-腾讯云开发者社区-腾讯云


1.2 知识汇总

(1)原型模式:先 new 一个实例,该实例符合需求,之后再根据这个实例为原型,重复构建新的对象;
(2)所属类型:创建型模式
(3)作用:重复创建对象;
(4)优点:可以重复获得对象的同时保持较高的性能;

1.3 基本构成

(1)抽象原型类:抽象原型类是定义具有克隆自己的方法接口,是所有具体原型类的公共父类,可以是抽象类,也可以是接口;在 Java 中 Cloneable 接口可以看作是抽象原型类;
(2)具体原型类:根据这个类可以获得一个原型对象,并且这个类中需要重现 clone 方法,可以根据这个 clone 方法复制对象;
(3)访问类:使用具体原型类中的 clone 方法,可以不断重复的复制对象;

2 Java 中的克隆

根据现有的对象复制一个新的对象的操作就是克隆,Java 中克隆分为浅克隆和深克隆(我个人更喜欢说浅拷贝和深拷贝);浅拷贝和深拷贝的区别主要在重写 clone 方法上有所区别;但是,虽然之前有了解过拷贝的相关问题,但是发今天发现其实这里面还是有一些细节需要注意的。

2.1 拷贝的必要条件

(1)实现 Cloneable 接口:这个接口其实是一个标记接口,想要使用 clone 方法,就必须先实现这个接口,否则该类的 clone 方法是不可用的。

(2)重写 clone 方法:仅仅实现 Cloneable 接口还不能达到要求,还需要重写这个方法,否则依然不能使用 clone 方法;

2.2 浅拷贝

浅拷贝是将某个对象的所有成员属性都赋值给 clone 得到的对象;
(1)简单数据类型,如 int 等等的数据类型,直接将值拷贝过去;
(2)非简单数据类型,如引用类型的数据,是将引用的地址赋值给拷贝得到的对象,也就是说并没有新建一个成员对象,而是简单的将引用复制一遍;

package se.wangs.clonedemo.pojo;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 9:07* @description 演示浅拷贝*/
public class Teacher {private String name;private String idNo;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getIdNo() {return idNo;}public void setIdNo(String idNo) {this.idNo = idNo;}
}
package se.wangs.clonedemo.pojo;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 9:08* @description 演示clone*/
public class Student implements Cloneable{private String idNo;private String name;private Teacher teacher;public String getIdNo() {return idNo;}public void setIdNo(String idNo) {this.idNo = idNo;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}@Overridepublic Student clone() {try {Student clone = (Student) super.clone();// TODO: copy mutable state here, so the clone can't change the internals of the originalreturn clone;} catch (CloneNotSupportedException e) {throw new AssertionError();}}
}
package se.wangs.clonedemo;import se.wangs.clonedemo.pojo.Student;
import se.wangs.clonedemo.pojo.Teacher;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/4 16:37* @description 浅拷贝*/
public class ShallowClone {public static void main(String[] args) {Teacher teacher1 = new Teacher();teacher1.setIdNo("001");teacher1.setName("teacher_001");Student student1 = new Student();student1.setIdNo("101");student1.setName("student_101");student1.setTeacher(teacher1);Student cloneStudent = student1.clone();System.out.println("---------------------------");System.out.println(student1);System.out.println("idNo = " + student1.getIdNo());System.out.println("name = " + student1.getName());System.out.println("teacher = " + student1.getTeacher());System.out.println("---------------------------");System.out.println(cloneStudent);System.out.println("idNo = " + cloneStudent.getIdNo());System.out.println("name = " + cloneStudent.getName());System.out.println("teacher = " + cloneStudent.getTeacher());}
}

运行测试类得到的结果如下:

---------------------------
se.wangs.clonedemo.pojo.Student@41629346
idNo = 101
name = student_101
teacher = se.wangs.clonedemo.pojo.Teacher@214c265e
---------------------------
se.wangs.clonedemo.pojo.Student@448139f0
idNo = 101
name = student_101
teacher = se.wangs.clonedemo.pojo.Teacher@214c265e

说明:

  • student1:原对象
  • cloneStudent:浅拷贝得到的对象

可以看到,student 中有一个引用类型的成员变量 teacher,浅拷贝得到的 cloneStudent 中的 teacher 变量与 student1 指向的对象相同;

2.3 深拷贝

深拷贝就是会将引用类型的变量做进一步处理,clone 出的对象的引用数据类型的成员变量,会指向一个新的变量

package se.wangs.clonedemo.pojo;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 9:07* @description 演示浅拷贝*/
public class Teacher implements Cloneable{private String name;private String idNo;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getIdNo() {return idNo;}public void setIdNo(String idNo) {this.idNo = idNo;}@Overridepublic Teacher clone() {try {Teacher clone = (Teacher) super.clone();// TODO: copy mutable state here, so the clone can't change the internals of the originalreturn clone;} catch (CloneNotSupportedException e) {throw new AssertionError();}}
}
package se.wangs.clonedemo.pojo;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 9:08* @description 演示clone*/
public class Student implements Cloneable{private String idNo;private String name;private Teacher teacher;public String getIdNo() {return idNo;}public void setIdNo(String idNo) {this.idNo = idNo;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}@Overridepublic Student clone() {try {Student clone = (Student) super.clone();// 获得原来的 teacher 对象,并clone出一个新的teacher对象Teacher newTeacher = clone.getTeacher().clone();// 重新设置teacherclone.setTeacher(newTeacher);return clone;} catch (CloneNotSupportedException e) {throw new AssertionError();}}
}
package se.wangs.clonedemo;import se.wangs.clonedemo.pojo.Student;
import se.wangs.clonedemo.pojo.Teacher;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 10:09* @description 深拷贝*/
public class DeepClone {public static void main(String[] args) {Teacher teacher1 = new Teacher();teacher1.setIdNo("001");teacher1.setName("teacher_001");Student student1 = new Student();student1.setIdNo("101");student1.setName("student_101");student1.setTeacher(teacher1);Student cloneStudent = student1.clone();System.out.println("---------------------------");System.out.println(student1);System.out.println("idNo = " + student1.getIdNo());System.out.println("name = " + student1.getName());System.out.println("teacher = " + student1.getTeacher());System.out.println("---------------------------");System.out.println(cloneStudent);System.out.println("idNo = " + cloneStudent.getIdNo());System.out.println("name = " + cloneStudent.getName());System.out.println("teacher = " + cloneStudent.getTeacher());}
}

运行得到

---------------------------
se.wangs.clonedemo.pojo.Student@41629346
idNo = 101
name = student_101
teacher = se.wangs.clonedemo.pojo.Teacher@214c265e
---------------------------
se.wangs.clonedemo.pojo.Student@448139f0
idNo = 101
name = student_101
teacher = se.wangs.clonedemo.pojo.Teacher@7cca494b

可以看到,得到的 teacher 对象的地址变了;

2.4 总结

浅拷贝和深拷贝的主要区别就是对引用类型的变量的处理
(1)浅拷贝:引用类型的变量拷贝前后指向同一个对象;
(2)深拷贝:引用类型的变量拷贝后指向新的对象;
(3)核心:重写 clone 方法时对引用类型变量的处理不同;
参考资料:
Java深入理解深拷贝和浅拷贝区别_java深拷贝浅拷贝-CSDN博客


3 原型设计模式

3.1 设计模式体验

(1)抽象原型类:Cloneable 接口
(2)具体原型类:可以 new 原型实例,实现 Cloneable 接口,重写了 clone() 方法;

package se.wangs.prototype;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 9:07* @description*/
public class Teacher implements Cloneable{private String name;private String idNo;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getIdNo() {return idNo;}public void setIdNo(String idNo) {this.idNo = idNo;}@Overridepublic Teacher clone() {try {Teacher clone = (Teacher) super.clone();// TODO: copy mutable state here, so the clone can't change the internals of the originalreturn clone;} catch (CloneNotSupportedException e) {throw new AssertionError();}}
}
package se.wangs.prototype;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 9:08* @description 具体原型类*/
public class Student implements Cloneable{private String idNo;private String name;private Teacher teacher;public String getIdNo() {return idNo;}public void setIdNo(String idNo) {this.idNo = idNo;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}@Overridepublic Student clone() {try {Student clone = (Student) super.clone();// 获得原来的 teacher 对象,并clone出一个新的teacher对象Teacher newTeacher = clone.getTeacher().clone();// 重新设置teacherclone.setTeacher(newTeacher);return clone;} catch (CloneNotSupportedException e) {throw new AssertionError();}}
}

(3)访问类:实例化原型,通过 clone 复制原型对象;

package se.wangs.prototype;/*** -- coding: UTF-8 -- *** @author wangs* @date 2023/12/6 10:30* @description 访问类*/
public class Client {public static void main(String[] args) {Teacher teacher = new Teacher();teacher.setName("okay");teacher.setIdNo("0001");Student student = new Student();student.setIdNo("stu_001");student.setName("onesun");student.setTeacher(teacher);System.out.println("---------------------------");System.out.println(student);System.out.println("idNo = " + student.getIdNo());System.out.println("name = " + student.getName());System.out.println("teacher = " + student.getTeacher());for (int i = 0; i < 10; i++) {System.out.println("---------------------------");Student cloneStudent = student.clone();System.out.println(cloneStudent);System.out.println("idNo = " + cloneStudent.getIdNo());System.out.println("name = " + cloneStudent.getName());System.out.println("teacher = " + cloneStudent.getTeacher());}}
}

3.2 总结

(1)原型设计模式的思想就是拿到一个对象,将该对象当作一个标准,然后使用 clone 方法不断重复的去复制这个标准(原型对象),从而得到大量同类型的对象,在游戏等等场景中应用广泛。
(2)使用原型模式而不是 new 对象的优点是,节约 new 需要消耗的资源,提高系统的性能。
(3)原型模式的应用
Spring 框架对 bean 对象进行管理,而默认的 bean 对象是单例模式的,也可以使用 scope 属性指定为非单例模式,当 scope 属性为"socpe=prototype"时,就是非单例模式,其实这里使用的就是原型模式;

http://www.tj-hxxt.cn/news/83984.html

相关文章:

  • xp做网站优化王
  • 1个空间做两个网站2345网址导航 中国最
  • 北京市建筑网站trinseo公司
  • @wordpress沈阳百度seo关键词优化排名
  • 网站建站方案网络营销广告案例
  • 网络广告的特点海外seo是什么
  • 哈尔滨网站建设科技公司官网seo关键词排名系统
  • 湖南网站制作公司百度一下网页版浏览器百度
  • 徐州金网网站建设简述网络营销的特点
  • 衡阳电商网站建设百度竞价推广公司
  • 代理做网站seo优化好做吗
  • wordpress没有上级目录的写权限seo搜索引擎优化方式
  • 网站建设及宣传管理规定com网站域名注册
  • 手机网站开发语言选择汕头seo管理
  • 个人引擎网站什么做网站推广的工作内容
  • 北京定制公交app武汉官网优化公司
  • 网站根目录权限设置网站推广排名公司
  • 一流的龙岗网站设计企业网络推广平台
  • 陕西电商b2c网站建设公司网络公关公司收费
  • 有了域名后怎么做网站免费的拓客平台有哪些
  • 手机自己制作app软件搜索引擎优化的流程
  • 河南建设工程造价信息seo网络优化专员是什么意思
  • 做网站前端和平面配合怎么样推广自己的公司
  • 猛烈做瞹瞹视频澳洲网站google官方下载安装
  • 网站建设公司销售常州seo外包
  • WordPress站内跳转设置免费网络营销平台
  • 在电脑上怎么做网站百度公司推广电话
  • 微信怎么建小网站怎么把抖音关键词做上去
  • 网站建设公众号开今天国际新闻最新消息10条
  • metinfo怎么做网站cms建站