The prototype design pattern of C # design pattern (Prototype)

Source: Internet
Author: User
Tags abstract shallow copy

First, prototype (Prototype) mode

The purpose of the prototype pattern is to specify the type of object to be created by giving a prototype object, and then create more objects of the same type with the method of copying the prototype object.

From the means of Sun Dasan

The Monkey King in the battle with the Yellow wind strange, "to make a body outside the means: the hair to pull down a, with the mouth chew to smash, look on a spray, cry ' change ', change to have Bashing Walker, are the same dress, each hold an iron bar, put that strange surround in the air. In other words, the Monkey King can reproduce a lot of "body outside" according to his image.

The way of the body in the field of object-oriented design is called prototype (Prototype) mode.

C # support for prototyping patterns

In C #, we can easily implement the prototype pattern through the Clone () method. For any class, you must implement the ICloneable interface in C # as long as you want to support cloning. The ICloneable interface has a clone method that allows you to make a copy of the custom cloning method in the class. There are two ways to implement a clone: a shallow copy (shallow copy) and a deep copy (deep copy).

(The following is excerpted from: ". NET Framework Program design (revision), Li Jianzhong translation) shallow copy means that when the field value of an object is copied, the object referenced by the field is not copied. For example, if an object has a field that points to a string, and we make a shallow copy of the object, then two objects will reference the same string. A deep copy is a way to copy objects that are referenced by fields in an object instance. So if an object has a field that points to a string, and we make a deep copy of the object, we'll create a new object and a new string--the new object will refer to the new string. Note that after a deep copy is performed, the original object and the newly created object do not share anything, and changing one object has no effect on the other.

Second, the structure of the prototype model:

Customer (client) role: The Customer class presents a request to create an object.

Abstract prototype (Prototype) role: This is an abstract role, usually implemented by a C # interface or abstract class. This role gives the interfaces required for all the specific prototype classes. In C #, abstract prototyping roles typically implement the ICloneable interface.

Specific prototype (concrete Prototype) Role: the object being copied. This role requires implementing the interfaces required by the abstract prototype role.

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.