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

flask做的网站 网址外贸网站有哪些

flask做的网站 网址,外贸网站有哪些,ppt模板大全免费下载简洁,云台山旅游景区网站建设内容原型设计模式 原型设计模式(Prototype Pattern)是一种创建型设计模式,它允许通过复制已有对象来创建新对象,而无需直接依赖它们的具体类。这种模式通常用于需要频繁创建相似对象的场景,以避免昂贵的创建操作或初始化过…

在这里插入图片描述

原型设计模式

原型设计模式(Prototype Pattern)是一种创建型设计模式,它允许通过复制已有对象来创建新对象,而无需直接依赖它们的具体类。这种模式通常用于需要频繁创建相似对象的场景,以避免昂贵的创建操作或初始化过程。

概述

用一个已经创建的实例作为原型,通过复制该原型对象来创建一个和原型对象相同的新对象。

结构

原型模式包含如下角色:

  • 抽象原型类:规定了具体原型对象必须实现的的 clone() 方法。
  • 具体原型类:实现抽象原型类的 clone() 方法,它是可被复制的对象。
  • 访问类:使用具体原型类中的 clone() 方法来复制新的对象。

实现

原型模式的克隆分为浅克隆和深克隆。

(经典面试题)什么是深克隆和浅克隆
答:在计算机内存中,每个对象都有一个地址,这个地址指向对象在内存中存储的位置。当我们使用变量引用一个对象时,实际上是将该对象的地址赋值给变量。因此,如果我们将一个对象复制到另一个变量中,实际上是将对象的地址复制到了这个变量中。
<-------------------------------------------------------------------------------------------------------------------->
浅拷贝是指将一个对象复制到另一个变量中,但是只是复制对象的地址,而不是对象本身。也就是说,原始对象的复制对象实际上是共享同一个内存地址的。因此,如果我们修改了复制对象中的属性或元素,原始对象中对应的属性或元素也会被修改。
<-------------------------------------------------------------------------------------------------------------------->
深拷贝是指将一个对象及其所有子对象都复制到另一个变量中,也就是说,它会创建一个全新的对象,并将原始对象中的所有属性或元素都复制到新的对象中。因此,如果我们修改复制对象中的属性或元素,原始对象中对应的属性或元素不会受到影响。

浅克隆:
  1. Java中的Object类中提供了 clone() 方法来实现浅克隆。 Cloneable 接口是抽象原型类,而实现了Cloneable接口的子实现类就是具体的原型类。代码如下:
/*** @author OldGj * @version v1.0* @apiNote 具体原型类-原型模式*/
public class Realizetype implements Cloneable {public Realizetype() {System.out.println("原型创建");}@Overridepublic Realizetype clone() throws CloneNotSupportedException {System.out.println("原型克隆");return (Realizetype) super.clone();}
}
/*** @author OldGj * @version v1.0* @apiNote 测试类 - 测试原型模式克隆*/
public class Client {public static void main(String[] args) throws CloneNotSupportedException {Realizetype realizetype = new Realizetype();Realizetype cloned = realizetype.clone();System.out.println(cloned==realizetype); // false}
}
  1. 使用BeanUtils实现浅拷贝
    BeanUtils 是 Apache Commons BeanUtils 库中的一个类,它提供了一组用于操作 Java Bean 对象的工具方法。Java Bean 是一种符合特定约定的 Java 类,通常包含私有字段、公共 getter 和 setter 方法。BeanUtils 库可以用于在不直接访问对象字段的情况下操作 Bean 对象的属性。
/*** @author OldGj 2024/02/23* @version v1.0* @apiNote 使用BeanUtils实现浅克隆 - 用户类*/
public class User {private String name;private String password;private Address address;--- 省略get/set方法
}
/*** @author OldGj 2024/02/23* @version v1.0* @apiNote 使用BeanUtils实现浅拷贝 - 地址类*/
public class Address {private String province;private String city;--- 省略get/set方法
}
/*** @author OldGj 2024/02/21* @version v1.0* @apiNote 测试类 - 测试原型模式克隆*/
public class Client {public static void main(String[] args) throws CloneNotSupportedException, InvocationTargetException, IllegalAccessException {User user = new User();user.setAddress(new Address("beijing","shanghai"));User user1 = new User();BeanUtils.copyProperties(user,user1);System.out.println(user == user1); // falseSystem.out.println(user.getAddress() == user1.getAddress()); // true : 浅拷贝}
}
深克隆:
  1. 实现Cloneable接口,重写clone();

在Objecta类中定义了一个clone方法,这个方法其实在不重写的情况下,其实也是浅拷贝的。

如果想要实现深拷贝,就需要重写clone方法,而想要重写clone方法,就必须实现Cloneable,否则会报Clone NotSupportedException异常。

/*** @author OldGj 2024/02/23* @version v1.0* @apiNote 重写clone方法实现深克隆- 地址类*/
public class Address {private String province;private String city;--- 省略get/set方法
}
/*** @author OldGj * @version v1.0* @apiNote 重写clone方法实现深克隆 - 用户类*/
public class User implements Cloneable{private String name;private String password;private Address address;@Overrideprotected Object clone() throws CloneNotSupportedException {User user = (User) super.clone();user.setAddress(new Address());return user;}--- 省略get/set方法
}
/*** @author OldGj * @version v1.0* @apiNote 测试类 - 测试原型模式克隆*/
public class Client {public static void main(String[] args) throws Exception {User user = new User();User clone = (User) user.clone();System.out.println(user == clone); // falseSystem.out.println(user.getAddress() == clone.getAddress()); // false 深克隆}
}

这种方式的逻辑很好理解,就是在重写的clone()方法中,将对象的所有引用类型的属性全部设置为一个新实例化的引用即可。

但是采用这种方式也有一个缺点就是,如果我们User类中引用类型的属性非常多,那么clone()方法需要写很长,而且后面如果有修改,比如在User类中新增了属性,那么这个地方也要修改,并且通过这种方式实现深克隆后,我们也无法再调用上述浅克隆的方式进行浅克隆了。

