Design Pattern-prototype pattern case code

Source: Internet
Author: User

 

Prototype: Use a prototype instance to specify the object type and copy the prototype to create a new object.

The class diagram is as follows:

We all know that the following code cannot create new objects. They all point to the same object.

Case code:

Domesticrobots. java

 

/*** Housework robot ** @ author wanghl **/public class Domesticrobots {private String mName; private String mVersion; public Domesticrobots (String name, String version) {mName = name; mVersion = version ;}@ Overridepublic String toString () {return Domesticrobots [mName = + mName +, mVersion = + mVersion +] + super. toString ();}}

Main. java

 

 

Public class Main {public static void main (String args []) {Domesticrobots robots1 = new robot (household robot 1, Terminator Generation 1); Domesticrobots robots2 = robots1; Domesticrobots robots3 = robots1; system. out. println (robots1); System. out. println (robots2); System. out. println (robots3 );}}

The output is as follows:

 

 

Domesticrobots [mName = housework robot 1, mVersion = Terminator Generation 1] Domesticrobots @ 3ce53?domesticrobots [mName = housework robot 1, mVersion = Terminator Generation 1] Domesticrobots @ Taobao [mName = housework robot 1, mVersion = Terminator Generation 1] Domesticrobots @ 3ce53108

If you want to produce household robots in batches, we can use the prototype.

 

In order to clone and produce household robots, we allow them to implement clone interfaces.

ICloneRobots. java

 

public interface IRobotsClone
 
   {public T clone();}
 

The modified Domesticrobots class:

 

 

/*** Housework robot ** @ author wanghl **/public class Domesticrobots implements IRobotsClone
 
  
{Private String mName; private String mVersion; public Domesticrobots (String name, String version) {mName = name; mVersion = version ;}@ Overridepublic String toString () {return Domesticrobots [mName = + mName +, mVersion = + mVersion +] + super. toString () ;}public Domesticrobots clone () {return new Domesticrobots (mName, mVersion );}}
 

Test code:

 

Main. java

 

Public class Main {public static void main (String args []) {Domesticrobots robots1 = new Domesticrobots (housekeeping robot 1, Terminator Generation 1); Domesticrobots robotⅱ = robots1.clone (); domesticrobots robotsIII = robots1.clone (); System. out. println (robots1); System. out. println (robotⅱ); System. out. println (robotsIII );}}
Print result:

 

 

Domesticrobots [mName = housework robot 1, mVersion = Terminator Generation 1] Domesticrobots @ 6af62373Domesticrobots [mName = housework robot 1, mVersion = Terminator Generation 1] Domesticrobots @ Taobao [mName = housework Robot, mVersion = Terminator Generation 1] Domesticrobots @ 55f33675
We can see that our robotⅱ and robotsIII are different objects.

 

 

The prototype mode allows you to create another object from one object without knowing the details of the Creation.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.