工厂方法模式

定义

定义了一个创建对象的接口,由子类决定实例化哪一个对象,工厂方法让类把实例化推迟到子类。

UML类图

实现

案例:提供一个画图程序,支持柱状图和饼图,根据不同的用户类型(免费/付费),显示不同的图形版本,付费用户会得到更丰富的图形显示。
为了能够简单描述工厂方法模式,应用以控制台应用的形式编写

定义接口

Creater

    public interface IChartCreater
    {
        IChart GetChart(string type,string title);
    }

Product

    public interface IChart
    {
        string Title { get; set; }
        void GenerateChart();
    }

定义ConcreteProduct

免费类型

    public class BarChart : IChart
    {
        public string Title { get; set ; }

        public BarChart(string title)
        {
            Title = title;
        }

        public void GenerateChart()
        {
            Console.WriteLine($"Drawing a Bar chart with title {Title}");
        }
    }
    public class PieChart : IChart
    {
        public string Title { get; set; }

        public PieChart(string title)
        {
            Title = title;
        }

        public void GenerateChart()
        {
            Console.WriteLine($"Drawing a Pie chart with title {Title}");
        }
    }

付费类型

    public class RichBarChart : IChart
    {
        public string Title { get; set; }

        public RichBarChart(string title)
        {
            Title = title;
        }

        public void GenerateChart()
        {
            Console.WriteLine($"Drawing a Rich Bar chart with title {Title}");
        }
    }
    public class RichPieChart : IChart
    {
        public string Title { get; set; }

        public RichPieChart(string title)
        {
            Title = title;
        }

        public void GenerateChart()
        {
            Console.WriteLine($"Drawing a Rich Pie chart with title {Title}");
        }
    } 

定义ConcreteCreater

免费类型

    public class ChartCreaterFree : IChartCreater
    {
        public IChart GetChart(string type, string title)
        {
            if (type=="bar")
            {
                return new BarChart(title);
            }
            if (type=="pie")
            {
                return new PieChart(title);
            }
            return null;
        }
    }

付费类型

    public class ChartCreaterPaid : IChartCreater
    {
        public IChart GetChart(string type, string title)
        {
            if (type == "bar")
            {
                return new RichBarChart(title);
            }
            if (type == "pie")
            {
                return new RichPieChart(title);
            }
            return null;
        }
    }

测试

    class Program
    {
        static void Main(string[] args)
        {
            IChartCreater createrFree = new ChartCreaterFree();
            IChartCreater createrPaid = new ChartCreaterPaid();

            IChart bar = createrFree.GetChart("bar", "a free bar chart");
            IChart pie = createrPaid.GetChart("pie", "a paid pie chart");

            bar.GenerateChart();
            pie.GenerateChart();
            Console.ReadLine();
        }
    }

如果Creater中有别的方法需要使用FactoryMethod中返回的对象,则可以将Creater设计为抽象类,在其他需要的方法中调用FactoryMethod方法,并操作其产生的对象,这种结构类似于模板模式。

posted @ 2020-03-30 12:02  青玄鸟  阅读(168)  评论(0编辑  收藏  举报