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

推荐订阅源

T
Tenable Blog
月光博客
月光博客
雷峰网
雷峰网
WordPress大学
WordPress大学
博客园 - 司徒正美
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security @ Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
爱范儿
爱范儿
W
WeLiveSecurity
J
Java Code Geeks
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
T
Threatpost
The Cloudflare Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
N
Netflix TechBlog - Medium
Latest news
Latest news
V2EX - 技术
V2EX - 技术
小众软件
小众软件
T
The Blog of Author Tim Ferriss
A
Arctic Wolf
B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
C
Check Point Blog
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
V
V2EX
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
Microsoft Security Blog
Microsoft Security Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
D
DataBreaches.Net
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
K
Kaspersky official blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - 青玄鸟

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