Prototype mode (Prototype pattern)

Source: Internet
Author: User

First, prototype (Prototype) mode

By giving a prototype object to indicate the type of object to be created, and then creating more homogeneous objects with the method of copying the prototype object. Prototype mode allows an object to create another customizable object without having to know the details of how to create it at all.

II. structure of the prototype model

Iii. detailed introduction to the prototype model

In real life, there are many examples of prototype design patterns, for example, the process of cell division, the mitosis of a cell produces two identical cells, and the Monkey King in the journey to the sun after the ability to change and Naruto Naruto in the shadow of the Hidden Shen. The following is the example of the Monkey King to demonstrate the implementation of the prototype model. The specific implementation code is as follows:

///Naruto's Shadow and Sun Wukong's change are prototype patterns    classClient {Static voidMain (string[] args) {            //Monkey King prototypeMonkeykingprototype prototypemonkeyking =NewConcreteprototype ("monkeyking"); //Change oneMonkeykingprototype clonemonkeyking = Prototypemonkeyking.clone () asConcreteprototype; Console.WriteLine ("cloned1:\t"+clonemonkeyking.id); //Change TwoMonkeykingprototype cloneMonkeyKing2 = Prototypemonkeyking.clone () asConcreteprototype; Console.WriteLine ("cloned2:\t"+clonemonkeyking2.id);        Console.ReadLine (); }    }    /// <summary>    ///Monkey King prototype/// </summary>     Public  Abstract classMonkeykingprototype { Public stringId {Get;Set; }  PublicMonkeykingprototype (stringID) { This. Id =ID; }        //cloning method, that is, Bajau said "change"         Public AbstractMonkeykingprototype Clone (); }    /// <summary>    ///Create a concrete prototype/// </summary>     Public classConcreteprototype:monkeykingprototype { PublicConcreteprototype (stringID):Base(ID) {}/// <summary>        ///Shallow Copy/// </summary>        /// <returns></returns>         Public OverrideMonkeykingprototype Clone () {//the call MemberwiseClone method implements a shallow copy, plus a deep copy            return(Monkeykingprototype) This.        MemberwiseClone (); }    }

The result of the above prototype mode is (as can be seen from the running result, the id attribute of the two copied objects created is the same as the prototype Object ID attribute):

The above code implements a shallow copy of the method, the shallow copy refers to when the object's field value is copied, the field refers to the object will not be copied. For example, if an object has a field that points to a string, and we make a shallow copy of the object, both objects will reference the same string, and the deep copy is a copy of the object referenced by the field in the object instance, and if an object has a field that points to a string, And we have a deep copy of the object, we will create an object and a new string, and the new object will reference the new string. That is, the new object and the original object that are created by the deep copy do not share anything, and changing one object has no effect on another object, while a new object that executes a shallow copy creates a shared member with the original object, changes one object, and the other member of the object changes.

After introducing the implementation code of the prototype pattern, the following is a look at the class diagram of the prototype pattern and the class diagram to clarify the relationship between the classes in the prototype pattern implementation. The specific class diagram is as follows:

Iv. advantages and disadvantages of prototype model

The advantages of the prototype model are:

    1. Prototype mode hides the complexity of creating a new instance to the customer
    2. Prototype mode allows for dynamically increasing or fewer product classes.
    3. Prototype mode simplifies the creation of an instance, and the factory method pattern needs to have a hierarchical structure that is the same as the product class hierarchy, which is not required for prototype mode.
    4. Product classes do not need to determine the grade structure of the product beforehand, because the prototype pattern is suitable for any hierarchy structure

The disadvantages of the prototype model are:

    1. Each class must be equipped with a clone method
    2. Having a cloning method requires a holistic view of the functionality of the class, which is not difficult for new classes, but not necessarily easy for existing classes, especially when a class reference does not support serialization of indirect objects, or when a reference contains a looping structure.
Five. Realization of net Zhongyuan model

In. NET it is easy to implement the prototype pattern by implementing the ICloneable interface (the interface is a prototype that provides a clone method, equivalent to the Monkeykingprototype abstract class in the above code) in the Clone () method. If we want our custom classes to have cloning capabilities, first define the class inheritance with the ICloneable interface and implement the Clone method. The class that implements the prototype pattern in. NET is shown (only part of the figure is truncated and can be viewed with the Reflector Anti-compilation tool):

Summarize

Here is the end of the introduction of prototype mode, which uses a prototype object to indicate the type of object to be created, and then uses the method of copying the prototype object to create more of the same type object, which is very similar to the implementation of the factory method pattern. The Clone method in prototype mode is similar to the factory method in the factory method pattern, but the factory method of the factory method mode is to recreate a new object (equivalent to the deep copy implementation of the prototype pattern) with the operator new. The prototype mode is to copy the original object by calling the MemberwiseClone method, which is also the difference between the factory method and the prototype model.

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.