首页 > 代码库 > 设计模式之抽象工厂模式

设计模式之抽象工厂模式

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;using System.Reflection;//抽象工厂方法namespace Abstract_Dactory{    /// <summary>    /// 客户端测试类    /// </summary>    class Program    {        static void Main(string[] args)        {            //使用抽象层定义            SkinFactory factory;            Button bt;            TextFiled tf;            ComBox cb;            //读取配置文件            string factoryType = ConfigurationManager.AppSettings["factory"];            //反射生成对象            factory = (SkinFactory)Assembly.Load("Abstract_Dactory").CreateInstance(factoryType);            bt = factory.CreateButton();            tf = factory.CReateTextFiled();            cb = factory.CreateComBox();            bt.Display();            tf.Display();            cb.Display();            Console.ReadKey();        }    }    /// <summary>    /// 按钮接口,充当抽象产品    /// </summary>    internal interface Button    {        void Display();    }    /// <summary>    ///  Spring按钮类充当具体产品    /// </summary>    class SpringButton:Button    {        public void Display()        {            Console.WriteLine("显示浅绿色按钮");        }    }    /// <summary>    ///  Summer按钮类充当具体产品    /// </summary>    class SummerButton : Button    {        public void Display()        {            Console.WriteLine("显示浅蓝色按钮");        }    }    /// <summary>    /// 文本框接口类,充当抽象产品    /// </summary>    interface TextFiled    {        void Display();    }    /// <summary>    /// Spring文本框充当具体产品    /// </summary>    class SpringTextFiled : TextFiled    {        public void Display()        {            Console.WriteLine("显示浅绿色文本框");        }    }    /// <summary>    /// Summer文本框充当具体产品    /// </summary>    class SummerTextFiled : TextFiled    {        public void Display()        {            Console.WriteLine("显示浅蓝色文本框");        }    }    /// <summary>    /// 组合框接口,充当抽象产品类    /// </summary>    interface ComBox    {        void Display();    }    /// <summary>    /// Spring组合框,充当具体产品    /// </summary>    class SpringComBox : ComBox    {        public void Display()        {           Console.WriteLine("显示浅绿色组合框");        }    }    /// <summary>    /// Summer组合框,充当具体产品    /// </summary>    class SummerComBox : ComBox    {        public void Display()        {            Console.WriteLine("显示浅蓝色组合框");        }    }    /// <summary>    /// 界面工厂接口,充当抽象工厂    /// </summary>    interface SkinFactory    {        Button CreateButton();        TextFiled CReateTextFiled();        ComBox CreateComBox();    }    /// <summary>    /// Spring皮肤工厂,充当具体工厂    /// </summary>    class SpringSkinFactory : SkinFactory    {        public Button CreateButton()        {            return new SpringButton();        }        public TextFiled CReateTextFiled()        {           return new SpringTextFiled();        }        public ComBox CreateComBox()        {            return new SpringComBox();        }    }    /// <summary>    /// Summer皮肤工厂,充当具体工厂    /// </summary>    class SummerSkinFactory : SkinFactory    {        public Button CreateButton()        {            return new SummerButton();        }        public TextFiled CReateTextFiled()        {            return new SummerTextFiled();        }        public ComBox CreateComBox()        {           return new SummerComBox();        }    }}

App.config

<?xml version="1.0" encoding="utf-8"?><configuration>  <appSettings>    <add key="factory" value="Abstract_Dactory.SummerSkinFactory"/>  </appSettings></configuration>

总结:

1.更改皮肤只需修改配置文件

2.添加新皮肤只需添加新的具体工厂与新的一族组件原有的代码不需要修改,符合开闭原则

设计模式之抽象工厂模式