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

网站外链的建设中国建设银行网站查询密码

网站外链的建设,中国建设银行网站查询密码,免费建立网站软件,申请完域名如何建网站引言 原型是一种创建型设计模式#xff0c; 使你能够复制对象#xff0c; 甚至是复杂对象#xff0c; 而又无需使代码依赖它们所属的类。 所有的原型类都必须有一个通用的接口#xff0c; 使得即使在对象所属的具体类未知的情况下也能复制对象。 原型对象可以生成自身的完…引言 原型是一种创建型设计模式 使你能够复制对象 甚至是复杂对象 而又无需使代码依赖它们所属的类。 所有的原型类都必须有一个通用的接口 使得即使在对象所属的具体类未知的情况下也能复制对象。 原型对象可以生成自身的完整副本 因为相同类的对象可以相互访问对方的私有成员变量。 Java代码示例  使用示例 Java 的 Cloneable  可克隆 接口就是立即可用的原型模式。 任何类都可通过实现该接口来实现可被克隆的性质。 java.lang.Object#clone() 类必须实现 java.lang.Cloneable 接口 识别方法 原型可以简单地通过 clone或 copy等方法来识别。 复制图形 让我们来看看在不使用标准 Cloneable接口的情况下如何实现原型模式。 shapes: 形状列表 shapes/Shape.java: 通用形状接口 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);} } shapes/Circle.java: 简单形状 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;} } shapes/Rectangle.java: 另一个形状 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;} } Demo.java: 克隆示例 import refactoring_guru.prototype.example.shapes.Circle; import refactoring_guru.prototype.example.shapes.Rectangle; import refactoring_guru.prototype.example.shapes.Shape;import java.util.ArrayList; import java.util.List;public class Demo {public static void main(String[] args) {ListShape shapes new ArrayList();ListShape 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(ListShape shapes, ListShape 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!));}}} } OutputDemo.txt: 执行结果 0: Shapes are different objects (yay!) 0: And they are identical (yay!) 1: Shapes are different objects (yay!) 1: And they are identical (yay!) 2: Shapes are different objects (yay!) 2: And they are identical (yay!) 原型注册站 你可以实现中心化的原型注册站 或工厂 其中包含一系列预定义的原型对象。 这样一来 你就可以通过传递对象名称或其他参数的方式从工厂处获得新的对象。 工厂将搜索合适的原型 然后对其进行克隆复制 最后将副本返回给你。 cache cache/BundledShapeCache.java: 原型工厂 import refactoring_guru.prototype.example.shapes.Circle; import refactoring_guru.prototype.example.shapes.Rectangle; import refactoring_guru.prototype.example.shapes.Shape;import java.util.HashMap; import java.util.Map;public class BundledShapeCache {private MapString, 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.java: 克隆示例 import refactoring_guru.prototype.caching.cache.BundledShapeCache; import refactoring_guru.prototype.example.shapes.Shape;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!));}} } OutputDemo.txt: 执行结果 Big green circle ! Medium blue rectangle (yay!) Medium blue rectangles are two different objects (yay!) And they are identical (yay!) GO语言代码示例 概念示例 让我们尝试通过基于操作系统文件系统的示例来理解原型模式。 操作系统的文件系统是递归的 文件夹中包含文件和文件夹 其中又包含文件和文件夹 以此类推。 每个文件和文件夹都可用一个 inode接口来表示。 ​ inode接口中同样也有 clone克隆功能。 file文件和 folder文件夹结构体都实现了 print打印和 clone方法 因为它们都是 inode类型。 同时 注意 file和 folder中的 clone方法。 这两者的 clone方法都会返回相应文件或文件夹的副本。 同时在克隆过程中 我们会在其名称后面添加 “_clone” 字样。 inode.go: 原型接口 package maintype Inode interface {print(string)clone() Inode } file.go: 具体原型 package mainimport fmttype File struct {name string }func (f *File) print(indentation string) {fmt.Println(indentation f.name) }func (f *File) clone() Inode {return File{name: f.name _clone} } folder.go: 具体原型 package mainimport fmttype Folder struct {children []Inodename string }func (f *Folder) print(indentation string) {fmt.Println(indentation f.name)for _, i : range f.children {i.print(indentation indentation)} }func (f *Folder) clone() Inode {cloneFolder : Folder{name: f.name _clone}var tempChildren []Inodefor _, i : range f.children {copy : i.clone()tempChildren append(tempChildren, copy)}cloneFolder.children tempChildrenreturn cloneFolder } main.go: 客户端代码 package mainimport fmtfunc main() {file1 : File{name: File1}file2 : File{name: File2}file3 : File{name: File3}folder1 : Folder{children: []Inode{file1},name: Folder1,}folder2 : Folder{children: []Inode{folder1, file2, file3},name: Folder2,}fmt.Println(\nPrinting hierarchy for Folder2)folder2.print( )cloneFolder : folder2.clone()fmt.Println(\nPrinting hierarchy for clone Folder)cloneFolder.print( ) } output.txt: 执行结果 Printing hierarchy for Folder2Folder2Folder1File1File2File3Printing hierarchy for clone FolderFolder2_cloneFolder1_cloneFile1_cloneFile2_cloneFile3_clone Ruby代码示例 main.rb: 概念示例 # The example class that has cloning ability. Well see how the values of field # with different types will be cloned. class Prototypeattr_accessor :primitive, :component, :circular_referencedef initializeprimitive nilcomponent nilcircular_reference nilend# return [Prototype]def clonecomponent deep_copy(component)# Cloning an object that has a nested object with backreference requires# special treatment. After the cloning is completed, the nested object# should point to the cloned object, instead of the original object.circular_reference deep_copy(circular_reference)circular_reference.prototype selfdeep_copy(self)end# deep_copy is the usual Marshalling hack to make a deep copy. But its rather# slow and inefficient, therefore, in real applications, use a special gemprivate def deep_copy(object)Marshal.load(Marshal.dump(object))end endclass ComponentWithBackReferenceattr_accessor :prototype# param [Prototype] prototypedef initialize(prototype)prototype prototypeend end# The client code. p1 Prototype.new p1.primitive 245 p1.component Time.now p1.circular_reference ComponentWithBackReference.new(p1)p2 p1.cloneif p1.primitive p2.primitiveputs Primitive field values have been carried over to a clone. Yay! elseputs Primitive field values have not been copied. Booo! endif p1.component.equal?(p2.component)puts Simple component has not been cloned. Booo! elseputs Simple component has been cloned. Yay! endif p1.circular_reference.equal?(p2.circular_reference)puts Component with back reference has not been cloned. Booo! elseputs Component with back reference has been cloned. Yay! endif p1.circular_reference.prototype.equal?(p2.circular_reference.prototype)print Component with back reference is linked to original object. Booo! elseprint Component with back reference is linked to the clone. Yay! end output.txt: 执行结果 Primitive field values have been carried over to a clone. Yay! Simple component has been cloned. Yay! Component with back reference has been cloned. Yay! Component with back reference is linked to the clone. Yay!
http://www.zqtcl.cn/news/825135/

