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

贵德县wap网站建设公司/女生学电子商务好吗

贵德县wap网站建设公司,女生学电子商务好吗,简单网站建设 有教程视频,长沙关键词优化概述:将一个对象作为原型,通过对其进行复制而克隆除多个和原型类型类似的新实例。原型对象那个可以生成自身的完成副本,因为相同类的对象可以相互访问对方的私有成员变量。 1.优缺点 1).优点: Java自带的原型模式基于内存二进制…

概述:将一个对象作为原型,通过对其进行复制而克隆除多个和原型类型类似的新实例。原型对象那个可以生成自身的完成副本,因为相同类的对象可以相互访问对方的私有成员变量。

1.优缺点

1).优点:

  • Java自带的原型模式基于内存二进制流的复制,在性能上比直接new一个对象更加优良。
  • 可以使用深克隆方式保存对象的状态,使用原型模式将对象复制一份,并将其状态保存起来,简化了创建对象的过程,以便在需要的时候使用,可辅助实现撤销操作。

2).缺点:

  • 克隆包含循环引用的复杂对象可能会非常麻烦。

2.应用场景

  • 如果你需要复制一些对象,同时又希望代码独立于这些对象所属的具体类,可以使用原型模式。
  • 如果子类的区别在于其对象的初始化方式,那么你可以使用该模式来减少子类的数量。别人创建这些子类的目的可能是为了创建特定类型的对象。
  • 在原型模式中,你可以使用一系列预生成的,各种类型的对象作为原型。

3. 实现方式 (java)

在Java中的Cloneable 接口就是立即可用的原型模式。

接下来不使用标准Cloneable接口的情况下如何实现原型模式。

1)形状列表

通用形状接口

import java.util.Objects;public abstract class Shape {public int x;public int y;public String color;public Shape() {}public Shape(Shape target) {if (target != null) {this.x = target.x;this.y = target.y;this.color = target.color;}}public abstract Shape clone();@Overridepublic boolean equals(Object object2) {if (!(object2 instanceof Shape)) return false;Shape shape2 = (Shape) object2;return shape2.x == x && shape2.y == y && Objects.equals(shape2.color, color);}
}

圆形

public class Circle extends Shape {public int radius;public Circle() {}public Circle(Circle target) {super(target);if (target != null) {this.radius = target.radius;}}@Overridepublic Shape clone() {return new Circle(this);}@Overridepublic boolean equals(Object object2) {if (!(object2 instanceof Circle) || !super.equals(object2)) return false;Circle shape2 = (Circle) object2;return shape2.radius == radius;}
}

矩形

public class Rectangle extends Shape {public int width;public int height;public Rectangle() {}public Rectangle(Rectangle target) {super(target);if (target != null) {this.width = target.width;this.height = target.height;}}@Overridepublic Shape clone() {return new Rectangle(this);}@Overridepublic boolean equals(Object object2) {if (!(object2 instanceof Rectangle) || !super.equals(object2)) return false;Rectangle shape2 = (Rectangle) object2;return shape2.width == width && shape2.height == height;}
}

克隆示例

import java.util.ArrayList;
import java.util.List;public class Demo {public static void main(String[] args) {List<Shape> shapes = new ArrayList<>();List<Shape> shapesCopy = new ArrayList<>();Circle circle = new Circle();circle.x = 10;circle.y = 20;circle.radius = 15;circle.color = "red";shapes.add(circle);Circle anotherCircle = (Circle) circle.clone();shapes.add(anotherCircle);Rectangle rectangle = new Rectangle();rectangle.width = 10;rectangle.height = 20;rectangle.color = "blue";shapes.add(rectangle);cloneAndCompare(shapes, shapesCopy);}private static void cloneAndCompare(List<Shape> shapes, List<Shape> shapesCopy) {for (Shape shape : shapes) {shapesCopy.add(shape.clone());}for (int i = 0; i < shapes.size(); i++) {if (shapes.get(i) != shapesCopy.get(i)) {System.out.println(i + ": Shapes are different objects (yay!)");if (shapes.get(i).equals(shapesCopy.get(i))) {System.out.println(i + ": And they are identical (yay!)");} else {System.out.println(i + ": But they are not identical (booo!)");}} else {System.out.println(i + ": Shape objects are the same (booo!)");}}}
}

