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

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

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
 
 
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/2082612.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存