相关文章:

  • 广州seo网站策划wordpress关闭主题提示
  • 做门票售卖网站怎么制作自己的水印
  • 网站绑定两个域名怎么做跳转asp 网站后台
  • 百度网站怎么做的赚钱吗郑州资助app下载
  • 成都成华区网站建设天津网站优
  • 大朗网站制作商城网站建设相关费用
  • 付费阅读网站代码搜索引擎推广方式有哪些
  • 企业网站搭建介绍一个电影的网站模板下载
  • wordpress网站插件下载郑州专业网站制作
  • 佛山南海区建网站的公司dw怎么做购物网站
  • 杭州网站关键词排名优化响应式网站好还是自适应网站好
  • 潍坊作风建设网站什么是网站建设技术
  • 网站后台图片不显示东莞市企业招聘信息网
  • 网站发布平台商业网站的网址
  • 免费的培训网站建设门户网站建设管理工作方案
  • 企业网站建设实验感想企业网络推广哪家公司好
  • 网站建设和维护视频如何入侵网站服务器
  • 怎样建设网站空间成都网站设公司
  • 百度文库账号登录入口百度seo规则最新
  • 服务器可以自己的网站吗网络营销策划与创意
  • 广州市招投标网站个人网站可以做论坛
  • 易语言做购物网站春节网站怎么做
  • 建公司网站设计网站公司做网上夫妻去哪个网站
  • 稷山网站建设wordpress单本小说采集
  • 凡客网站规划与建设ppt网站做跳转教程
  • 怎么看网站空间多大做网站旅游销售
  • 天津做手机网站建设旅游网站的目的
  • 飞机查询网站开发的创新点注册公司流程和费用大概多少钱
  • 高质量的邯郸网站建设厦门网页制作厦门小程序app
  • 建设企业网站企业网上银行官网官方二建证从住房建设厅网站调出流程