Factory Mode Implemented in. net

Source: Internet
Author: User

Problem:
There is a page for normal list display and chart display.
Solution:
Define two user controls: chartlist. ascx and datagridlist. ascx.
Abstract A class factory, such as abstractfactory.

// The chartlist. ascx and datagridlist. ascx control classes are both derived classes of system. Web. UI. usercontrol and inherit an iview interface. There is only one method, bindlist ()
// The example is very simple, and the interface can be expanded...
Let's take a look at how abstractfactory provides us with the corresponding "product"
 

Public class abstractfactory
{
Public Enum viewtype
{
Chart, DataGrid

}
Private iview _ iview;
Public iview
{
Get {return _ iview ;}
}
Private system. Web. UI. webcontrols. placeholder _ placeholder = new system. Web. UI. webcontrols. placeholder ();
Public System. Web. UI. webcontrols. placeholder myholder
{
Get {return _ placeholder ;}
}

Public abstractfactory (INT viewtype)
{
Switch (viewtype)
{
Case (INT) viewtype. chart:
Chartlist c = new chartlist ();
_ Iview = C;
_ Placeholder. Controls. Add (C );
Break;
Case (INT) viewtype. DataGrid:
Datagridlist d = new datagridlist ();
_ Iview = D;
_ Placeholder. Controls. Add (d );
Break;
}
}

}

// Defined interface
Public interface iview
{
Void bindlist ();

}
// The specific code of datagridlist and chartlist is not provided here.

Then complete the final work in a page.

Public class documentinfo: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. placeholder placeholder1;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1;
 
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Dropdownlist1.items. Add (New listitem (abstractfactory. viewtype. Chart. tostring (), abstractfactory. viewtype. Chart. tostring ()));
Dropdownlist1.items. Add (New listitem (abstractfactory. viewtype. DataGrid. tostring (), abstractfactory. viewtype. DataGrid. tostring ()));

Abstractfactory a1 = new abstractfactory (convert. toint32 (dropdownlist1.selectedvalue ));
Placeholder1 = a1.myholder;
A1.iview. bindlist ();
}
}

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.