博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
反射 简单工厂模式
阅读量:6857 次
发布时间:2019-06-26

本文共 3414 字,大约阅读时间需要 11 分钟。

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

 

 

转载地址:http://evjyl.baihongyu.com/

你可能感兴趣的文章
Entity Framwork one to one problem
查看>>
[转] Attach、Detach和DeleteObject
查看>>
[转] C# 获取程序运行目录
查看>>
【OpenCV学习】极坐标变换
查看>>
[Android Pro] InputStream.skip方法的思考
查看>>
判断页面加载
查看>>
8年前,《西班牙,我为你哭泣。》
查看>>
SVN:服务器资源删掉,本地添加时和删掉的名字同名出现One or more files are in a conflicted state....
查看>>
MySQL数据库遭到攻击篡改---使用备份和binlog进行数据恢复
查看>>
如何在Java中定义常量(Constant)
查看>>
Windows 8实用窍门系列:8.Windows 8 中Slider控件和ToggleSwitch控件
查看>>
在IIS 7.0中架设网站,并用VS2005来调试Web项目
查看>>
白话学习MVC(七)Action的执行一
查看>>
javaweb学习总结(四)——Http协议
查看>>
GridView实战二:使用ObjectDataSource数据源控件
查看>>
C# 视频监控系列(3):客户端——连接服务器并预览
查看>>
oracle表空间使用率统计查询
查看>>
virtualbox不能安装64位操作系统
查看>>
MyBatis Generator(MBG)Oracle使用说明 公共同义词 LONG数据类型
查看>>
ORA-00918:未明确定义列
查看>>