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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 走到天亮

设计模式之“适配器模式” - 走到天亮 设计模式之“门面模式” 设计模式之“单例模式” - 走到天亮 设计模式之“代理模式” 设计模式之“策略模式” - 走到天亮 《C# to IL》第三章 选择和循环 《C# to IL》第二章 IL基础 - 走到天亮 《C# to IL》第一章 IL入门 淘宝下单高并发解决方案(转载) java linux 配置环境 - 走到天亮 Spring Aop之(二)--Aop 切面声明和通知 Spring aop Spring RegexpMethodPointcutAdvisor和NameMatchMethodPointcutAdvisor Spring BeanNameAutoProxyCreator 与 ProxyFactoryBean - 走到天亮 CentOS的IP配置专题 Spring Bean属性绑定Bean返回值 - 走到天亮 【阿里的感悟】质量该如何做? .(转载) Ubuntu开机自动启动script(2) Ubuntu开机自动启动Script - 走到天亮
设计模式之“抽象工厂模式”
走到天亮 · 2013-07-22 · via 博客园 - 走到天亮

抽象工厂模式的优点

        抽象工厂模式除了具有工厂方法模式的优点外,最主要的优点就是可以在类的内部对产品族进行约束。所谓的产品族,一般或多或少的都存在一定的关联,抽象工厂模式就可以在类内部对产品族的关联关系进行定义和描述,而不必专门引入一个新的类来进行管理。

抽象工厂模式的缺点

       产品族的扩展将是一件十分费力的事情,假如产品族中需要增加一个新的产品,则几乎所有的工厂类都需要进行修改。所以使用抽象工厂模式时,对产品等级结构的划分是非常重要的。

适用场景

       当需要创建的对象是一系列相互关联或相互依赖的产品族时,便可以使用抽象工厂模式。说的更明白一点,就是一个继承体系中,如果存在着多个等级结构(即存在 着多个抽象类),并且分属各个等级结构中的实现类之间存在着一定的关联或者约束,就可以使用抽象工厂模式。假如各个等级结构中的实现类之间不存在关联或约 束,则使用多个独立的工厂来对产品进行创建,则更合适一点。

类图:

实例:

Linux 文本框于按钮

windows 文本框于按钮

 #region 抽象工厂模式
    public abstract class AbstractFactory {
        public Food getFood();
        public TableWare getTool();
    }
    public class KetchenFactory : AbstractFactory
    {

        public override Food getFood()
        {
            return new Milk(); 
        }

        public override TableWare getTool()
        {
            return new Spoon();
        }
    }
    public abstract class Food
    {
        public abstract String getEatable();
    }
    public abstract class TableWare
    {
        public abstract String getTool();
    }

    public class Spoon : TableWare
    {
        public String getTool()
        {
            return "spoon";
        }
    }
    public class Milk : Food
    {
        public String getEatable()
        {
            return "milk";
        }
    }
    
    #endregion