java设计模式之创建型模式-抽象工厂模式

抽象工厂设计模式

概念

抽象工厂模式(英语:Abstract factory pattern)是一种软件开发设计模式。抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来。

在正常使用中,客户端程序需要创建抽象工厂的具体实现,然后使用抽象工厂作为接口来创建这一主题的具体对象。客户端程序不需要知道(或关心)它从这些内部的工厂方法中获得对象的具体类型,因为客户端程序仅使用这些对象的通用接口。

抽象工厂模式将一组对象的实现细节与他们的一般使用分离开来。


适用性

在以下情况可以考虑使用抽象工厂模式:

  1. 一个系统要独立于它的产品的创建、组合和表示时。

  2. 一个系统要由多个产品系列中的一个来配置时。

  3. 需要强调一系列相关的产品对象的设计以便进行联合使用时。

  4. 提供一个产品类库,而只想显示它们的接口而不是实现时。

UML


例子

一个抽象工厂类叫做DocumentCreator(文档创建器),此类提供创建若干种产品的接口,包括createLetter()(创建信件)和createResume()(创建简历)。

  
  
  1. public interface DocumentCreator {
  2. Letter createLetter();
  3. Resume createResume();
  4. }

其中,createLetter()返回一个Letter(信件),createResume()返回一个Resume(简历)。

系统中还有一些DocumentCreator的具体实现类,包括FancyDocumentCreator和ModernDocumentCreator。

  
  
  1. public class FancyDocumentCreator implements DocumentCreator {
  2. public Letter createLetter() {
  3. return new FancyLetter();
  4. }
  5. public Resume createResume() {
  6. return new FancyResume();
  7. }
  8. }

  
  
  1. public class ModernDocumentCreator implements DocumentCreator {
  2. public Letter createLetter() {
  3. return new ModernLetter();
  4. }
  5. public Resume createResume() {
  6. return new ModernResume();
  7. }
  8. }

这两个类对DocumentCreator的两个方法分别有不同的实现,用来创建不同的“信件”和“简历”(用FancyDocumentCreator的实例可以创建FancyLetter和FancyResume,

  
  
  1. public class FancyLetter implements Letter {
  2. public void sendLetter() {
  3. System.out.println("发送FancyLetter");
  4. }
  5. }

  
  
  1. public class FancyResume implements Resume {
  2. public void sendResume() {
  3. System.out.println("发送FancyResume");
  4. }
  5. }


用ModernDocumentCreator的实例可以创建ModernLetter和ModernResume)。

  
  
  1. public class ModernLetter implements Letter {
  2. public void sendLetter() {
  3. System.out.println("发送ModernLetter");
  4. }
  5. }

  
  
  1. public class ModernResume implements Resume {
  2. public void sendResume() {
  3. System.out.println("发送ModernResume");
  4. }
  5. }


这些具体的“信件”和“简历”类均继承自抽象类,即Letter和Resume类。

  
  
  1. public interface Letter {
  2. public void sendLetter();
  3. }

  
  
  1. public interface Resume {
  2. public void sendResume();
  3. }


客户端需要创建“信件”或“简历”时,先要得到一个合适的DocumentCreator实例,然后调用它的方法。一个工厂中创建的每个对象都是同一个主题的(“fancy”或者“modern”)。客户端程序只需要知道得到的对象是“信件”或者“简历”,而不需要知道具体的主题,因此客户端程序从抽象工厂DocumentCreator中得到了Letter或Resume类的引用,而不是具体类的对象引用。

  
  
  1. public void test6() throws Exception {
  2. // 客户端需要华丽的Letter
  3. DocumentCreator dc1 = new FancyDocumentCreator();
  4. Letter letter = dc1.createLetter();
  5. letter.sendLetter();
  6. // 客户端需要现代化的Resume
  7. DocumentCreator dc2 = new ModernDocumentCreator();
  8. Resume resume = dc2.createResume();
  9. resume.sendResume();
  10. }

结果:


“工厂”是创建产品(对象)的地方,其目的是将产品的创建与产品的使用分离。

抽象工厂模式的目的,是将若干抽象产品的接口与不同主题产品的具体实现分离开。

这样就能在增加新的具体工厂的时候,不用修改引用抽象工厂的客户端代码。

使用抽象工厂模式,能够在具体工厂变化的时候,不用修改使用工厂的客户端代码,甚至是在运行时。然而,使用这种模式或者相似的设计模式,可能给编写代码带来不必要的复杂性和额外的工作。正确使用设计模式能够抵消这样的“额外工作”。


参考:https://zh.wikipedia.org/wiki/抽象工厂 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值