介绍
这种类型的设计模式属于创建型模式。
意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
主要解决:主要解决接口选择的问题。
适用场景:系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。
例如:QQ 换皮肤,一整套一起换。
实现
图解
抽象工厂模式
需求:创建一个家庭对象,需要创建父亲对象、母亲对象、孩子对象等等,所谓的父亲、母亲、孩子就构成了一个产品族,中国家庭、美国家庭就是产品族等级。
//先创建抽象产品(抽象母亲、抽象父亲),具体产品(具体母亲、具体父亲)
interface IMother {
public void printName();
}
interface IFather {
public void printName();
}
class ChineseMother implements IMother{
private String name;
public ChineseMother(String name) {
this.name = name;
System.out.println("创建一个中国母亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
class AmericanMother implements IMother{
private String name;
public AmericanMother(String name) {
this.name = name;
System.out.println("创建一个美国母亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
class ChineseFather implements IFather{
private String name;
public ChineseFather(String name) {
this.name = name;
System.out.println("创建一个中国父亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
class AmericanFather implements IFather{
private String name;
public AmericanFather(String name) {
this.name = name;
System.out.println("创建一个美国父亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
//创建一个抽象家庭工厂接口
interface IFamilyFactory {
public IFather createFather(String name);
public IMother createMother(String name);
}
//分别创建具体的中国家庭工厂和美国家庭工厂
class ChineseFamilyFactory implements IFamilyFactory{
@Override
public IFather createFather(String name) {
return new ChineseFather(name);
}
@Override
public IMother createMother(String name) {
return new ChineseMother(name);
}
}
class AmericanFamilyFactory implements IFamilyFactory{
@Override
public IFather createFather(String name) {
return new AmericanFather(name);
}
@Override
public IMother createMother(String name) {
return new AmericanMother(name);
}
}
public class Client {
// 创建产品使用者
public static void main(String[] args) {
IFamilyFactory cnFamilyFactory = new ChineseFamilyFactory();
IFamilyFactory usFamilyFactory = new AmericanFamilyFactory();
IFather cnFather = cnFamilyFactory.createFather("中国父亲");
IMother cnMother = cnFamilyFactory.createMother("中国母亲");
IFather usFather = usFamilyFactory.createFather("美国父亲");
IMother usMother = usFamilyFactory.createMother("美国母亲");
cnFather.printName();
cnMother.printName();
usFather.printName();
usMother.printName();
}
}
不使用抽象工厂模式
//先创建抽象产品(抽象母亲、抽象父亲),具体产品(具体母亲、具体父亲)
interface IMother {
public void printName();
}
interface IFather {
public void printName();
}
class ChineseMother implements IMother{
private String name;
public ChineseMother(String name) {
this.name = name;
System.out.println("创建一个中国母亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
class AmericanMother implements IMother{
private String name;
public AmericanMother(String name) {
this.name = name;
System.out.println("创建一个美国母亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
class ChineseFather implements IFather{
private String name;
public ChineseFather(String name) {
this.name = name;
System.out.println("创建一个中国父亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
class AmericanFather implements IFather{
private String name;
public AmericanFather(String name) {
this.name = name;
System.out.println("创建一个美国父亲。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
public class Client {
// 创建产品使用者
public static void main(String[] args) {
IFather cnFather = new ChineseFather("中国父亲");
IMother cnMother = new ChineseMother("中国母亲");
IFather usFather = new AmericanFather("美国父亲");
IMother usMother = new AmericanMother("美国母亲");
cnFather.printName();
cnMother.printName();
usFather.printName();
usMother.printName();
}
}
总结
优点
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点
产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator里加代码,又要在具体的里面加代码。