抽象工廠模式相對于工廠方法模式來說,就是工廠方法模式是針對一個產(chǎn)品系列的啊胶,而抽象工廠模式是針對多個產(chǎn)品系列的甸各,即工廠方法模式是一個產(chǎn)品系列一個工廠類,而抽象工廠模式是多個產(chǎn)品系列一個工廠類焰坪。在抽象工廠模式中趣倾,客戶端不再負責對象的創(chuàng)建,而是把這個責任丟給了具體的工廠類某饰,客戶端只負責對對象的調(diào)用儒恋,從而明確了各個類的職責。并且當一系列相互關(guān)聯(lián)的產(chǎn)品被設(shè)計到一個工廠類里后黔漂,客戶端的調(diào)用將會變得非常簡單诫尽,而且,如果要更換這一系列的產(chǎn)品炬守,則只需要更換一個工廠類即可牧嫉。
如果客戶端需要創(chuàng)建一些產(chǎn)品結(jié)構(gòu),而這些產(chǎn)品結(jié)構(gòu)又分別屬于不同的產(chǎn)品類別,則可以使用抽象工廠模式酣藻,抽象工廠模式中抽象工廠類負責定義創(chuàng)建對象的接口曹洽,具體這一系列對象的創(chuàng)建工作由實現(xiàn)抽象工廠的具體工廠類來完成。
package com.strife.pattern.factory;
/**
* 抽象工廠
*
* @author mengzhenghao
* @date 2022/5/28
*/
public class AbstractFactory {
public static void main(String[] args) throws Exception {
CarProductFactory factory = new ChinaCarFactory();
factory.getCar("hq");
factory.getCar("df");
factory = new GermanyCarFactory();
factory.getCar("ad");
factory.getCar("bm");
}
}
/** 抽象工廠 */
interface CarProductFactory {
/** 生產(chǎn)汽車 */
AbstractCarProduct getCar(String type) throws Exception;
}
/** 中國汽車工廠 */
class ChinaCarFactory implements CarProductFactory {
@Override
public AbstractCarProduct getCar(String type) throws Exception {
AbstractCarProduct product = null;
for (ChinaCarEnumType carEnumType : ChinaCarEnumType.values()) {
if (type.equals(carEnumType.getCarType())) {
product = (AbstractCarProduct) Class.forName(carEnumType.getClazz()).newInstance();
product.name = carEnumType.getCarName();
product.material();
product.origin();
break;
}
}
if (product == null) {
throw new RuntimeException("暫時不能生產(chǎn)該汽車");
}
return product;
}
}
/** 德國汽車工廠 */
class GermanyCarFactory implements CarProductFactory {
@Override
public AbstractCarProduct getCar(String type) throws Exception {
AbstractCarProduct product = null;
for (GermanyCarEnumType carEnumType : GermanyCarEnumType.values()) {
if (type.equals(carEnumType.getCarType())) {
product = (AbstractCarProduct) Class.forName(carEnumType.getClazz()).newInstance();
product.name = carEnumType.getCarName();
product.material();
product.origin();
break;
}
}
if (product == null) {
throw new RuntimeException("暫時不能生產(chǎn)該汽車");
}
return product;
}
}
/** 汽車生產(chǎn)抽象類 */
abstract class AbstractCarProduct {
/** 汽車名稱 */
protected String name ;
/** 材料 */
abstract void material() ;
/** 產(chǎn)地 */
abstract void origin() ;
}
/** 紅旗車 */
class HQCar extends AbstractCarProduct {
@Override
void material() {
System.out.println(super.name+"材料...");
}
@Override
void origin() {
System.out.println(super.name+"在中國北京生產(chǎn)");
}
}
/** 東風車 */
class DFCar extends AbstractCarProduct {
@Override
void material() {
System.out.println(super.name+"材料...");
}
@Override
void origin() {
System.out.println(super.name+"在中國南京生產(chǎn)");
}
}
/** 奧迪車 */
class ADCar extends AbstractCarProduct {
@Override
void material() {
System.out.println(super.name+"材料...");
}
@Override
void origin() {
System.out.println(super.name+"在德國柏林生產(chǎn)");
}
}
/** 寶馬車 */
class BMCar extends AbstractCarProduct {
@Override
void material() {
System.out.println(super.name+"材料...");
}
@Override
void origin() {
System.out.println(super.name+"在德國慕尼黑生產(chǎn)");
}
}
enum ChinaCarEnumType {
/** 紅旗車 */
HQ("hq", "紅旗", "com.strife.pattern.factory.HQCar"),
/** 東風車 */
DF("df", "東風", "com.strife.pattern.factory.DFCar");
private final String carType;
private final String carName;
private final String clazz;
ChinaCarEnumType(String carType, String carName, String clazz) {
this.carType = carType;
this.carName = carName;
this.clazz = clazz;
}
public String getCarType() {
return carType;
}
public String getCarName() {
return carName;
}
public String getClazz() {
return clazz;
}
}
enum GermanyCarEnumType {
/** 奧迪車 */
AD("ad", "奧迪", "com.strife.pattern.factory.ADCar"),
/** 寶馬車 */
BM("bm", "寶馬", "com.strife.pattern.factory.BMCar");
private final String carType;
private final String carName;
private final String clazz;
GermanyCarEnumType(String carType, String carName, String clazz) {
this.carType = carType;
this.carName = carName;
this.clazz = clazz;
}
public String getCarType() {
return carType;
}
public String getCarName() {
return carName;
}
public String getClazz() {
return clazz;
}
}