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

h5网站建站ps自学网

h5网站建站,ps自学网,做网站需要什么 图片视频,青岛模板建站公司引言 原型是一种创建型设计模式#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.pierceye.com/news/85121/

相关文章:

  • 吉祥物在线设计网站网站dns多久刷新
  • 网站制作背景无锡公司网站设计
  • 盐山做网站价格垦利县建设局网站
  • 文昌市住房和城乡建设局网站wordpress如何搬站
  • 百色网站优化三合一网站制作公司
  • 建设网站要求成都青羊区建设局网站
  • 自己建设网站麻烦吗附近广告设计与制作门店电话
  • 相亲网站透露自己做理财的女生手机网站制作建设
  • 湖南省建设信息网站查询自定义域名
  • 北京网站改版哪家好广州外贸网站信息
  • 建设工程自学网站网站开发知识培训
  • 如何进行网站分析wordpress 设置cookies
  • 佛山网站建设服务基于百度地图的网站开发
  • 如何做网站的后台管理海南景区网站建设方案
  • 做网站维护费是怎么算的高明网站设计多少钱
  • 如何加入网站天津营销网站建设联系方式
  • 提高网站响应速度广告招牌设计效果图
  • 大连市开发区网站建设公司手机网站开发工具 2018
  • 网站 策划福州2017网站建设
  • 做哪一类网站容易有排名红河公司 网站建设
  • 网站搜索引擎优化方案的案例最大的建材采购平台
  • 上海做机床的公司网站深圳it公司
  • 网站名称怎样做建设刷单网站
  • wordpress 快速建站网络空间设计说明怎么写
  • 全栈网站开发工程师wordpress国内现状
  • 做网站 报价 需要了解快速排名工具免费查询
  • 代写代码的平台如何优化移动端网站
  • 准备网站的工作网站托管方案
  • 网站制作的评价标准深圳网站建设怎样做
  • 网站报价书app制作工具无需编程