2) cache

原型注册站

创建一个原型工厂,其中包含一系列预定义的原型对象。这样一来,你就可以通过传递对象名称或其他参数的方式从工厂处获得新的对象。工厂将搜索合适的原型,然后对其进行克隆复制,最后将副本返回给你。

原型工厂

import java.util.HashMap;
import java.util.Map;public class BundledShapeCache {private Map<String, Shape> cache = new HashMap<>();public BundledShapeCache() {Circle circle = new Circle();circle.x = 5;circle.y = 7;circle.radius = 45;circle.color = "Green";Rectangle rectangle = new Rectangle();rectangle.x = 6;rectangle.y = 9;rectangle.width = 8;rectangle.height = 10;rectangle.color = "Blue";cache.put("Big green circle", circle);cache.put("Medium blue rectangle", rectangle);}public Shape put(String key, Shape shape) {cache.put(key, shape);return shape;}public Shape get(String key) {return cache.get(key).clone();}
}

demo示例

public class Demo {public static void main(String[] args) {BundledShapeCache cache = new BundledShapeCache();Shape shape1 = cache.get("Big green circle");Shape shape2 = cache.get("Medium blue rectangle");Shape shape3 = cache.get("Medium blue rectangle");if (shape1 != shape2 && !shape1.equals(shape2)) {System.out.println("Big green circle != Medium blue rectangle (yay!)");} else {System.out.println("Big green circle == Medium blue rectangle (booo!)");}if (shape2 != shape3) {System.out.println("Medium blue rectangles are two different objects (yay!)");if (shape2.equals(shape3)) {System.out.println("And they are identical (yay!)");} else {System.out.println("But they are not identical (booo!)");}} else {System.out.println("Rectangle objects are the same (booo!)");}}
}
http://www.lbrq.cn/news/1604845.html

相关文章:

  • 寻找做日文网站/百度网站的网址
  • 双语网站建设方案/哪里的网络推广培训好
  • 商城网站开发方案书/一手项目对接app平台
  • 电子商务系统 网站建设/打字赚钱平台 学生一单一结
  • mvc在网站开发中的应用/吉林关键词优化的方法
  • 政务中心网站建设方案/慈溪seo排名
  • wordpress自动更新电视剧/seo 适合哪些行业
  • 国外建筑设计网站推荐/广告营销案例分析
  • 开发网站的基本流程/八大营销方式有哪几种
  • 内网做网站外网访问/宁德市人民政府
  • 没内涵网站源码/一个完整的营销策划案范文
  • 潍坊网站建设方案/信息流广告公司排名
  • 做学校后台网站用什么浏览器/优化网站打开速度
  • wordpress 快递公司/百度关键词优化工具
  • 做网站必须要购买空间吗/广告点击一次多少钱
  • 宜昌平台网站建设/恩施seo整站优化哪家好
  • 手提包 东莞网站建设/视频互联网推广选择隐迅推
  • wap网站在线生成/谷歌官网注册入口
  • 镇海网站建设/免费的网站
  • 孝感哪家做网站的公司好/企业营销策划实训报告
  • 网站建设公司的市场定位/北京网站排名推广
  • 淘宝网络营销推广方式/长沙seo男团
  • 大连建站费用/邢台网站网页设计
  • 做网站 用什么语言好/seo网站分析
  • 只做一种产品的网站/自己做网站的流程
  • 教育网站如何做seo/台州网站制作维护
  • 怎么自己创建一个平台/网络优化工程师有前途吗
  • 中国建设招标网站首页/飞猪关键词排名优化
  • 生物科技网站建设 中企动力北京/超级seo外链
  • 选择赣州网站建设/百度推广如何代理加盟
  • 配置Mybatis环境
  • 深度学习TR3周:Pytorch复现Transformer
  • Nginx服务做负载均衡网关
  • io_cancel系统调用及示例
  • Spring lookup-method实现原理深度解析
  • Compose笔记(四十一)--ExtendedFloatingActionButton