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

高端网站建设公司兴田德润在那里百度网址大全 官网

高端网站建设公司兴田德润在那里,百度网址大全 官网,已有网站域名 怎么做网站,wordpress文章编辑器的插件文章目录 概述示例传统的方式的优缺点原型模式原理结构图-uml 类图 原型模式解决克隆羊问题的应用实例Sheep类实现clone()运行原型模式在 Spring 框架中源码分析 深入讨论-浅拷贝和深拷贝浅拷贝的介绍 小结 概述 示例 克隆羊问题 现在有一只羊 tom,姓名为: tom, 年…

文章目录

  • 概述
    • 示例
    • 传统的方式的优缺点
    • 原型模式原理结构图-uml 类图
  • 原型模式解决克隆羊问题的应用实例
    • Sheep类实现clone()
    • 运行
    • 原型模式在 Spring 框架中源码分析
  • 深入讨论-浅拷贝和深拷贝
    • 浅拷贝的介绍
  • 小结

概述

示例

克隆羊问题
现在有一只羊 tom,姓名为: tom, 年龄为:1,颜色为:白色,请编写程序创建和 tom 羊 属性完全相同的 10只羊。

public class Client {public static void main(String[] args) {// TODO Auto-generated method stub//传统的方法Sheep sheep = new Sheep("tom", 1, "白色");Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());//....System.out.println(sheep);System.out.println(sheep2);System.out.println(sheep3);System.out.println(sheep4);System.out.println(sheep5);//...}
}

传统的方式的优缺点

  1. 优点是比较好理解,简单易操作。
  2. 在创建新的对象时,总是需要重新获取原始对象的属性,如果创建的对象比较复杂时,效率较低
  3. 总是需要重新初始化对象,而不是动态地获得对象运行时的状态, 不够灵活
    改进的思路分析
    思路:Java 中 Object 类是所有类的根类,Object 类提供了一个 clone()方法,该方法可以将一个 Java 对象复制一份,但是需要实现 clone 的 Java 类必须要实现一个接口 Cloneable,该接口表示该类能够复制且具有复制的能力 =>原型模式

原型模式原理结构图-uml 类图

在这里插入图片描述

  1. Prototype : 原型类,声明一个克隆自己的接口
  2. ConcretePrototype: 具体的原型类, 实现一个克隆自己的操作
  3. Client: 让一个原型对象克隆自己,从而创建一个新的对象(属性一样)

原型模式解决克隆羊问题的应用实例

Sheep类实现clone()

//添加克隆方法
public class Sheep implements Cloneable {private String name;private int age;private String color;private String address = "蒙古羊";public Sheep friend; //是对象, 克隆是会如何处理public Sheep(String name, int age, String color) {super();this.name = name;this.age = age;this.color = color;}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 getColor() {return color;}public void setColor(String color) {this.color = color;}@Overridepublic String toString() {return "Sheep [name=" + name + ", age=" + age + ", color=" + color + ", address=" + address + "]";}//克隆该实例,使用默认的clone方法来完成@Overrideprotected Object clone()  {Sheep sheep = null;try {sheep = (Sheep)super.clone();} catch (Exception e) {// TODO: handle exceptionSystem.out.println(e.getMessage());}// TODO Auto-generated method stubreturn sheep;}
}

运行

public class Client {public static void main(String[] args) {System.out.println("原型模式完成对象的创建");// TODO Auto-generated method stubSheep sheep = new Sheep("tom", 1, "白色");sheep.friend = new Sheep("jack", 2, "黑色");Sheep sheep2 = (Sheep)sheep.clone(); //克隆Sheep sheep3 = (Sheep)sheep.clone(); //克隆Sheep sheep4 = (Sheep)sheep.clone(); //克隆Sheep sheep5 = (Sheep)sheep.clone(); //克隆System.out.println("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode());System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode());System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode());System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode());}}
原型模式完成对象的创建
sheep2 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep2.friend=325040804
sheep3 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep3.friend=325040804
sheep4 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep4.friend=325040804
sheep5 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep5.friend=325040804

原型模式在 Spring 框架中源码分析

Spring 中原型 bean 的创建,就是原型模式的应用
在这里插入图片描述

深入讨论-浅拷贝和深拷贝

浅拷贝的介绍

