惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 基于接口隔离原则的依赖注入实现 HttpClient with Stream HttpClient partial update HttpClient 基本使用 值对象的封装 只读集合类型属性实现 适配器模式 模板模式 最少知识原则 单例模式 抽象工厂 简单工厂、工厂方法、抽象工厂 使用 Visual Studio Code创建和执行T-SQL
工厂方法模式
青玄鸟 · 2020-03-30 · via 博客园 - 青玄鸟

定义

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

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