C # design Pattern (6) -- Prototype Pattern)

Source: Internet
Author: User

I. Introduction

In software systems, when the process of creating a class instance is expensive or complex, and we need to create multiple such class instances, if we use the new operator to create such a class instance, it will increase the complexity of the class creation and consume more memory space, in this way, multiple identical class instance objects are allocated in the memory. If the factory mode is used to create such a system, the number of sub-classes increases as the product class increases, instead, the complexity of the system is increased. Therefore, it is not appropriate to use the factory mode to encapsulate the class creation process. However, the prototype mode can solve this problem well because each class instance is the same, when we need multiple identical class instances, we do not need to use the new operator to create the same class instance object every time. In this case, we generally want to create only one class instance object, if you need more such instances later, you can copy the original object to create them. In this way, you do not need to create multiple identical class instances in the memory, this reduces memory consumption and achieves the reuse of class instances. However, this idea is exactly how the prototype is implemented. The following describes the prototype design mode in the design mode.

II. A detailed introduction to the prototype mode

In real life, there are also many examples of prototype design patterns ,. The specific implementation code is as follows:

      Main(            MonkeyKingPrototype prototypeMonkeyKing =  ConcretePrototype(            MonkeyKingPrototype cloneMonkeyKing = prototypeMonkeyKing.Clone() +            MonkeyKingPrototype cloneMonkeyKing2 = prototypeMonkeyKing.Clone()  +               Id { ;  MonkeyKingPrototype(.Id =                     ConcretePrototype(                                         (MonkeyKingPrototype)

The running result of the above prototype mode is (from the running result, we can see that the ID attributes of the created two copy objects are the same as those of the prototype Object ID ):

The shortest COPY method implemented by the code above,

After introducing the implementation code of the prototype mode, let's look at the class diagram of the prototype mode and use the class diagram to clarify the relationship between classes in the prototype mode implementation. The specific class diagram is as follows:

Iii. Advantages and Disadvantages of Prototype

The prototype has the following advantages:

Disadvantages of the prototype:

IV. Implementation of the. NET Model

In. NET can easily implement the prototype mode by implementing the Clone () method in the ICloneable interface (). If we want our custom class to have the Clone function, first, define the class inheritance interface and ICloneable interface and implement the Clone method. Shows the classes that implement the prototype mode in. NET (only part is truncated in the figure and can be viewed using Reflector decompilation tool ):

 

V. Summary

The introduction to the prototype mode is over. In the prototype mode, a prototype object is used to specify the type of the object to be created, then, use the method of copying this prototype object to create more objects of the same type ,.

All source code in this topic: design pattern prototype

Related Article

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.