设计模式之原型模式

package designPatterns.builder.prototype;

import java.util.Objects;

/**
 * @since 2020-08-23 19:03:27
 * @author TEENWAY
 * 原型是一种创建型设计模式, 使你能够复制对象, 甚至是复杂对象, 而又无需使代码依赖它们所属的类。
 *
 * 所有的原型类都必须有一个通用的接口, 使得即使在对象所属的具体类未知的情况下也能复制对象。
 * 原型对象可以生成自身的完整副本, 因为相同类的对象可以相互访问对方的私有成员变量。
 */
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();

    @Override
    public 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);
    }
}
package designPatterns.builder.prototype;

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;
        }
    }

    @Override
    public Shape clone() {
     
        return new Rectangle(this);
    }

    @Override
    public boolean equals(Object object2) {
     
        if (!(object2 instanceof Rectangle) || !super.equals(object2)) return false;
        Rectangle shape2 = (Rectangle) object2;
        return shape2.width == width && shape2.height == height;
    }
}
package designPatterns.builder.prototype;

public class Circle extends Shape {
     
    public int radius;

    public Circle() {
     
    }

    public Circle(Circle target) {
     
        super(target);
        if (target != null) {
     
            this.radius = target.radius;
        }
    }

    @Override
    public Shape clone() {
     
        return new Circle(this);
    }

    @Override
    public boolean equals(Object object2) {
     
        if (!(object2 instanceof Circle) || !super.equals(object2)) return false;
        Circle shape2 = (Circle) object2;
        return shape2.radius == radius;
    }
}
package designPatterns.builder.prototype;

import java.util.HashMap;
import java.util.Map;

/**
 * @author TEENWAY
 * @since 2020-08-23 19:06
 * 原型注册站
 * 你可以实现中心化的原型注册站 (或工厂), 其中包含一系列预定义的原型对象。
 * 这样一来, 你就可以通过传递对象名称或其他参数的方式从工厂处获得新的对象。
 * 工厂将搜索合适的原型, 然后对其进行克隆复制, 最后将副本返回给你。
 **/
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();
    }
}

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!)

Process finished with exit code 0

package designPatterns.builder.prototype;

/**
 * @author TEENWAY
 * @since 2020-08-23 19:07
 **/
public class Demo1 {
     
    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!)");
        }
    }
}

Big green circle != Medium blue rectangle (yay!)
Medium blue rectangles are two different objects (yay!)
And they are identical (yay!)

你可能感兴趣的:(Java基础)