反射使抽象工厂模式的面向对象更上一层楼(知识点:依赖注入,反射,多态性,操作XML文件等)

简介:
namespace test
{
    #region 使用反射实现的抽象工厂
    internal static class ReflectionFactory
    {
        private static String _windowType;
        private static String _styleType;
        static ReflectionFactory()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"f:/test.xml");
            XmlNode xmlNode = xmlDoc.ChildNodes[0];
            _windowType = xmlNode.ChildNodes[0].ChildNodes[0].Value;
            _styleType = xmlNode.ChildNodes[1].ChildNodes[0].Value;
        }
 
        public static IWindow MakeWindow()
        {
            return Assembly.Load("test").CreateInstance("test." + _windowType) as IWindow;
        }
 
        public static IStyle MakeStyle()
        {
            return Assembly.Load("test").CreateInstance("test." + _styleType) as IStyle;
        }
 
    }
    #endregion
 
    #region 操作接口规范
    internal interface IStyle
    {
        String ShowInfo();
    }
 
    internal interface IWindow
    {
        String ShowInfo();
    }
 
    #endregion
 
    #region 页面风格
    internal sealed class WindowsStyle : IStyle
    {
        public String Description { get; private set; }
 
        public WindowsStyle()
        {
            this.Description = "Windows风格按钮";
        }
 
        public String ShowInfo()
        {
            return this.Description;
        }
    }
 
    internal sealed class MacStyle : IStyle
    {
        public String Description { get; private set; }
 
        public MacStyle()
        {
            this.Description = "Mac风格按钮";
        }
 
        public String ShowInfo()
        {
            return this.Description;
        }
    }
    #endregion
 
    #region 窗口风格
    internal sealed class WindowsWindow : IWindow
    {
        public String Description { get; private set; }
 
        public WindowsWindow()
        {
            this.Description = "Windows风格窗口";
        }
 
        public String ShowInfo()
        {
            return this.Description;
        }
    }
 
    internal sealed class MacWindow : IWindow
    {
        public String Description { get; private set; }
 
        public MacWindow()
        {
            this.Description = "Mac风格窗口";
        }
 
        public String ShowInfo()
        {
            return this.Description;
        }
    }
    #endregion
 
    #region 工厂接口规范
    internal interface IFactory
    {
        IWindow MakeWindow();
 
        IStyle MakeStyle();
    }
    #endregion
 
    #region 具体工厂的实现
    internal sealed class WindowsFactory : IFactory
    {
        public IWindow MakeWindow()
        {
            return new WindowsWindow();
        }
 
        public IStyle MakeStyle()
        {
            return new WindowsStyle();
        }
 
    }
 
    internal sealed class MacFactory : IFactory
    {
        public IWindow MakeWindow()
        {
            return new MacWindow();
        }
 
        public IStyle MakeStyle()
        {
            return new MacStyle();
        }
 
    }
    #endregion
 
 
}

在主程序中这样去调用它:

            #region 通过学习接口与反射再次看看抽象工厂模式
            IWindow window = ReflectionFactory.MakeWindow();
            Console.WriteLine("创建 " + window.ShowInfo());
            IStyle style = ReflectionFactory.MakeStyle();
            Console.WriteLine("创建 " + style.ShowInfo());
  #endregion

XML配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<config>
  <window>MacWindow</window>
  <style>MacStyle</style>
</config>
,如需转载请自行联系原博主。
目录
相关文章
|
8天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
9 0
|
10天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
28天前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
29天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
1月前
|
Kubernetes Cloud Native Java
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
37 1
|
29天前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
10 0
|
25天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
14 1
|
28天前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0
|
1月前
|
XML 存储 JavaScript
深入学习 XML 解析器及 DOM 操作技术
所有主要的浏览器都内置了一个XML解析器,用于访问和操作XML XML 解析器 在访问XML文档之前,必须将其加载到XML DOM对象中 所有现代浏览器都有一个内置的XML解析器,可以将文本转换为XML DOM对象
72 0
|
1月前
|
SQL Java 数据库连接
Mybatis中Mapper.xml 文件使用注释遇到的坑
Mybatis中Mapper.xml 文件使用注释遇到的坑