首页 > 代码库 > 简单工厂模式-2015-1-7

简单工厂模式-2015-1-7

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(); break;                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());        }    }    }

  

 /*             * 输出结果            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>这篇文章简单罗列了在项目开发中用到的类、接口、抽象类,三者组合在一起构成了简单的工厂模式</div>

简单工厂模式-2015-1-7