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

备案网站建设方案书网络营销外包公司哪家好

备案网站建设方案书,网络营销外包公司哪家好,久久建筑网企业,大学生网页设计主题在开发一个界面的时候#xff0c;里面有多个Button#xff0c;这些对象的属性内容相似。如果一个个实例化Button对象#xff0c;并设置其属性#xff0c;那么代码量将会增多。 通过一个原型对象克隆出多个一模一样的对象#xff0c;该模式被称为原型模式。 图 原型模式 … 在开发一个界面的时候里面有多个Button这些对象的属性内容相似。如果一个个实例化Button对象并设置其属性那么代码量将会增多。 通过一个原型对象克隆出多个一模一样的对象该模式被称为原型模式。 图 原型模式 Prototype: 抽象原型类是声名克隆方法的接口也可以是具体原型类的公共父类。 ConcretePrototype具体原型类实现了克隆方法在克隆方法中返回自己的一个克隆对象。 Client客户类让一个原型对象克隆自身从而创建一个新的对象。 public interface ButtonClone {ButtonClone cloneBtn();}public class Button implements ButtonClone{private Double width;private Double height;public Double getWidth() {return width;}public void setWidth(Double width) {this.width width;}public Double getHeight() {return height;}public void setHeight(Double height) {this.height height;}Overridepublic ButtonClone cloneBtn() {Button newBtn new Button();newBtn.setHeight(this.height);newBtn.setWidth(this.width);return newBtn;} }1 浅克隆与深克隆 浅克隆如果源对象的成员变量是值类型将复制一份给克隆对象如果源对象的成员变量是引用类型则将引用对象的地址复制一份给对象。 深克隆无论源对象的成员变量是值类型还是引用类型都将复制一份给目标对象。 1.1 浅克隆 Java的Object类提供了一个clone()方法可以将一个Java对象浅克隆。能实现克隆Java类必须实现一个标识接口Cloneable表示这个类支持被复制。 public class ConcretePrototype implements Cloneable{private String name;private String type;private ListString list new ArrayList();public ConcretePrototype(String name, String type) {this.name name;this.type type;}Overridepublic String toString() {return ConcretePrototype{ name name \ , type type \ , list list };}public static void main(String[] args) throws CloneNotSupportedException {ConcretePrototype prototype new ConcretePrototype(实际类型, 浅克隆);System.out.println(prototype: prototype);Object clone prototype.clone();System.out.println(clone: clone);prototype.list.add(hello);System.out.println(clone: clone);}} 1.2 深克隆 深克隆除了对象本身被复制外对象所包含的所有成员变量也将被复制。在Java中如果需要实现深克隆可以通过序列化等方式来实现。需要实例化的对象其类必须实现Serializable接口否则无法实现序列化操作。 public class CusForm implements Serializable , Cloneable{private String name;private CusFile cusFile;public CusForm(String name, CusFile cusFile) {this.name name;this.cusFile cusFile;}public static void main(String[] args) throws IOException, ClassNotFoundException, CloneNotSupportedException {CusForm cusForm new CusForm(表单, new CusFile());System.out.println(cusForm);System.out.println(cusForm.clone()); //java浅克隆对象成员变量直接复制地址CusForm deepCloneObj cusForm.deepClone();System.out.println(deepCloneObj);// 运行结果 // CusForm{name表单, cusFilecom.huangmingfu.prototype.deep.CusForm$CusFile1b6d3586} // CusForm{name表单, cusFilecom.huangmingfu.prototype.deep.CusForm$CusFile1ddc4ec2} // CusForm{name表单, cusFilecom.huangmingfu.prototype.deep.CusForm$CusFile1b6d3586}}public CusForm deepClone() throws IOException, ClassNotFoundException {ByteArrayOutputStream outputStream new ByteArrayOutputStream();ObjectOutputStream objectOutputStream new ObjectOutputStream(outputStream);objectOutputStream.writeObject(this);ByteArrayInputStream inputStream new ByteArrayInputStream(outputStream.toByteArray());ObjectInputStream objectInputStream new ObjectInputStream(inputStream);return (CusForm) objectInputStream.readObject();}Overridepublic String toString() {return CusForm{ name name \ , cusFile cusFile };}private static class CusFile implements Serializable{} } 2 原型管理器 将多个原型对象存储在一个集合中供客户端使用它是一个专门复制克隆对象的工厂。其中定义了一个集合用于存储原型对象。 图 原型管理器 public class DeepClone implements Serializable {public DeepClone deepClone() {try {ByteArrayOutputStream outputStream new ByteArrayOutputStream();ObjectOutputStream objectOutputStream new ObjectOutputStream(outputStream);objectOutputStream.writeObject(this);ByteArrayInputStream inputStream new ByteArrayInputStream(outputStream.toByteArray());ObjectInputStream objectInputStream new ObjectInputStream(inputStream);return (DeepClone)objectInputStream.readObject();} catch (Exception e) {System.out.println(e.getMessage());}return null;} }public class ConcreteA extends DeepClone{private final String name concreteA;Overridepublic String toString() {return ConcreteA{ name name \ } super.toString();}}public class ConcreteB extends DeepClone{private final String name ConcreteB;Overridepublic String toString() {return ConcreteB{ name name \ } super.toString();} }public class PrototypeManager {private final MapClass? extends DeepClone,DeepClone prototypeMap new HashMap();private final SetClass? extends DeepClone classes new HashSet();private PrototypeManager() {}private static class HolderClass {private final static PrototypeManager instance new PrototypeManager();}public static PrototypeManager getInstance() {return HolderClass.instance;}public void addPrototype(DeepClone deepClone) {if (classes.add(deepClone.getClass())) {prototypeMap.put(deepClone.getClass(),deepClone);}}public DeepClone getClone(Class? extends DeepClone cls) {DeepClone deepClone prototypeMap.get(cls);if (deepClone null) {throw new RuntimeException(克隆体不存在);}return deepClone.deepClone();}}public class Client {public static void main(String[] args) {ConcreteA concreteA new ConcreteA();ConcreteB concreteB new ConcreteB();PrototypeManager prototypeManager PrototypeManager.getInstance();System.out.println(concreteA);System.out.println(concreteB);prototypeManager.addPrototype(concreteA);prototypeManager.addPrototype(concreteB);System.out.println(克隆分割线---------------);DeepClone deepCloneA prototypeManager.getClone(ConcreteA.class);DeepClone deepCloneB prototypeManager.getClone(ConcreteB.class);System.out.println(deepCloneA);System.out.println(deepCloneB);// 运行结果 // ConcreteA{nameconcreteA}com.huangmingfu.prototype.manager.ConcreteA1b6d3586 // ConcreteB{nameConcreteB}com.huangmingfu.prototype.manager.ConcreteB4554617c // 克隆分割线--------------- // ConcreteA{nameconcreteA}com.huangmingfu.prototype.manager.ConcreteA30dae81 // ConcreteB{nameConcreteB}com.huangmingfu.prototype.manager.ConcreteB1b2c6ec2}}3 适用场景 当创建新的对象实例较为复杂时使用原型模式可以简化对象的创建工厂通过复制一个已有实例可以提高新实例的创建效率。 创建新对象成本较大。对象的状态变化很小且系统需要保存对象的状态时。需要创建许多相似对象。
http://www.huolong8.cn/news/75886/

