抽象工厂模式(Abstract Factory)
先上类图吧,自己懒得画了,找了个现成的
抽象工厂的引入是根据需求而产生的,说到这里先要提到一个名字—产品族(Product Family),还是以生产汽车为例,我想要一个上汽的汽车工厂能够同时生产一款MPV,一款SUV和一款Sedan,那比如说它就是上海通用别克的工厂啦,可以生产MPV GL8,生产SUV 昂科威,可以生产Sedan 君威,这几款产品就算是一个产品族。作为公司高层,肯定是希望工厂能够有能力同时生产这三款车,将这个工程抽象出来,就成了这个样子:
public interface AbstractFactory {
public SUV makeSUV();
public MPV makeMPV();
public Sedan makeSedan();
}
不过这个例子并不好,因为这里的三种车辆还是属于车辆,而产品族,是指不同的产品,这里还是换一个例子来说明吧。
生产一个SUV,需要生产不同的零件,如轮胎,方向盘,发动机。该款SUV上的Tyre, SteeringWheel和Engine,则属于一个产品族,一个SUV对象需要Tyre, SteeringWheel和Engine的实例才能正常工作。如果在SUV的类中,直接调用新建三种对象,当然是没有问题的。问题出在,如果换一款SUV,轮胎,方向盘,发动机都变了,那我们就得重新新建TyreB, SteeringWheelB和EngineB. 为了让SUV更加通用,可以抽象出Tyre, SteeringWheel和Engine三个接口,然后实现不同的Tyre, SteeringWheel和Engine产品。 同时针对SUVA和SUVB两种SUV。创建一个Creater接口,接口中定义了创建3中产品的方法。而在Creater接口的具体实现类中,则实现三种产品不同型号的创建。
我们抽象出来的类图如下所示:
类图
代码:
三种产品
interface Tyre {
public void Roll();
}
class TyreA implements Tyre{
@Override
public void Roll() {
System.out.println("I am TyreA ,I am Big");
}
}
class TyreB implements Tyre{
@Override
public void Roll() {
System.out.println("I am TyreB ,I am Small");
}
}
interface SteeringWheel{
public void Turn();
}
class SteeringWheelA implements SteeringWheel{
@Override
public void Turn() {
System.out.println("I am SteeringWheelA ,I am Big");
}
}
class SteeringWheelB implements SteeringWheel{
@Override
public void Turn() {
System.out.println("I am SteeringWheelB ,I am Small");
}
}
interface Engine{
public void Roar();
}
class EngineA implements Engine{
@Override
public void Roar() {
System.out.println("I am EngineA ,I am 3.0Turbo ");
}
}
class EngineB implements Engine{
@Override
public void Roar() {
System.out.println("I am EngineB ,I am 1.6L ");
}
}
两个工厂
public interface AbstractFactory {
public Tyre createTyre();
public SteeringWheel createSteeringWheel();
public Engine createEnginen();
}
class SUVFactory1 implements AbstractFactory{
@Override
public Tyre createTyre() {
return new TyreA();
}
@Override
public SteeringWheel createSteeringWheel() {
return new SteeringWheelA();
}
@Override
public Engine createEnginen() {
return new EngineA();
}
}
class SUVFactory2 implements AbstractFactory{
@Override
public Tyre createTyre() {
return new TyreB();
}
@Override
public SteeringWheel createSteeringWheel() {
return new SteeringWheelB();
}
@Override
public Engine createEnginen() {
return new EngineB();
}
}
使用工厂1创建的SUV
public class SUV {
public Tyre mTyre;
public SteeringWheel mSteeringWheel;
public Engine mEngine;
public AbstractFactory mFactory;
public SUV() {
mFactory=new SUVFactory1();
mTyre=mFactory.createTyre();
mSteeringWheel=mFactory.createSteeringWheel();
mEngine=mFactory.createEnginen();
}
public void show(){
mTyre.Roll();
mSteeringWheel.Turn();
mEngine.Roar();
}
}
测试类
public class Host {
public static void main(String[] args) {
SUV mSUV=new SUV();
mSUV.show();
}
}
输出:
I am TyreA ,I am Big
I am SteeringWheelA ,I am Big
I am EngineA ,I am 3.0Turbo
这时如果需要创建另一款SmallSUV车型,只需要创建另一个SmallSUV类,并新建第二个工厂,生产另外小型号的三种产品。
public class SmallSUV {
public Tyre mTyre;
public SteeringWheel mSteeringWheel;
public Engine mEngine;
public AbstractFactory mFactory;
public SmallSUV() {
mFactory=new SUVFactory2();
mTyre=mFactory.createTyre();
mSteeringWheel=mFactory.createSteeringWheel();
mEngine=mFactory.createEnginen();
}
public void show(){
mTyre.Roll();
mSteeringWheel.Turn();
mEngine.Roar();
}
}
测试类
public class Host {
public static void main(String[] args) {
SmallSUV mSUV=new SmallSUV();
mSUV.show();
}
}
输出
I am TyreB ,I am Small
I am SteeringWheelB ,I am Small
I am EngineB ,I am 1.6L
Android中的抽象工厂
暂时没遇到,欢迎知道的童鞋告知在下,先行谢过。