C # ProtoType)

Source: Internet
Author: User

1. Why use a prototype? Benefits of Prototype
For example, we have a toolbar button and a new button, which is an instance of ToolbarButton. It has the length and width attributes, and all attributes are assigned values.
Now we need to add another save button, which is also an instance of ToolbarButton. It also has the length and width attributes, but it has not been assigned a value. Its value is the same as the value of the new button.
If we do not need a prototype design pattern, we may re-assign the value. If we use the prototype design mode, we can use the new button as the prototype of the Save button. In this case, you do not need to assign a value to the Save button. Its default length and width are the same as those of the new button.

2. How to use a prototype in C?
Because the instance of the class is a reference type, to use the data of the instance in the original class, you can only use the clone method.
The Clone methods are divided into deep clone and shortest clone.
The shortest clone method is provided in C #, that is, MemberwiseClone ()

C # example of shortest Clone:
Using System;

Namespace DesignPattern
{
Public class ToolbarButton: ICloneable // implement the Clone Interface
{

Private int _ Width;
Private int _ Height;

Public ToolbarButton ()
{
}

Public int Width
{
Get
{
Return _ Width;
}
Set
{
_ Width = value;
}
}

Public int Height
{
Get
{
Return _ Height;
}
Set
{
_ Height = value;
}
}


Public object Clone ()

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.