设计模式--简单工厂


简单的说,就是通过工厂创建对象,然后客户端进行调用


1.声明接口

namespace ConsoleApplication1
{
    public interface IRace
    {
        void ShowKing();
    }
}

2.实现接口

namespace ConsoleApplication1
{
    public class Human : IRace
    {
        public void ShowKing()
        {
            Console.WriteLine(" this is Human ShowKing");
        }
    }
}

namespace ConsoleApplication1
{
    public class NE : IRace
    {
        public void ShowKing()
        {
            Console.WriteLine(" this is NE ShowKing");
        }
    }
}


namespace ConsoleApplication1
{
    public class Orc : IRace
    {
        public void ShowKing()
        {
            Console.WriteLine(" this is Orc ShowKing");
        }
    }
}


3.Factory ,三种创建对象的方式

namespace ConsoleApplication1
{
    public class Factory
    {
        /// <summary>
        /// 第一种创建
        /// </summary>
        /// <param name="raceType"></param>
        /// <returns></returns>
        public static IRace CreateInstance(RaceType raceType)
        {
            switch (raceType)
            {
                case RaceType.Human:
                    return new Human();
                case RaceType.OrC:
                    return new Orc();
                case RaceType.NE:
                    return new NE();
                default:
                    throw new Exception("创建失败!");
            }
        }


        private static string RaceTypeConfig = System.Configuration.ConfigurationManager.AppSettings["RaceTypeConfig"];

        /// <summary>
        /// 第二种通过配置文件创建实例
        /// </summary>
        /// <param name="raceType"></param>
        /// <returns></returns>
        public static IRace CreateInstanceConfig()
        {
            RaceType raceType = (RaceType)Enum.Parse(typeof(RaceType), RaceTypeConfig);
            switch (raceType)
            {
                case RaceType.Human:
                    return new Human();
                case RaceType.OrC:
                    return new Orc();
                case RaceType.NE:
                    return new NE();
                default:
                    throw new Exception("创建失败!");
            }
        }

        private static string RaceTypeReflect = System.Configuration.ConfigurationManager.AppSettings["RaceTypeReflect"];

        /// <summary>
        /// 第三种通过反射创建实例,可扩展性极强
        /// </summary>
        /// <param name="raceType"></param>
        /// <returns></returns>
        public static IRace CreateInstanceReflect()
        {
            string assemblyName = RaceTypeReflect.Split(',')[0];
            string typeName = RaceTypeReflect.Split(',')[1];
            return (IRace)Activator.CreateInstance(assemblyName, typeName).Unwrap();
        }
    }

    public enum RaceType
    {
        Human,
        OrC,
        NE
    }
}

4.App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <!--靠配置文件改变生成的类 value 的值是具体的类 -->
    <add key="RaceTypeConfig" value="Human"/>
    <!--靠反射生成的对应的类 value 的值是具体的类 -->
    <!--<add key="RaceTypeReflect" value="ConsoleApplication1,ConsoleApplication1.NE"/>-->

    <!--靠反射生成的对应的类 value 的值是具体的类 -->
    <add key="RaceTypeReflect" value="TheFive,TheFive.Five"/>
  </appSettings>
</configuration>

5.client 调用

    public class Program
    {

        static void Main(string[] args)
        {
            IRace irace1 = Factory.CreateInstance(RaceType.Human);
            irace1.ShowKing();
            IRace irace2 = Factory.CreateInstance(RaceType.OrC);
            irace2.ShowKing();
            IRace irace3 = Factory.CreateInstance(RaceType.NE);
            irace3.ShowKing();

            Console.WriteLine("***********CreateInstanceConfig******************");
            IRace irace4 = Factory.CreateInstanceConfig();
            irace4.ShowKing();

            Console.WriteLine("***********CreateInstanceReflect******************");
            IRace irace5 = Factory.CreateInstanceReflect();
            irace5.ShowKing();



            Console.ReadKey();
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值