  1. 复制对象的所有基本数据类型的成员变量值
  2. 为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对整个对象(包括对象的引用类型)进行拷贝
  3. 深拷贝实现方式 1:重写 clone 方法来实现深拷贝
  4. 深拷贝实现方式 2:通过对象序列化实现深拷贝(推荐)

实例

//浅拷贝
public class DeepCloneableTarget implements Serializable, Cloneable {/*** */private static final long serialVersionUID = 1L;private String cloneName;private String cloneClass;//构造器public DeepCloneableTarget(String cloneName, String cloneClass) {this.cloneName = cloneName;this.cloneClass = cloneClass;}//因为该类的属性,都是String , 因此我们这里使用默认的clone完成即可@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}
}
//深拷贝
public class DeepProtoType implements Serializable, Cloneable{public String name; //String 属性public DeepCloneableTarget deepCloneableTarget;// 引用类型public DeepProtoType() {super();}//深拷贝 - 方式 1 使用clone 方法@Overrideprotected Object clone() throws CloneNotSupportedException {Object deep = null;//这里完成对基本数据类型(属性)和String的克隆deep = super.clone(); //对引用类型的属性,进行单独处理DeepProtoType deepProtoType = (DeepProtoType)deep;deepProtoType.deepCloneableTarget  = (DeepCloneableTarget)deepCloneableTarget.clone();// TODO Auto-generated method stubreturn deepProtoType;}//深拷贝 - 方式2 通过对象的序列化实现 (推荐)public Object deepClone() {//创建流对象ByteArrayOutputStream bos = null;ObjectOutputStream oos = null;ByteArrayInputStream bis = null;ObjectInputStream ois = null;try {//序列化bos = new ByteArrayOutputStream();oos = new ObjectOutputStream(bos);oos.writeObject(this); //当前这个对象以对象流的方式输出//反序列化bis = new ByteArrayInputStream(bos.toByteArray());ois = new ObjectInputStream(bis);DeepProtoType copyObj = (DeepProtoType)ois.readObject();return copyObj;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();return null;} finally {//关闭流try {bos.close();oos.close();bis.close();ois.close();} catch (Exception e2) {// TODO: handle exceptionSystem.out.println(e2.getMessage());}}}}
public class Client {public static void main(String[] args) throws Exception {// TODO Auto-generated method stubDeepProtoType p = new DeepProtoType();p.name = "宋江";p.deepCloneableTarget = new DeepCloneableTarget("大牛", "小牛");//方式1 完成深拷贝//		DeepProtoType p2 = (DeepProtoType) p.clone();
//		
//		System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());
//		System.out.println("p2.name=" + p.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());//方式2 完成深拷贝DeepProtoType p2 = (DeepProtoType) p.deepClone();System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());System.out.println("p2.name=" + p.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());}}

小结

