Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
We continue to use questions to lead out this article
The problem of cloning sheep
Now there is a sheep Tom, name: Tom, age: 1, color: white, please write a program to create 10 sheep with the same attributes as Tom sheep.
First of all, let’s do it the old-fashioned way
The first step is to create a sheep
public class Sheep { private String name; private int age; private String color; public Sheep(String name, int age, String color) { super(); this.name = name; this.age = age; this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Sheep [name=" + name + ", age=" + age + ", color=" + color + "]"; }}Copy the code
We’re creating a Client to call the Sheep class
Public class Client {public static void main(String[] args) {// TODO auto-generated method stub // Traditional method Sheep = New Sheep(" Tom ", 1, "white "); Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor()); / /... System.out.println(sheep); System.out.println(sheep2); System.out.println(sheep3); System.out.println(sheep4); System.out.println(sheep5); / /... }}Copy the code
This method looks very simple, but when we create a new object, we always need to obtain the properties of the original object. If the object is complex, the efficiency will be very low, and we always need to initialize the object, rather than dynamically obtain the object, which is not flexible enough
To improve the way
The Object class is the root of all classes in Java. The Object class provides a clone() method, which can make a copy of a Java Object. However, the Clone class must implement a Cloneable interface. This interface indicates that the class can copy and is capable of copying => Prototype mode
-
The Prototype pattern refers to using Prototype instances to specify the type of objects to be created and copying these prototypes to create new objects
-
The prototype pattern is a creative design pattern that allows one object to create another customizable object without knowing the details of how to create it
-
How it works is that by passing a prototype object to the creation object, the creation object performs the creation by asking the prototype objects to copy themselves, that is, the object.clone ()
First we need to modify our Sheep class
public class Sheep implements Cloneable { private String name; private int age; private String color; Private String address = "private String address "; public Sheep friend; Public Sheep(String name, int age, String color) {super(); this.name = name; this.age = age; this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Sheep [name=" + name + ", age=" + age + ", color=" + color + ", address=" + address + "]"; @override protected Object clone() {Sheep = null; try { sheep = (Sheep)super.clone(); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } // TODO Auto-generated method stub return sheep; }}Copy the code
When we need to create new objects, we perform cloning
Public class Client {public static void main(String[] args) {system.out.println (" prototype mode to create the object "); // TODO auto-generated method Stub Sheep Sheep = new Sheep(" Tom ", 1, "white "); Sheep. Friend = new sheep ("jack", 2, "black "); Sheep sheep2 = (Sheep)sheep.clone(); // Clone Sheep sheep3 = (Sheep)sheep.clone(); // Clone Sheep sheep4 = (Sheep)sheep.clone(); // Clone Sheep sheep5 = (Sheep)sheep.clone(); // clone system.out.println ("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode()); System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode()); System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode()); System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode()); }}Copy the code