zoukankan      html  css  js  c++  java
  • 工厂方法模式

    定义

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

    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方法,并操作其产生的对象,这种结构类似于模板模式。

  • 相关阅读:
    [C#] 多线程总结(结合进度条)
    [Winform] DataGridView 中 DataGridViewComboBox 的可编辑
    [Tool] 仿博客园插入代码的 WLW 插件
    [C#] 获取打印机列表
    [Access] C# 通过 COM 组件访问 Access 文件
    [Excel] 打印设置
    [Q&A] 应用程序清单生成失败
    [Excel] Worksheet.PasteSpecial
    canvas裁剪图片,纯前端
    javascript将base64编码的图片数据转换为file并提交
  • 原文地址:https://www.cnblogs.com/Saints/p/12596951.html
Copyright © 2011-2022 走看看