一.定义
提供一个创建一系列相关或相互依赖对象的接口
二.角色
- 1.抽象工厂
- 2.抽象产品(多个)
- 3.具体产品(多个)
- 4.具体工厂(多个)
三.适用场景
- 客户端不依赖于产品类实例如何被创建、实现等细节
- 强调一系列相关的产品对象(属于同一产品族)一起使用创建对象需要大量重复的代码
- 提供一个产品类的库,所有的产品以同样的接口出现,从而使用客户端不依赖于具体实现
四.优缺点
(1).优点
- 具体产品在应用层代码隔离,无须关心创建细节(所有工厂的设计模式都有这个特点)
- 将一个系列的产品族统一到一起创建
(2).缺点
- 规定了所有有可能被创建的产品集合,产品族中扩展新的产品困难,需要修改抽象工厂接口
- 增加了系统的抽象性的和理解难度
五.相关概念
1.产品等级结构 : 相同类型的产品
如手机行业,国内比较出名的华为,Vivo和小米,手机这个产品属于一个产品等级,耳机这个产品也属于一个产品等级
2.产品族 : 由同一个工厂生产的不同产品等级结构的产品
如手机行业,国内的华为,Vivo和小米,一个品牌可以认为是一个产品族
六.实例
1.抽象产品
- 抽象产品1:手机
/**
* 手机
* @author shixinke
*/
public abstract class Mobile {
protected String name;
public String getName() {
return this.getBrand() + name;
}
/**
* 获取品牌
* @return
*/
abstract String getBrand();
}
- 抽象产品2: 耳机
/**
* 耳机
* @author shixinke
*/
public abstract class Headset {
protected String name;
public String getName() {
return this.getBrand() + name;
}
/**
* 获取品牌
* @return
*/
abstract String getBrand();
}
- 抽象产品3:智能手环
/**
* 智能手环
* @author shixinke
*/
public abstract class Wristband {
protected String name;
public String getName() {
return this.getBrand() + name;
}
/**
* 获取品牌
* @return
*/
abstract String getBrand();
}
2.抽象工厂
/**
* 智能工厂
*/
public interface SmartFactory {
/**
* 生产耳机
* @return
*/
Headset produceHeadSet();
/**
* 生产手机
* @return
*/
Mobile produceMobile();
/**
* 生产手环
* @return
*/
Wristband produceWristband();
}
3.具体产品:
- 具体产品1:华为手机
/**
* 华为手机
* @author shixinke
*/
public class HuaweiMobile extends Mobile {
public HuaweiMobile(String name) {
this.name = name;
}
@Override
String getBrand() {
return "华为";
}
}
- 具体产品2:华为耳机
/**
* 华为耳机
* @author shixinke
*/
public class HuaweiHeadset extends Headset {
public HuaweiHeadset(String name) {
this.name = name;
}
@Override
String getBrand() {
return "华为";
}
}
- 具体产品3:华为手环
/**
* 华为手环
* @author shixinke
*/
public class HuaweiWristband extends Wristband {
public HuaweiWristband(String name) {
this.name = name;
}
@Override
String getBrand() {
return "华为";
}
}
- 具体产品4:Vivo手机
/**
* Vivo手机
* @return
*/
public class VivoMobile extends Mobile {
public VivoMobile(String name) {
this.name = name;
}
@Override
String getBrand() {
return "vivo";
}
}
- 具体产品5:Vivo耳机
/**
* Vivo耳机
* @return
*/
public class VivoHeadset extends Headset {
public VivoHeadset(String name) {
this.name = name;
}
@Override
String getBrand() {
return "Vivo";
}
}
- 具体产品6:Vivo手环
/**
* Vivo手环
* @return
*/
public class VivoWristband extends Wristband {
public VivoWristband(String name) {
this.name = name;
}
@Override
String getBrand() {
return "Vivo";
}
}
- 具体产品7:小米手机
/**
* 小米手机
* @return
*/
public class XiaomiMobile extends Mobile {
public XiaomiMobile(String name) {
this.name = name;
}
@Override
String getBrand() {
return "小米";
}
}
- 具体产品8:小米耳机
/**
* 小米耳机
* @return
*/
public class XiaomiHeadset extends Headset {
public XiaomiHeadset(String name) {
this.name = name;
}
@Override
public String getBrand() {
return "小米";
}
}
- 具体产品9:小米手环
/**
* 小米手环
* @return
*/
public class XiaomiWristband extends Wristband {
public XiaomiWristband(String name) {
this.name = name;
}
@Override
String getBrand() {
return "小米";
}
}
3.具体工厂:
- 华为工厂
/**
* 华为工厂
* @author shixinke
*/
public class HuaweiFactory implements SmartFactory {
/**
* 华为工厂生产华为耳机
* @return
*/
public Headset produceHeadSet() {
return new HuaweiHeadset("荣耀xSport 运动蓝牙耳机");
}
/**
* 华为工厂生产华为手机
* @return
*/
public Mobile produceMobile() {
return new HuaweiMobile("Mate 20 X ");
}
/**
* 华为工厂生产华为手环
* @return
*/
public Wristband produceWristband() {
return new HuaweiWristband("3 Pro(曜石黑)智能运动手环");
}
}
- Vivo工厂
/**
* Vivo工厂
* @author shixinke
*/
public class VivoFactory implements SmartFactory {
/**
* Vivo工厂生产vivo耳机
* @return
*/
public Headset produceHeadSet() {
return new VivoHeadset("TWS Earphone 真无线蓝牙耳机");
}
/**
* Vivo工厂生产vivo手机
* @return
*/
public Mobile produceMobile() {
return new VivoMobile("NEX 3");
}
/**
* Vivo工厂生产vivo手环
* @return
*/
public Wristband produceWristband() {
return new VivoWristband("乐心手环Mambo5");
}
}
- 小米工厂
/**
* 小米工厂
* @author shixinke
*/
public class XiaomiFactory implements SmartFactory {
/**
* 小米工厂生产小米耳机
* @return
*/
public Headset produceHeadSet() {
return new XiaomiHeadset("降噪项圈蓝牙耳机");
}
/**
* 小米工厂生产小米手机
* @return
*/
public Mobile produceMobile() {
return new XiaomiMobile("MIX 3");
}
/**
* 小米工厂生产小米手环
* @return
*/
public Wristband produceWristband() {
return new XiaomiWristband("Amazfit手环");
}
}
5.客户端(调用端)
/**
* 测试类
* @author shixinke
*/
public class FactoryTest {
public static void main(String[] args) {
SmartFactory factory = new VivoFactory();
Headset headset = factory.produceHeadSet();
Mobile mobile = factory.produceMobile();
Wristband wristband = factory.produceWristband();
System.out.println("耳机:"+headset.getName() +" 手机:"+mobile.getName() +" 手环:"+wristband.getName());
}
}
以上类的关系图:
七.抽象工厂在源码中的应用
1.抽象工厂在JDK中的使用
java.sql.Connection类
public interface Connection extends Wrapper, AutoCloseable {
Statement createStatement() throws SQLException;
PreparedStatement prepareStatement(String sql)
throws SQLException;
}
- 抽象产品: Statement,PreparedStatement等
- 抽象工厂:Connection
- 具体产品:CallableStatement和PgCallableStatement等
- 具体工厂:MultiHostMySQLConnection和PgConnection
类关系图: