• 抽象工厂模式


    抽象工厂模式:提供一个接口,让该接口负责创建一系列相关或者相互依赖的对象,无需指定它们具体的类。{创建型模式}

    抽象工厂模式以一种倾斜的方式支持增加,可以很容易的增加产品族,但是不能为增加产品等级结构提供方便。[如下图,我们可以很容易的纵向的系统,但是添加横向的控件确不易]

    实现代码如下:

    namespace AbstractN

    {

        //AbstractFactory

        abstract public class ViewFactory

        {

            abstract public Button makeButton();

            abstract public TextBox makeTextBox();

        }

     

        //ConcreteFactory1

        public class WindowsView : ViewFactory

        {

            public override Button makeButton()

            {

                return new WindowsXPButton();

            }

     

            public override TextBox makeTextBox()

            {

                return new WindowsXPTextBox();

            }

        }

        //ConcreteFactory2

        public class UnixView : ViewFactory

        {

            public override Button makeButton()

            {

                return new UnixAixButton();

            }

     

            public override TextBox makeTextBox()

            {

                return new UnixAixTextBox();

            }

        }

     

        //AbstractProductA

        abstract public class Button

        {

            abstract public void ButtonName(string name);

            abstract public void ButtonSize();

        }

        //AbstractProductB

        abstract public class TextBox

        {

            abstract public double TextboxLen();

        }

     

        //ProductA1

        public class WindowsXPButton : Button

        {

            public override void ButtonName(string name)

            {

                Console.WriteLine("Xp系统的按钮默认名:" + name);

            }

            public override void ButtonSize()

            {

                Console.WriteLine("按钮尺寸可调整!");

            }

        }

        //ProductA2

        public class UnixAixButton : Button

        {

            public override void ButtonName(string name)

            {

                Console.WriteLine("AIX系统的按钮默认名:" + name);

            }

     

            public override void ButtonSize()

            {

                Console.WriteLine("按钮尺寸不调整!");

            }

        }

     

        //ProductB1

        public class WindowsXPTextBox : TextBox

        {

            public override double TextboxLen()

            {

                return 12.9;

            }

        }

        //ProductB2

        public class UnixAixTextBox : TextBox

        {

            public override double TextboxLen()

            {

                return 19.2;

            }

        }

     

        //Client端----------------------------

        public class Client

        {

            Button button;

            TextBox textbox;

            public Client(ViewFactory system)

            {

                button = system.makeButton();

                textbox = system.makeTextBox();

            }

     

            public void showAllView()

            {

                button.ButtonName("按钮名称");

                button.ButtonSize();

                Console.WriteLine(textbox.TextboxLen().ToString());

            }

        }

     

        class Program

        {

            static void Main(string[] args)

            {

                ViewFactory v = new WindowsView();

                Client client = new Client(v);

                client.showAllView();

     

                ViewFactory v1 = new UnixView();

                Client c1 = new Client(v1);

                c1.showAllView();

            }

        }

    }

    我们以后在增加/替换系统的时候,可以增加一些DLL,切换相关的引用;

    但是在增加控件的时候,类库项目和Client端的变动都较大。

  • 相关阅读:
    keepalived.conf配置说明
    监控端口是否开放,端口未开放关闭虚拟ip,端口开放启动虚拟IP
    lvs UDP端口负载均衡配置
    keepalived自动安装脚本
    Keepalived+LVS实现高可用负载均衡双主模式
    cookie和session的区别
    jmeter 关联
    浏览器缓存详解:expires,cache-control,last-modified,etag详细说明
    Session、Cookie、Cache、Token分别是什么及区别
    获取MyBatis
  • 原文地址:https://www.cnblogs.com/hometown/p/3204217.html
Copyright © 2020-2023  润新知