相关文章:

  • 网站制作什么巢湖自助建站系统
  • 运城有做网站设计wordpress静态页面
  • 广州网站排名优化报价北京专业英文网站建设
  • 广西省住房和城乡建设厅官方网站网站建设的展望
  • 子凡wordpressseo范畴
  • 顶呱呱集团 网站建设新手学网络运营要多久
  • 啥前端框架可以做网站首页.net网站开发实训体会
  • 网站建设的技术问题不想花钱怎么做网站
  • wordpress 网站显示加载时长网络销售新手入门
  • 网站开发助手家教网站开发公司
  • 青岛行业网站建设电话哈尔滨公共资源网
  • 站长工具短链接生成教学网站模板下载
  • 专业网站的特点做网站有没有前途
  • 紫川网站建设最新国际新闻大事件
  • 有没有做婚车的网站项目定制开发网站
  • 浙江温州城乡建设网站福清哪有做网站的地方
  • 可以做哪些有趣的网站注册公司注册资金多少为好
  • 自己网站上放个域名查询建站工具评测 discuz
  • 对网站建设 意见和建议泰国浪琴手表网站
  • 国外做的比较好的购物网站百度营销中心
  • 做魔杖网站wordpress 未能连接到ftp服务器
  • 网站开发 ppt怎么自己做一个网页
  • 腾讯云网站备案吗免费设计签名软件
  • 搭建淘宝客网站源码网站前台怎么套用织梦后台
  • 企业手机网站建设咨询微能力者恶魔网站谁做的
  • 可以做雷达图的网站引擎搜索
  • 文山州建设局信息网站网站百度推广怎么做的
  • 照片管理网站模板公司商业网站怎么做
  • 深圳专业做网站设计做网站还能赚钱
  • 幸运28网站代理怎么做长春网站推广优化公司