抽象工厂模式(Abstract Factory Pattern)-创建型模式第二篇

解决的问题:

  1. 如何创建一个类,使其能够独立于它所需要的对象
  2. 如何让相关的产品对象能够一同被创建

怎么去解决:

  1. 将一系列具体对象的创建封装在单独的工厂对象里,你需要定义这个工厂的接口,然后实现这个抽象的工厂
  2. 客户端上下文在调用时,通过工厂去创建抽象的产品从而代替直接创建具体的产品对象

定义:

我想抽象工厂的核心定义是提供一个接口,用于创建一组依赖的对象(接口),并且无需指定具体的实现对象

具体的工厂决定了怎样的一系列具体对象会被创建,方法所返回的将是一个抽象的产品接口类型。在运行时上下文中,客户端将依赖于这个抽象的产品接口指针,而不用关心具体的实现提供者是什么。

UML:

实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactory
{
    public interface IButton
    {
    }

    public class FerricButton : IButton
    {
    }

    public class PlasticButton : IButton
    {
    }

    public interface IDisplayPanel
    {
    }

    public class LCDDisplayPanel : IDisplayPanel
    {
    }

    public class LEDDisplayPanel : IDisplayPanel
    {
    }

    public interface IDisplayerFactory
    {
        IButton CreateButton();

        IDisplayPanel CreateDisplayPanel();
    }

    public class HighendDisplayerFactory : IDisplayerFactory
    {
        public IButton CreateButton()
        {
            return new FerricButton();
        }

        public IDisplayPanel CreateDisplayPanel()
        {
            return new LEDDisplayPanel();
        }
    }

    public class LowendDisplayerFactory : IDisplayerFactory
    {
        public IButton CreateButton()
        {
            return new PlasticButton();
        }

        public IDisplayPanel CreateDisplayPanel()
        {
            return new LCDDisplayPanel();
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            DoWork();
        }

        private static void DoWork()
        {
            IDisplayerFactory displayerFactory;
            while (true)
            {
                Console.WriteLine("Please select the type of displayer you want.");
                Console.WriteLine($"A: LED{Environment.NewLine}B: LCD");
                var inputKey = Console.ReadKey();
                Console.WriteLine();
                switch (inputKey.KeyChar.ToString().ToUpper())
                {
                    case "A":
                        displayerFactory = new HighendDisplayerFactory();
                        Console.WriteLine($"Producing button -- {displayerFactory.CreateButton()}");
                        Console.WriteLine($"Producing display panel -- {displayerFactory.CreateDisplayPanel()}");

                        break;

                    case "B":
                        displayerFactory = new LowendDisplayerFactory();
                        Console.WriteLine($"Producing button -- {displayerFactory.CreateButton()}");
                        Console.WriteLine($"Producing display panel -- {displayerFactory.CreateDisplayPanel()}");
                        break;

                    case "X":
                        return;

                    default:
                        Console.WriteLine("You enter a wrong character and enter 'x' to exit the program");
                        break;
                }
            }
        }
    }

}

输出:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.