Using system;using system.collections.generic;using system.linq;using system.text;namespace ConsoleApplication1{ Class Program {static void Main (string[] args) {Acompany item = Companyfactory.createcompany (Enumcompany.car); if (item = = NULL) Console.WriteLine ("null"); else {item. Open (); Item. Close (); } Console.WriteLine ("****************************************"); Item = Companyfactory.createcompany (Enumcompany.car); if (item = = NULL) Console.WriteLine ("null"); else {item. Open (); Item. Close (); } console.readkey (); }} enum Enumcompany {car=0, boat=1, shoat=2, food=3,} class Companyfactory {public static Acompany Createcompany (Enumcompany item) {Acompany Company = NULL; Switch (item) {Case enumcompany.car:company= new Car (); Case enumcompany.boat:company= New Boat (); Break } return company; }} interface Icompany {bool Open (); BOOL Close (); } abstract class Acompany:icompany {public int Count = 0; public static int Flag = 0; public abstract bool Open (); public abstract bool Close (); public void Test () {} public abstract void Method (); } class Car:acompany {public override void Method () {Console.WriteLine (this. ToString ()); public override bool Open () {Console.WriteLine (this). ToString ()); count++; flag++; Console.WriteLine ("Count:{0}", Count); Console.WriteLine ("Flag:{0}", Flag); return true; }public override bool Close () {Console.WriteLine (this. ToString ()); count++; flag++; Console.WriteLine ("Count:{0}", Count); Console.WriteLine ("Flag:{0}", Flag); return true; }} class Boat:acompany {public override bool Open () {Console.WriteLine (this. ToString ()); return true; public override bool Close () {Console.WriteLine (this). ToString ()); return true; public override void Method () {Console.WriteLine (this). ToString ()); } } }
/ * * Output results consoleapplication1.car count:1 flag:1 consoleapplication1.car count:2 Flag:2 **************************************** consoleapplication1.car count:1 Flag:3 Consoleapplication1.car count:2 flag:4 * * *
<div> This article simply lists the classes, interfaces, and abstract classes that are used in project development, which together form a simple factory model </div>
Simple Factory mode-2015-1-7