A subtle foreach mode expressed in C #

Source: Internet
Author: User

Public class factory
{
Public Virtual void free ()
{
Console. writeline ("factory. Free ");
}
Public Virtual void Init ()
{
Console. writeline ("factory. init ");
}
Public Virtual void execute ()
{
Console. writeline ("factory. Execute ");
}
}
Public class factory1: Factory
{
Public override void free ()
{
Base. Free ();
Console. writeline ("factory1.free ");
}
Public override void Init ()
{
Base. INIT ();
Console. writeline ("factory1.init ");
}
}
Public class factory2: Factory
{
Public override void free ()
{
Base. Free ();
Console. writeline ("factory2.free ");

}
Public override void Init ()
{
Base. INIT ();
Console. writeline ("factory2.init ");
}
}

Public class Server
{
Public void freeserver ()
{
Manager. foreachfactory (this, new manager. facotrypro (factoryfree ));
}
Public void initserver ()
{
Manager. foreachfactory (this, new manager. facotrypro (factoryinit ));
}
Public void execute ()
{
Manager. foreachfactory (this, new manager. facotrypro (factoryexecte ));
}
Protected void factoryexecte (factory)
{
Factory. Execute ();
}
Protected void factoryfree (factory)
{
Factory. Free ();
}
Protected void factoryinit (factory)
{
Factory. INIT ();
}
}
Public class Manager
{
Public Delegate void facotrypro (factory );
Public static factory [] getfactories () {return new factory [] {New factory1 (), new factory2 ()};}
Public static void foreachfactory (server, facotrypro factorypro)
{
Factory [] factories = getfactories ();
Foreach (factory in factories)
{
Factorypro (factory );
}
}
}
Class Application
{
[Stathread]
Static void main (string [] ARGs)
{
Server curserver = new server ();
Console. writeline ("begin server init ...");
Curserver. initserver ();
Console. writeline ("execute ...");
Curserver. Execute ();
Console. writeline ("begin server free ...");
Curserver. freeserver ();
Console. Read ();
}
}

// Output results

 

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.