设计模式之(三):抽象工厂设计模式

抽象工厂内容概要

一.定义

提供一个创建一系列相关或相互依赖对象的接口

二.角色

  • 1.抽象工厂
  • 2.抽象产品(多个)
  • 3.具体产品(多个)
  • 4.具体工厂(多个)

    三.适用场景

  • 客户端不依赖于产品类实例如何被创建、实现等细节
  • 强调一系列相关的产品对象(属于同一产品族)一起使用创建对象需要大量重复的代码
  • 提供一个产品类的库,所有的产品以同样的接口出现,从而使用客户端不依赖于具体实现

四.优缺点

(1).优点
  • 具体产品在应用层代码隔离,无须关心创建细节(所有工厂的设计模式都有这个特点)
  • 将一个系列的产品族统一到一起创建
    (2).缺点
  • 规定了所有有可能被创建的产品集合,产品族中扩展新的产品困难,需要修改抽象工厂接口
  • 增加了系统的抽象性的和理解难度

五.相关概念

1.产品等级结构 : 相同类型的产品

如手机行业,国内比较出名的华为,Vivo和小米,手机这个产品属于一个产品等级,耳机这个产品也属于一个产品等级

2.产品族 : 由同一个工厂生产的不同产品等级结构的产品

如手机行业,国内的华为,Vivo和小米,一个品牌可以认为是一个产品族

六.实例

1.抽象产品
  • 抽象产品1:手机
  1. /**
  2. * 手机
  3. * @author shixinke
  4. */
  5. public abstract class Mobile {
  6. protected String name;
  7. public String getName() {
  8. return this.getBrand() + name;
  9. }
  10. /**
  11. * 获取品牌
  12. * @return
  13. */
  14. abstract String getBrand();
  15. }
  • 抽象产品2: 耳机
  1. /**
  2. * 耳机
  3. * @author shixinke
  4. */
  5. public abstract class Headset {
  6. protected String name;
  7. public String getName() {
  8. return this.getBrand() + name;
  9. }
  10. /**
  11. * 获取品牌
  12. * @return
  13. */
  14. abstract String getBrand();
  15. }
  • 抽象产品3:智能手环
  1. /**
  2. * 智能手环
  3. * @author shixinke
  4. */
  5. public abstract class Wristband {
  6. protected String name;
  7. public String getName() {
  8. return this.getBrand() + name;
  9. }
  10. /**
  11. * 获取品牌
  12. * @return
  13. */
  14. abstract String getBrand();
  15. }
2.抽象工厂
  1. /**
  2. * 智能工厂
  3. */
  4. public interface SmartFactory {
  5. /**
  6. * 生产耳机
  7. * @return
  8. */
  9. Headset produceHeadSet();
  10. /**
  11. * 生产手机
  12. * @return
  13. */
  14. Mobile produceMobile();
  15. /**
  16. * 生产手环
  17. * @return
  18. */
  19. Wristband produceWristband();
  20. }
