反射 简单工厂模式

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            LeiFeng xueleifeng = new Undergraduate();
            xueleifeng.BuyRice();
            xueleifeng.Sweep();
            xueleifeng.Wash();
            LeiFeng student1 = new Undergraduate();
            student1.BuyRice();
            LeiFeng student2 = new Undergraduate();
            student2.Sweep();
            LeiFeng student3 = new Undergraduate();
            student3.Wash();
            //简单工厂模式
            LeiFeng studentA = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentA.BuyRice();
            LeiFeng studentB = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentB.Sweep();
            LeiFeng studentC = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentC.Wash();
            //工厂方法模式
            IFactory factory = new UndergraduateFactory();
            LeiFeng student = factory.CreateLeiFeng();
            student.BuyRice();
            student.Sweep();
            student.Wash();
            //简单工厂模式中反射应用
            LeiFeng l = SimpleFactory.CreateLeiFeng2("Undergraduate");
            l.BuyRice();
            l.Wash();
            Console.Read();
        }
    }
    //雷锋
    class LeiFeng
    {
        public void Sweep()
        {
            Console.WriteLine("扫地");
        }
        public void Wash()
        {
            Console.WriteLine("洗衣");
        }
        public void BuyRice()
        {
            Console.WriteLine("买米");
        }
    }
    //学雷锋的大学生
    class Undergraduate : LeiFeng
    { }
    //社区志愿者
    class Volunteer : LeiFeng
    { }
    //简单雷锋工厂
    class SimpleFactory
    {
        public static LeiFeng CreateLeiFeng(string type)
        {
            LeiFeng result = null;
            switch (type)
            {
                case "学雷锋的大学生":
                    result = new Undergraduate();
                    break;
                case "社区志愿者":
                    result = new Volunteer();
                    break;
            }
            return result;
        }
        public static LeiFeng CreateLeiFeng2(string type)
        {
            Assembly ass = Assembly.Load("ConsoleApplication2");
            Type t = ass.GetType("ConsoleApplication2." + type);
            object instance = Activator.CreateInstance(t, true);
            return instance as LeiFeng;
            //Assembly assembly = Assembly.Load("bgw.upay");
            //Type t = assembly.GetType("bgw.upay.Bank.Bank_" + argAcquireID);
            //Type ift = t.GetInterface("IBank", true);
            //if (ift == null)
            //{
            //    Utility.LogHelper.Debug("无法映射接口编号:" + argAcquireID);
            //    throw new GlbAppException(PrefixErrorCode.BGW + "0001");//, "收单机构编号为" + argAcquireID + "的银行接口不存在");
            //}
            //object instance = Activator.CreateInstance(t, true);
            //Utility.LogHelper.Debug("结束反射银行接口");
            //return instance as Bank.IBank;
                 
        }
    }
    //雷锋工厂
    interface IFactory
    {
        LeiFeng CreateLeiFeng();
    }
    //学雷锋的大学生工厂
    class UndergraduateFactory : IFactory
    {
        public LeiFeng CreateLeiFeng()
        {
            return new Undergraduate();
        }
    }
    //社区志愿者工厂
    class VolunteerFactory : IFactory
    {
        public LeiFeng CreateLeiFeng()
        {
            return new Volunteer();
        }
    }
}


知识共享许可协议
《反射 简单工厂模式》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     156.70ms