avatar

目录
Design Patterns: Prototype

What is Prototype

It is known as Clone. Prototype is a creational design pattern that enables you copy/clone existing object to create new objects.

Example

Given below exmpale shows how Prototyp works. Here I created Animal Interface that extends Cloneable. Dog and Sheep both implement Animal as well as it’s makeCopy method. CloneFactory playing as broker and it offers getClone (super.clone()) method to return the copy object of its input Animal parameter.

image-20200323160213729

  • Animal Interface

    java
    1
    2
    3
    public interface Animal extends Cloneable {
    public Animal makeCopy();
    }
  • Dog, it implements Animal. From makeCopy method, it use super.clone() to clone existing object.

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class Dog implements Animal{

    public Dog(){
    System.out.println("Dog is Made");
    }
    public Animal makeCopy() {
    System.out.println("Dog is being made");
    Dog dog = null;
    try {
    dog = (Dog) super.clone(); // use clone method to clone object
    } catch (CloneNotSupportedException e) {
    e.printStackTrace();
    }
    return dog;
    }

    public String toString(){
    return "A Dog!";
    }

    }
  • Sheep, the same structure with Dog class

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class Sheep implements Animal{

    public Sheep(){
    System.out.println("Sheep is Made");
    }
    public Animal makeCopy() {
    System.out.println("Sheep is being made");
    Sheep sheep = null;
    try {
    sheep = (Sheep) super.clone();
    } catch (CloneNotSupportedException e) {
    e.printStackTrace();
    }
    return sheep;
    }

    public String toString(){
    return "A Sheep";
    }

    }
  • CloneFactoy

    java
    1
    2
    3
    4
    5
    public class CloneFactory {
    public Animal getClone(Animal animal){
    return animal.makeCopy();
    }
    }
  • TestCloning

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public class TestCloning {
    public static void main(String[] args) {
    //crete facory instance
    CloneFactory cloneFactory = new CloneFactory();
    Sheep sheep = new Sheep();
    Sheep clonedSheep = (Sheep) cloneFactory.getClone(sheep);

    // sheep and clonedSheep are having the different hashcode which means they are different object.
    System.out.println(sheep.hashCode());
    System.out.println(clonedSheep.hashCode());

    Dog dog = new Dog();
    Dog clonedDog = (Dog) cloneFactory.getClone(dog);

    System.out.println(dog.hashCode());
    System.out.println(clonedDog.hashCode());
    }
    }
文章作者: Aries Kou
文章链接: http://yoursite.com/posts/Design-Patterns/794099507.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Aries' Blog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论