On design pattern: Prototype mode (Prototype pattern)

Source: Internet
Author: User

Love life, enjoy entertainment, focus on technology, welcome to pay attention to Qger, we witness growth together!

What is prototype mode?

  官方解释: cloning of an existing object instead of creating new one and can also be customized as per the requirement.(克隆一个现有对象来代替新建一个对象,并且可以按定制要求克隆)
    • Popular explanation: By creating a new prototype object (which implements an abstract class, interface with a cloned interface) that indicates the type to be created, the cloned interface that invokes the object itself creates more of the same type of object.

Why use prototype mode?
1, simplify the process of object creation, using existing objects directly invoke the Clone method to implement the object creation.
2, reduce the needs of sub-categories.
3, customers do not need to know what type of object will be created.
4. You can create and destroy objects at run time.
How do I use prototype mode?
The prototype schema UML diagram is as follows:

    • Caller: Invokes the Clone method of the prototype object to get (a large number of) the same type object, if one is already in the prototype object.
    • Prototype interface: An abstract class (interface) that defines a clone interface.
    • Concrete Prototype class: Each prototype class implements (inherits) the interface and implements the Clone (clone) method of the interface. The clone here must be deeply cloned, that is, in some reference objects (Java references, C + + pointers), you must copy all the members referred to by the reference object, instead of just copying the referenced address again.

      Here is the deep copy, in order to let the students do not understand the concept of understanding, then to science and a deep copy of the concept of shallow copy.

    • Shallow copy: When cloning a class object, when the class has a reference member object, only the address of the referenced object in the object class is copied to the new object, so the application member object of the new object is cloned to the same point as the original object.

    • Deep copy: When cloning a class object, the members that point to the reference member object inside the class are also copied one at a time to the newly created class object, and this procedure can be nested because the members that the reference member object points to may also have a reference member object, so when you implement the clone, It is important to consider whether the cloned object will appear in this nested process, and repeatedly note that if the nesting is too deep to cause the performance of the clones to be too poor, consider abandoning the use of prototype mode, which is generally rare.

      Prototype mode usage scenarios:

      • When a class needs to be instantiated at run time.
      • When creating an object is too complex and expensive.
      • When you want to keep the number of classes in your app minimal.
      • When the client (caller) does not want to know the creation and presentation of the class.

Application Examples:
Have you ever heard of a nano robot? Now there is a nano-robot that, as long as they have enough energy to replicate themselves infinitely, in many areas have a powerful role, then we now abstract the definition of a nano-robot prototype.

    1. The first must be: Prototype abstract class (interface), the interface has a clone method. In Java, object objects have a clone interface, which is a subclass of all class object declared as protected, Java itself, but the cloneable interface needs to be implemented before the Clone method can be called. Therefore, in Java prototype this interface can be directly omitted, directly with the concrete prototype object class implementation cloneable interface can be. Aside, the Clone method here is also declared as native, the meaning of the keyword in Java is that the implementation of the declared method is implemented using non-Java code, detailed details can be self-searching ha.
    2. Then define the specific prototype object class (Narorobot) and implement the prototype interface (here is cloneable), rewrite the Clone method, the implementation of the clone must be deep copy, here is not tangled with the implementation of the depth of the copy, then there will be related detailed introduction.
 Public  class Narorobot implements cloneable {    PrivateString ID;PrivateString name;//... Various attributes, which require deep replication for reference-type members     Public Narorobot(string ID, string name) { This. id = ID; This. name = name; }@Override    protectedObjectClone()throwsclonenotsupportedexception {///This assumes that the prototype class does not have a reference type object.         return Super. Clone (); }}
    1. Finally, in the case of an existing Narorobot, the Clone method of invoking object is used to realize the creation of large-scale nano-robot.
public  class  Client { Span class= "Hljs-keyword" >public static  void  main  (string[] args) {Narorobot Narorobot = new< /span> Narorobot (,  "robot" ); list<narorobot> narorobotlist = new  arraylist<> (); for  (int  i = 0 ; i < 100< /span>; ++i) {try  {narorobotlist.add (Narorobot.clone ()); } catch  (clonenotsupportedexception e) {e.printstacktrace (); } } }}
 PS:此处的原型接口也可不省略,在有多个同类的原型对象类时,可增加一个抽象类实现Cloneable接口,然后具体原型类再继承该类即可。

On design pattern: Prototype mode (Prototype pattern)

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.