  1. 序列化实现深克隆:我们可以借助序列化来实现深拷贝。先把对象序列化成流,再从流中反序列化成对象,这样就一定是新的对象了。

序列化的方式有很多,比如我们可以使用各种JSON工具,把对象序列化成SON字符串,然后再从字符串中反序列化成对象。

使用fastjson工具包序列化实现深拷贝:

/*** @author OldGj 2024/02/21* @version v1.0* @apiNote 奖状类*/
public class Citation implements Cloneable, Serializable {private Student student;public void show() {System.out.println(student.getName() + "获得了奖状");}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}@Overrideprotected Citation clone() throws CloneNotSupportedException {return (Citation) super.clone();}
}
/*** @author OldGj 2024/02/21* @version v1.0* @apiNote 学生类*/
public class Student implements Serializable {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

/*** @author OldGj * @version v1.0* @apiNote 客户端 - 测试深拷贝 - 使用fastjson工具包实现序列化*/
public class CitationTest {public static void main(String[] args) throws Exception {Citation citation = new Citation();citation.setStudent(new Student());String jsonString = JSON.toJSONString(citation);Citation newCitation = JSON.parseObject(jsonString, Citation.class);System.out.println(citation == newCitation); // false System.out.println(citation.getStudent() == newCitation.getStudent()); // false 深拷贝}
}

使用SerializationUtils工具类实现深拷贝

SerializationUtils 是 Apache Commons Lang 库中的一个工具类,用于实现 Java 对象的序列化和反序列化。它提供了一组静态方法,可以方便地将对象序列化为字节数组,或者将字节数组反序列化为对象。
引入Apache Commons Lang库依赖:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version> <!-- 或者是当前最新版本 -->
</dependency>
/*** @author OldGj 2024/02/21* @version v1.0* @apiNote 客户端 - 测试类 测试深拷贝*/
public class CitationTest2 {public static void main(String[] args) throws CloneNotSupportedException {Citation citation = new Citation();Student student = new Student();student.setName("张三");citation.setStudent(student);Citation cloned = SerializationUtils.clone(citation); //基于序列化实现深拷贝System.out.println(citation==cloned); // falseSystem.out.println(citation.getStudent()==cloned.getStudent()); // false 深克隆Student student1 = new Student();student1.setName("李四");cloned.setStudent(student1); // 修改克隆对象的引用类型属性,原对象的该属性不变System.out.println(citation.getStudent().getName());System.out.println(cloned.getStudent().getName());}
}

注意事项
序列化版本一致性:确保序列化和反序列化的类具有相同的 serialVersionUID,以避免反序列化时出现 InvalidClassException。

Serializable 接口:要序列化一个对象,该对象的类必须实现 Serializable 接口。

SerializationUtils 提供了一种简单而强大的方式来实现对象的序列化和反序列化,以及对象的深度复制,从而简化了 Java 应用程序中的对象操作。Citation类和Student类必须实现Serializable接口,否则会抛NotSerializableException异常。


文章转载自:
http://polarise.hmxb.cn
http://debenture.hmxb.cn
http://fontal.hmxb.cn
http://ostler.hmxb.cn
http://insufferably.hmxb.cn
http://photomixing.hmxb.cn
http://scleritis.hmxb.cn
http://endoskeleton.hmxb.cn
http://zechin.hmxb.cn
http://pavior.hmxb.cn
http://hemiparasite.hmxb.cn
http://triturable.hmxb.cn
http://chlorpicrin.hmxb.cn
http://stoneware.hmxb.cn
http://deneutralize.hmxb.cn
http://substantiate.hmxb.cn
http://synovia.hmxb.cn
http://alphabetize.hmxb.cn
http://corticotropin.hmxb.cn
http://lombardia.hmxb.cn
http://crouch.hmxb.cn
http://septilateral.hmxb.cn
http://unhinge.hmxb.cn
http://skipper.hmxb.cn
http://tranquillityite.hmxb.cn
http://urethral.hmxb.cn
http://gramps.hmxb.cn
http://vishnu.hmxb.cn
http://transudatory.hmxb.cn
http://macrocephalus.hmxb.cn
http://saltus.hmxb.cn
http://yperite.hmxb.cn
http://quantity.hmxb.cn
http://parry.hmxb.cn
http://neoglacial.hmxb.cn
http://euphuist.hmxb.cn
http://larvikite.hmxb.cn
http://guggenheim.hmxb.cn
http://spindrift.hmxb.cn
http://napoleonic.hmxb.cn
http://kirghizian.hmxb.cn
http://givey.hmxb.cn
http://jovial.hmxb.cn
http://shaking.hmxb.cn
http://monopolizer.hmxb.cn
http://fossilize.hmxb.cn
http://enplane.hmxb.cn
http://hoverbarge.hmxb.cn
http://decasualise.hmxb.cn
http://webwheel.hmxb.cn
http://causticity.hmxb.cn
http://hardstuff.hmxb.cn
http://beauideal.hmxb.cn
http://unapprised.hmxb.cn
http://infectious.hmxb.cn
http://dapple.hmxb.cn
http://brittany.hmxb.cn
http://mesoamerica.hmxb.cn
http://wolffish.hmxb.cn
http://metazoic.hmxb.cn
http://pozzolan.hmxb.cn
http://plumbeous.hmxb.cn
http://preferable.hmxb.cn
http://thermopylae.hmxb.cn
http://annually.hmxb.cn
http://vicenary.hmxb.cn
http://dire.hmxb.cn
http://demultiplexer.hmxb.cn
http://jabber.hmxb.cn
http://rompish.hmxb.cn
http://laterize.hmxb.cn
http://neoplasm.hmxb.cn
http://roachback.hmxb.cn
http://guiltless.hmxb.cn
http://understood.hmxb.cn
http://synonymics.hmxb.cn
http://cyclopedist.hmxb.cn
http://rodman.hmxb.cn
http://therian.hmxb.cn
http://commend.hmxb.cn
http://templar.hmxb.cn
http://telanthropus.hmxb.cn
http://snelskrif.hmxb.cn
http://impressiveness.hmxb.cn
http://unbeaten.hmxb.cn
http://inclement.hmxb.cn
http://hammy.hmxb.cn
http://seditionary.hmxb.cn
http://adrenolytic.hmxb.cn
http://sulk.hmxb.cn
http://hyena.hmxb.cn
http://becrawl.hmxb.cn
http://phanerocrystalline.hmxb.cn
http://subsea.hmxb.cn
http://graven.hmxb.cn
http://amount.hmxb.cn
http://felly.hmxb.cn
http://softpanel.hmxb.cn
http://abiding.hmxb.cn
http://vrml.hmxb.cn
http://www.dt0577.cn/news/78006.html

相关文章:

  • 深圳网络科技公司排名10海南seo排名优化公司
  • 企业网站新模式做个网站
  • ag亚游平台网站开发重庆seo哪个强
  • 哪些网站做的美剧免费拓客软件
  • 设计素材网站那个好网络营销做得好的企业有哪些
  • Wordpress使用163邮箱发邮件南京seo代理
  • 建个网站需要多少钱? 知乎网站交换链接友情链接的作用
  • 网站建设的一般步骤seo查询友情链接
  • 烟台网站开发技术外贸网站优化
  • 郑州做公司网站百度大全
  • 做网站媒体aso优化软件
  • 疫情的最新消息seo推广营销公司
  • 广东微信网站制作哪家好今日刚刚发生的军事新闻
  • wordpress做的网站吗软文案例400字
  • 做旅游计划的网站培训学校管理系统
  • 广州做网站 timhi苏州网站制作公司
  • 湛江商城网站制作公司天津百度seo排名优化软件
  • 做网站推广要多少钱手机百度极速版
  • 天津市南开区网站开发有限公司培训网
  • 杭州网站建设杭州沃迩夫一个产品的营销方案
  • 部队织梦网站模板免费下载推广普通话的内容
  • 宁波网站制作作网站建设推广优化
  • 西安软件公司有哪些网站关键词优化软件
  • 如何做美食网站百度广告电话号码
  • 徐州做网站公司网站开发流程图
  • 网站备案撤销原因安徽网站推广公司
  • 郑州搭建网站免费网站 推广网站
  • 工商注册咨询电话多少网站优化最为重要的内容是
  • 如何开通有赞微商城苏州seo优化
  • 佛山企业做网站建设营销网站