设计模式之抽象工厂模式
抽象工厂模式(Abstract Factory),其意图主要是提供一个创建一系列相关或相互依赖对象的接口,而无需指定它具体的类。
抽象工厂模式是对工厂方法模式的拓展和延伸,工厂方法是针对任意数量的产品等级,多应用于虚拟构造子类,抽象工厂则可以处理多个产品族结构。也就是说工厂方法模式只能创造出来一类茶品,而抽象工厂模式可以创造多种产品。其适用性在于:
一个系统要独立于它的产品的创建、组合和表示时。
一个系统要由多个产品系列中的一个来配置时。
当你要强调一系列相关的产品对象的设计以便进行联合使用时。
当你提供一个产品类库,而只想显示它们的接口而不是实现时。
如下图:
抽象工厂模式分离了具体的类,因为一个工厂封装了创建产品对象的责任和过程,其将客户与类的实现分离。如下一个例子猫和狗还有动物工厂都是通过接口来实现的,可能不是很恰当。
ICat.java
package org.designpattern.creational.abstractfactory;
public interface ICat {
public void eat();
}
public interface ICat {
public void eat();
}
IDog.java
package org.designpattern.creational.abstractfactory;
public interface IDog {
public void eat();
public interface IDog {
public void eat();
}
IAnimalFactory.java
package org.designpattern.creational.abstractfactory;
public interface IAnimalFactory {
public ICat createCat();
public IDog createDog();
public interface IAnimalFactory {
public ICat createCat();
public IDog createDog();
}
WhiteCat.java
package org.designpattern.creational.abstractfactory;
public class WhiteCat implements ICat {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a white cat is eating!");
}
public class WhiteCat implements ICat {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a white cat is eating!");
}
}
WhiteDog.java
package org.designpattern.creational.abstractfactory;
public class WhiteDog implements IDog {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a white dog is eating!");
}
public class WhiteDog implements IDog {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a white dog is eating!");
}
}
BlackCat.java
package org.designpattern.creational.abstractfactory;
public class BlackCat implements ICat {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a black cat is eating!");
}
public class BlackCat implements ICat {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a black cat is eating!");
}
}
BlackDog.java
package org.designpattern.creational.abstractfactory;
public class BlackDog implements IDog {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a black cat is eating!");
}
public class BlackDog implements IDog {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("a black cat is eating!");
}
}
下面是白色和黑色动物的创建工厂类:
WhiteAnimalFactory.java
package org.designpattern.creational.abstractfactory;
public class WhiteAnimalFactoy implements IAnimalFactory {
@Override
public ICat createCat() {
// TODO Auto-generated method stub
return new WhiteCat();
}
@Override
public IDog createDog() {
// TODO Auto-generated method stub
return new WhiteDog();
}
public class WhiteAnimalFactoy implements IAnimalFactory {
@Override
public ICat createCat() {
// TODO Auto-generated method stub
return new WhiteCat();
}
@Override
public IDog createDog() {
// TODO Auto-generated method stub
return new WhiteDog();
}
}
BlackAnimalFactory.java
package org.designpattern.creational.abstractfactory;
public class BlackAnimalFactory implements IAnimalFactory {
@Override
public ICat createCat() {
// TODO Auto-generated method stub
return new BlackCat();
}
@Override
public IDog createDog() {
// TODO Auto-generated method stub
return new BlackDog();
}
public class BlackAnimalFactory implements IAnimalFactory {
@Override
public ICat createCat() {
// TODO Auto-generated method stub
return new BlackCat();
}
@Override
public IDog createDog() {
// TODO Auto-generated method stub
return new BlackDog();
}
}
这个实现很简单,抽象工厂里的两个创建方法来构造具体产品,白色动物和黑色动物工厂负责来创建,两个产品就是猫和狗了,在这里我没有表现出两个产品的不同而是都用了eat方法,其实也不太合理,呵呵!
抽象工厂主要针对的是多个产品等级结构的创建,其缺点就是在添加新产品时要进行原代码改动较大!