1. Define an interface: human, which can run such an action (probably so)
public interface IHuman { void Run(); }
2. Come to a man and a woman to let them inherit from humans. Of course, you have to implement the run () method.
public class Woman : IHuman { public Woman() { Console.WriteLine("The girl fall in love"); } public void Run() { Console.WriteLine("There is a girl in the running"); } } public class Man : IHuman { public Man() { Console.WriteLine("The guy fall in love"); } public void Run() { Console.WriteLine("A guy with the girl behind"); } }
3. The following is a processing factory template.
Public interface ifacloud {// <summary> // declare a getmarried to return the ihuman instance /// </Summary> /// <returns> return the ihuman instance </returns> ihuman getmarried ();} public class womanfactory: ifacloud {public ihuman getmarried () {return New Woman () ;}} public class manfactory: ifacloud {public ihuman getmarried () {return New Man ();}}
4. Write a method to generate the corresponding processing factory
Public class humanfactory {public static ifacloud createfacory (string factorytype) {type T = type. getType (factorytype); Return activator. createinstance (T) as ifacloud; // another method is used to create an object factory object assembly. load (Assembly name ). createinstance (namespace. class Name) // ifactory carfactory = system. reflection. assembly. load ("leleapplication1 "). createinstance (factorytype) as ifacloud; // return carfactory ;}}
5. test:
Class program {static void main (string [] ARGs) {# region demo1 // leleapplication1.manfactory can be written to the configuration file. Here, ifactory humfactory = humanfactory. createfacory ("leleapplication1.manfactory"); ihuman Hyman = humfactory. getmarried (); Hyman. run (); console. readkey (); # endregion }}
Effect: