Prototype pattern)

Source: Internet
Author: User

Package prototype;
/*
* Prototype mode definition: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
* The Prototype mode allows an object to create another custom object without any details on how to create it.
* Working principle: Send a prototype object to the object to be created. The object to be created is requested
* Type objects are copied and created by themselves.
*/
Public abstract class Monkey implements Cloneable {

Private String monName;
Public void setMonName (String monName ){
This. monName = monName;
}
Public String getMonName (){
Return monName;
}
@ Override
Protected Object clone () throws CloneNotSupportedException {
Object obj = null;
If (obj = null)
Obj = super. clone ();
Return obj;
}
} Www.2cto.com
Package prototype;

Public class Human extends Monkey {

Public Human ()
{
SetMonName ("human ");
}
Public static void main (String [] args) throws Exception
{
Monkey mk = new Human ();
System. out. println (mk. getMonName ());
Monkey mk2 = (Monkey) mk. clone ();
System. out. println (mk2.getMonName ());
System. out. println ("equal:" + mk. equals (mk2 ));
}
}


 

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.