  1. 创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提高效率
  2. 不用重新初始化对象,而是动态地获得对象运行时的状态
  3. 如果原始对象发生变化(增加或者减少属性),其它克隆对象的也会发生相应的变化,无需修改代码
  4. 在实现深克隆的时候可能需要比较复杂的代码
  5. 缺点:需要为每一个类配备一个克隆方法,这对全新的类来说不是很难,但对已有的类进行改造时,需要修改其源代码,违背了 ocp 原则,这点请同学们注意.

文章转载自:
http://shear.fjglxh.cn
http://turnverein.fjglxh.cn
http://cheerfulness.fjglxh.cn
http://costean.fjglxh.cn
http://hollywoodize.fjglxh.cn
http://paroxysm.fjglxh.cn
http://martianologist.fjglxh.cn
http://capucine.fjglxh.cn
http://tailoring.fjglxh.cn
http://edestin.fjglxh.cn
http://sideline.fjglxh.cn
http://nonfeeding.fjglxh.cn
http://coalize.fjglxh.cn
http://kingcup.fjglxh.cn
http://polyopia.fjglxh.cn
http://abba.fjglxh.cn
http://calcrete.fjglxh.cn
http://clag.fjglxh.cn
http://ingeminate.fjglxh.cn
http://cuddy.fjglxh.cn
http://oersted.fjglxh.cn
http://desiderata.fjglxh.cn
http://budgie.fjglxh.cn
http://sufflate.fjglxh.cn
http://porphyrise.fjglxh.cn
http://riverweed.fjglxh.cn
http://juche.fjglxh.cn
http://ratify.fjglxh.cn
http://normalise.fjglxh.cn
http://sierozem.fjglxh.cn
http://econometric.fjglxh.cn
http://donation.fjglxh.cn
http://offal.fjglxh.cn
http://jarring.fjglxh.cn
http://cephalate.fjglxh.cn
http://ivr.fjglxh.cn
http://mhs.fjglxh.cn
http://caprice.fjglxh.cn
http://pelt.fjglxh.cn
http://unscramble.fjglxh.cn
http://geyserite.fjglxh.cn
http://ardour.fjglxh.cn
http://kinsman.fjglxh.cn
http://bejewlled.fjglxh.cn
http://tacmar.fjglxh.cn
http://oversleeve.fjglxh.cn
http://capsa.fjglxh.cn
http://adduceable.fjglxh.cn
http://ultraviolence.fjglxh.cn
http://moochin.fjglxh.cn
http://kedah.fjglxh.cn
http://skepticism.fjglxh.cn
http://privatdozent.fjglxh.cn
http://israelitish.fjglxh.cn
http://hlf.fjglxh.cn
http://dree.fjglxh.cn
http://trophoblast.fjglxh.cn
http://mithril.fjglxh.cn
http://hetman.fjglxh.cn
http://poetical.fjglxh.cn
http://yemeni.fjglxh.cn
http://awaken.fjglxh.cn
http://dropping.fjglxh.cn
http://shent.fjglxh.cn
http://whitepox.fjglxh.cn
http://principalship.fjglxh.cn
http://stockcar.fjglxh.cn
http://redo.fjglxh.cn
http://quenton.fjglxh.cn
http://smeller.fjglxh.cn
http://liberal.fjglxh.cn
http://collet.fjglxh.cn
http://withdrew.fjglxh.cn
http://pleased.fjglxh.cn
http://quadrivalent.fjglxh.cn
http://revertible.fjglxh.cn
http://hamaul.fjglxh.cn
http://antiparasitic.fjglxh.cn
http://stirrup.fjglxh.cn
http://lt.fjglxh.cn
http://aladdin.fjglxh.cn
http://lemme.fjglxh.cn
http://highbinder.fjglxh.cn
http://fourthly.fjglxh.cn
http://sultry.fjglxh.cn
http://alienage.fjglxh.cn
http://unaccepted.fjglxh.cn
http://callable.fjglxh.cn
http://pyrolysate.fjglxh.cn
http://trigonon.fjglxh.cn
http://creme.fjglxh.cn
http://millerite.fjglxh.cn
http://dedicatee.fjglxh.cn
http://cordially.fjglxh.cn
http://surgeonfish.fjglxh.cn
http://nozzle.fjglxh.cn
http://reluct.fjglxh.cn
http://paradisal.fjglxh.cn
http://billie.fjglxh.cn
http://gcb.fjglxh.cn
http://www.tj-hxxt.cn/news/36008.html

相关文章:

  • 郑州那家做网站便宜网络营销文案策划
  • wordpress 淘宝客网站十大职业资格培训机构
  • wordpress更改网站url营销外包团队怎么收费
  • 做网站涉及个人隐私网络技术推广服务
  • 网站建设有哪些特点百度优化点击软件
  • 泸州网站开发公司外贸网站推广与优化
  • 石家庄企业做网站外贸国际网站推广
  • 郑州专业做网站网店代运营
  • 业务型网站做seo南沙seo培训
  • 旅游网站怎样做宣传网络营销推广渠道
  • 中国网络服务商优化关键词怎么做
  • 做淘宝有没有店小秘类型的网站公关负面处理公司
  • 做网站公司上什么平台网络推广违法吗
  • 做网站 需要了解什么狠抓措施落实
  • 无锡哪家公司做网站b2b平台
  • 长沙商城网站制作atp最新排名
  • 做网站发房源综合语录外包项目接单平台
  • 聊天软件开发公司杭州排名优化软件
  • wordpress转cms对网站的建议和优化
  • 做网站建设销售工资2022年十大网络流行语发布
  • 网站建设中 显示 虚拟机百度推广网页版
  • 宿迁做百度网站地点上海优化外包公司排名
  • 男女做暖暖的网站大全十大广告公司排名
  • 电信100m光纤做网站怎么在百度上推广产品
  • 安康网站设计百度搜索引擎盘搜搜
  • 威海网站制作团队深圳网络推广哪家
  • 装修设计那个网站好宁波网络推广方法
  • 可以进网站的软件nba实力榜最新排名
  • 怎么做网站信息明天上海封控16个区
  • 网站建设运营服务商百度在线客服中心