Dahua Design Patterns--Prototype Patterns

  In some scenarios, it is necessary to copy and use some repeated data such as resumes, templates, etc., so it is too troublesome to do it manually, so there is a prototype mode in java code design, that is, a clone mode.

      Each class that needs to be cloned must have a Clone method and implement the Cloneable interface, otherwise CloneNotSupportedException will be reported.   

  Prototype mode has two states: shallow copy and deep copy.

   Shallow copy: The memory address of the original type is called, which is actually the same as the original class.


   Deep copy: Copy the original data and re-create a memory space in memory, that is, a new object.

    Examples are as follows:

package effective.yuanxing;

public class WorkExperience implements Cloneable {
	private String workDate;
	public String WorkDate;
	private String company;
	public  String Company;
	public String getWorkDate() {
		return workDate;
	}
	public void setWorkDate(String workDate) {
		this.workDate = workDate;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public Object Clone(){
		Object obj = null;
		try {
			obj = super.clone();
			return obj;
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
			System.out.println("Clone not supported");
			return null ;
		}
	}

}

Shallow clone:

package effective.yuanxing;

public class Resume implements Cloneable{

	private String name;
	private String sex;
	private String age;
	
	private WorkExperience work;
	
	public Resume(String name ){
		this.name = name;
		work = new WorkExperience();
	}
	//set personal information
	public void SetPersonalInfo(String sex , String age){
		this.sex = sex;
		this.age = age;
	}
	//set work experience
	public void SetWorkExperoence(String workDate , String company){
		work.WorkDate = workDate;
		work.Company = company;
	}
	//show
	public void Display(){
		System.out.println("name:"+name +"sex:"+sex+"age: "+age);
		System.out.println("Work experience: "+work.WorkDate+work.Company);
	}
	public Object Clone(){
		try {
			return (Object)super.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
			return null;
		}
	}
	public static void main(String[] args) {
		Resume a = new Resume("Big Bird");
		a.SetPersonalInfo("男", "29");
		a.SetWorkExperoence("2015-2018", "XX公司");
		Resume b =(Resume)a.Clone();
		b.SetPersonalInfo("2016-2018", "yy enterprise");
		Resume c =(Resume)a.Clone();
		c.SetPersonalInfo("男", "24");
		c.SetWorkExperoence("1998-2003", "zo企业");
		
		a.Display();
		b.Display();
		c.Display();
                System.out.println(a.work==b.work);
		System.out.println(a.work.Company);
	}
}
result:
name: big bird sex: male age: 29
Work experience: 1998-2003zo enterprise
name: Big Bird sex: 2016-2018age: yy enterprise
Work experience: 1998-2003zo enterprise
name: big bird sex: male age: 24
Work experience: 1998-2003zo enterprise
true
zo enterprise

It can be seen that the work object of the shallow copy is the same object. And the original Company will also be modified, that is, the original object is accessed.

deep copy:

package effective.yuanxing;

public class DeepResume {

	private String name;
	private String sex;
	private String age;
	
	private WorkExperience work;
	
	public DeepResume(String name ){
		this.name = name;
		work = new WorkExperience();
	}
	public DeepResume(WorkExperience work){
		this.work = (WorkExperience)work.Clone();
	}
	//set personal information
	public void SetPersonalInfo(String sex , String age){
		this.sex = sex;
		this.age = age;
	}
	//set work experience
	public void SetWorkExperoence(String workDate , String company){
		work.WorkDate = workDate;
		work.Company = company;
	}
	//show
	public void Display(){
		System.out.println("name:"+name +"sex:"+sex+"age: "+age);
	}
	public Object Clone(){
		DeepResume obj = new DeepResume(this.work);
		obj.name = this.name;
		obj.sex = this.sex;
		obj.age = this.age;
		return obj;
	}
	public static void main(String[] args) {
		DeepResume a = new DeepResume("Big Bird");
		a.SetPersonalInfo("男", "29");
		a.SetWorkExperoence("2015-2018", "XX公司");
		DeepResume b =(DeepResume)a.Clone();
		b.SetPersonalInfo("2016-2018", "yy enterprise");
		DeepResume c =(DeepResume)a.Clone();
		c.SetPersonalInfo("男", "24");
		c.SetWorkExperoence("1998-2003", "zz企业");
		
		a.Display();
		b.Display();
		c.Display();
		System.out.println(b.work==c.work);
		System.out.println(a.work.Company);
	}
}

result:

name: big bird sex: male age: 29
name: Big Bird sex: 2016-2018age: yy enterprise
name: big bird sex: male age: 24
false
XX company

Visible their work objects are recreated.

Application scenario: Similar to a game like Gobang, when creating chess pieces, you can use the prototype mode to create flags

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325658320&siteId=291194637