Prototype of C # design pattern (ProtoType)

Source: Internet
Author: User
Design 1. Why do you use prototypes? The benefits of using prototypes

For example, we have a toolbar button, a New button, which is an instance of ToolBarButton, it has a length, a width attribute, and so on, and the property is assigned a value.

Now we're going to add a save button, which is also an instance of ToolBarButton, which has a length, width, and so on, but it's not assigned a value, and it's the same value as the New button.

If we don't use a prototype design pattern, we may be able to reassign the value again. If we use the prototype design pattern, we can take the new button as the prototype of the Save button. That way, you don't have to assign a value to the Save button, which has the same default length and width as the new button.





2. How to use Prototypes in C #?

Because an instance of a class is a reference type, you can use the Clone method only if you want to use the data from an instance in the original class.

The Clone method is divided into deep clone and shallow clone

The method of shallow clone is provided in C #, that is MemberwiseClone ()





Examples of C # shallow clones:

Using System;







Namespace Designpattern



{



public class Toolbarbutton:icloneable//Implementing 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 ()



{



return this. MemberwiseClone (); Returns the shallow Clone method ¨



}



}







public class Test



{



public void TestMethod ()



{



ToolBarButton mtb_newbutton=new ToolBarButton ();



mtb_newbutton.width=60;



mtb_newbutton.height=30;







ToolBarButton mtb_savebutton=new ToolBarButton ();



mtb_savebutton= (ToolBarButton) Mtb_newbutton.clone ();



Then Mtb_savebutton has a value of width and height.



}



}



}







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.