贵德县wap网站建设公司/女生学电子商务好吗
概述:将一个对象作为原型,通过对其进行复制而克隆除多个和原型类型类似的新实例。原型对象那个可以生成自身的完成副本,因为相同类的对象可以相互访问对方的私有成员变量。
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!)");}}
}