3.具体产品:
  • 具体产品1:华为手机
  1. /**
  2. * 华为手机
  3. * @author shixinke
  4. */
  5. public class HuaweiMobile extends Mobile {
  6. public HuaweiMobile(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "华为";
  12. }
  13. }
  • 具体产品2:华为耳机
  1. /**
  2. * 华为耳机
  3. * @author shixinke
  4. */
  5. public class HuaweiHeadset extends Headset {
  6. public HuaweiHeadset(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "华为";
  12. }
  13. }
  • 具体产品3:华为手环
  1. /**
  2. * 华为手环
  3. * @author shixinke
  4. */
  5. public class HuaweiWristband extends Wristband {
  6. public HuaweiWristband(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "华为";
  12. }
  13. }
  • 具体产品4:Vivo手机
  1. /**
  2. * Vivo手机
  3. * @return
  4. */
  5. public class VivoMobile extends Mobile {
  6. public VivoMobile(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "vivo";
  12. }
  13. }
  • 具体产品5:Vivo耳机
  1. /**
  2. * Vivo耳机
  3. * @return
  4. */
  5. public class VivoHeadset extends Headset {
  6. public VivoHeadset(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "Vivo";
  12. }
  13. }
  • 具体产品6:Vivo手环
  1. /**
  2. * Vivo手环
  3. * @return
  4. */
  5. public class VivoWristband extends Wristband {
  6. public VivoWristband(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "Vivo";
  12. }
  13. }
  • 具体产品7:小米手机
  1. /**
  2. * 小米手机
  3. * @return
  4. */
  5. public class XiaomiMobile extends Mobile {
  6. public XiaomiMobile(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "小米";
  12. }
  13. }
  • 具体产品8:小米耳机
  1. /**
  2. * 小米耳机
  3. * @return
  4. */
  5. public class XiaomiHeadset extends Headset {
  6. public XiaomiHeadset(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. public String getBrand() {
  11. return "小米";
  12. }
  13. }
  • 具体产品9:小米手环
  1. /**
  2. * 小米手环
  3. * @return
  4. */
  5. public class XiaomiWristband extends Wristband {
  6. public XiaomiWristband(String name) {
  7. this.name = name;
  8. }
  9. @Override
  10. String getBrand() {
  11. return "小米";
  12. }
  13. }
3.具体工厂:
  • 华为工厂
  1. /**
  2. * 华为工厂
  3. * @author shixinke
  4. */
  5. public class HuaweiFactory implements SmartFactory {
  6. /**
  7. * 华为工厂生产华为耳机
  8. * @return
  9. */
  10. public Headset produceHeadSet() {
  11. return new HuaweiHeadset("荣耀xSport 运动蓝牙耳机");
  12. }
  13. /**
  14. * 华为工厂生产华为手机
  15. * @return
  16. */
  17. public Mobile produceMobile() {
  18. return new HuaweiMobile("Mate 20 X ");
  19. }
  20. /**
  21. * 华为工厂生产华为手环
  22. * @return
  23. */
  24. public Wristband produceWristband() {
  25. return new HuaweiWristband("3 Pro(曜石黑)智能运动手环");
  26. }
  27. }
  • Vivo工厂
  1. /**
  2. * Vivo工厂
  3. * @author shixinke
  4. */
  5. public class VivoFactory implements SmartFactory {
  6. /**
  7. * Vivo工厂生产vivo耳机
  8. * @return
  9. */
  10. public Headset produceHeadSet() {
  11. return new VivoHeadset("TWS Earphone 真无线蓝牙耳机");
  12. }
  13. /**
  14. * Vivo工厂生产vivo手机
  15. * @return
  16. */
  17. public Mobile produceMobile() {
  18. return new VivoMobile("NEX 3");
  19. }
  20. /**
  21. * Vivo工厂生产vivo手环
  22. * @return
  23. */
  24. public Wristband produceWristband() {
  25. return new VivoWristband("乐心手环Mambo5");
  26. }
  27. }
  • 小米工厂
  1. /**
  2. * 小米工厂
  3. * @author shixinke
  4. */
  5. public class XiaomiFactory implements SmartFactory {
  6. /**
  7. * 小米工厂生产小米耳机
  8. * @return
  9. */
  10. public Headset produceHeadSet() {
  11. return new XiaomiHeadset("降噪项圈蓝牙耳机");
  12. }
  13. /**
  14. * 小米工厂生产小米手机
  15. * @return
  16. */
  17. public Mobile produceMobile() {
  18. return new XiaomiMobile("MIX 3");
  19. }
  20. /**
  21. * 小米工厂生产小米手环
  22. * @return
  23. */
  24. public Wristband produceWristband() {
  25. return new XiaomiWristband("Amazfit手环");
  26. }
  27. }
5.客户端(调用端)
  1. /**
  2. * 测试类
  3. * @author shixinke
  4. */
  5. public class FactoryTest {
  6. public static void main(String[] args) {
  7. SmartFactory factory = new VivoFactory();
  8. Headset headset = factory.produceHeadSet();
  9. Mobile mobile = factory.produceMobile();
  10. Wristband wristband = factory.produceWristband();
  11. System.out.println("耳机:"+headset.getName() +" 手机:"+mobile.getName() +" 手环:"+wristband.getName());
  12. }
  13. }

以上类的关系图:

抽象工厂类关系图

七.抽象工厂在源码中的应用

1.抽象工厂在JDK中的使用

java.sql.Connection类

  1. public interface Connection extends Wrapper, AutoCloseable {
  2. Statement createStatement() throws SQLException;
  3. PreparedStatement prepareStatement(String sql)
  4. throws SQLException;
  5. }
  • 抽象产品: Statement,PreparedStatement等
  • 抽象工厂:Connection
  • 具体产品:CallableStatement和PgCallableStatement等
  • 具体工厂:MultiHostMySQLConnection和PgConnection

类关系图:

Connection类关系图