概述

在系统中往往会有这种需求,客户端会用到很多对象,而且根据需求变化很可能会切换成另外一套对象。抽象工厂模式可以提供一种封装机制来面对这种需求。

实践

物理模型:

皮肤主题:设计一个可以切换皮肤主题,切换为 黑色 和灰色样式,每个元素的样式都要单独定义,如Input的样式,button的样式,这些所有的元素样式组成一套皮肤主题。

根据模型,先抽象,每种元素相当于一种产品,如button

    /// <summary>
/// Button按钮样式
/// </summary>
public interface IButton
{
int Border { get; }
string BackColor { get; }
}

然后 具体实现它

    /// <summary>
/// 灰色按钮样式
/// </summary>
public class GrayButton : IButton
{
public GrayButton()
{
Border = ;
BackColor = "#ccc";
}
public int Border { get; set; }
public string BackColor { get; set; }
}
/// <summary>
/// 黑色按钮样式
/// </summary>
public class BlackButton : IButton
{
public BlackButton()
{
Border = ;
BackColor = "#fff";
}
public int Border { get; set; }
public string BackColor { get; set; }
}

上面是一种html元素,其它元素的样式类似

    /// <summary>
/// 文本框
/// </summary>
public interface IInput
{
int Border { get; }
}
/// <summary>
/// 灰色文本框样式
/// </summary>
public class GrayInput : IInput
{
public GrayInput() { Border = ; }
public int Border { get; set; }
}
/// <summary>
/// 黑色文本框样式
/// </summary>
public class BlackInput : IInput
{
public BlackInput() { Border = ; }
public int Border { get; set; }
}

产品抽象完后,接下来抽象工厂

    /// <summary>
/// 抽象工厂
/// </summary>
public interface IThemesFactory
{
IButton CreateButton();
IInput CreateInput();
}

具体主题样式工厂

    /// <summary>
/// 创建灰色样式工厂
/// </summary>
public class GrayFactory : IThemesFactory
{
public IButton CreateButton()
{
return new GrayButton();
} public IInput CreateInput()
{
return new GrayInput();
}
}

其它主题工厂类似

    /// <summary>
/// 黑色主题样式工厂
/// </summary>
public class BlackFactory : IThemesFactory
{
public IButton CreateButton()
{
return new BlackButton();
} public IInput CreateInput()
{
return new BlackInput();
}
}

客户端调用

    /// <summary>
/// 主题
/// </summary>
public class ThemesMain
{
public void Main()
{
//灰色主题工厂
IThemesFactory factory = new GrayFactory();
//按钮样式
IButton button = factory.CreateButton();
//文本框样式
IInput input = factory.CreateInput();
}
}

小结

抽象工厂适用于一系列的对象的创建和切换,如 数据库配置有 MsSql,MySql,每种数据库下的 查询User 都是不一样,可以抽象工厂,写两套,客户端方便切换。

抽象工厂优点:

  • 客户端Main 里 都是使用抽象产品如IButton 不依赖具体的产品创建,而统一由工厂方法创建;
  • 切换产品方便,只需要切换工厂,接下来的由工厂创建的产品也就切换了;
  • 易扩展,如后续加产品,只需在工厂里创建出来就行,客户端不用改。

缺点:

  • 代码量大,由于抽象有工厂接口,有产品接口;
  • 修改量大,如有修改现有的产品,可能从接口修改到具体,全改一遍。

[设计模式]第三回:抽象工厂模式(Abstract Factory)的更多相关文章

  1. .NET设计模式(2):1.2 抽象工厂模式(Abstract Factory)

    概述 抽象工厂模式(Abstract Factory)是所有形态的工厂模式中最为抽象和最具一般性的一种形态.抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式.抽象工厂模式可以向客户端提供一个接口 ...

  2. 设计模式之十一:抽象工厂模式(Abstract Factory)

    抽象工厂模式: 提供了一个创建一系列相关的或相互依赖的对象的接口而不须要详细指定它们的类型. Provide an interface for creating families of related ...

  3. PHP设计模式(三)抽象工厂模式(Abstract Factory)

    一.什么是抽象工厂模式 抽象工厂模式的用意为:给客户端提供一个接口,可以创建多个产品族中的产品对象 ,而且使用抽象工厂模式还要满足以下条件: 系统中有多个产品族,而系统一次只可能消费其中一族产品. 同 ...

  4. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  5. Java设计模式(三) 抽象工厂模式

    原创文章,同步发自作者个人博客,转载请注明出处 http://www.jasongj.com/design_pattern/abstract_factory/ 抽象工厂模式解决的问题 上文<工厂 ...

  6. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  7. 【UE4 设计模式】抽象工厂模式 Abstract Factory Pattern

    概述 描述 提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类:具体的工厂负责实现具体的产品实例 抽象工厂中每个工厂可以创建多种产品(如苹果公司生产iPhone.iPad): 工厂方法 ...

  8. 设计模式 - 抽象工厂模式(abstract factory pattern) 具体解释

    抽象工厂模式(abstract factory pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2709 ...

  9. 二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)

    抽象工厂模式(Abstract Factory Pattern) 介绍提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 示例有Message和MessageModel,Messag ...

  10. 抽象工厂模式(Abstract Factory)C#实例

    抽象工厂模式(Abstract Factory)C#实例 本文出处http://www.dofactory.com/net/abstract-factory-design-pattern 一.场景描述 ...

随机推荐

  1. UVALive - 2965 Jurassic Remains (LA)

    Jurassic Remains Time Limit: 18000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Sub ...

  2. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. 在VMware Workstation11虚拟机上安装黑苹果

    图文详解如何在VMware Workstation11虚拟机上安装黑苹果Mac OS X 10.10系统-网络教程与技术 -亦是美网络 http://www.yishimei.cn/network/5 ...

  4. log4j日志优先级导致的不输出日志

    在sae部署微信代码的时候,发现它的默认日志很不友好,看起来很费劲,详细看了一下说明发现它可以根据log4j的输出级别而分类输出,就拖了一个log4j的xml文件扔进项目代码,然后部署运行,发现没有日 ...

  5. 汇编中call printf参数压栈时错误理解

    EAX, ECX,EDX,EBX均可以32bit,16bit,8bit访问,如下所示: <-------------------EAX------------------------>|& ...

  6. [转]GridView排序——微软提供Sort

    本文转自:http://www.cnblogs.com/eva_2010/articles/1995646.html 在GridView中,根据其中的某列进行排序. 1. 页面:AllowSortin ...

  7. CORDIC原理与FPGA实现(2)

    CORDIC算法实现极坐标(polar)到直角坐标系(Cartesian)的变换. 1: function [horizonal,vertical]=polar2car(mag, pha); 2: x ...

  8. 利用Google Speech API实现Speech To Text

    很久很久以前, 网上流传着一个免费的,识别率暴高的,稳定的 Speech To Text API, 那就是Google Speech API. 但是最近再使用的时候,总是返回500 Error. 后来 ...

  9. 边工作边刷题:70天一遍leetcode: day 85

    Find the Celebrity 要点: 这题从solution反过来想比较好:loop through n同时maintain一个candidate:如果cand认识某个i,那么modify c ...

  10. YOU ARE MY SUNSHINE

    /*you are sunshine, my only sunshine, you make me happy when skies are grey. you'll never know dear ...