抽象工厂模式

  • 用来生产不同产品族的全部产品。对于增加新的产品,无能为力,支持增加产品族
  • 抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种、业务分类时,通过抽象工厂模式产生需要的对象是一种非常好的解决方式


类图




引擎

引擎接口:EngineInterface.java

package shejimoshi.factory.abstractfactory.engine;

/**
* 高端引擎
*
* @author 陈霓清
*/
public class LuxuryEngine implements EngineInterface {

@Override
public void run() {
System.out.println("转得快!");
}

@Override
public void start() {
System.out.println("启动快!可以自动启停!");
}

}

高端引擎实现类:LuxuryEngine.java

package shejimoshi.factory.abstractfactory.engine;

/**
* 高端引擎
*
* @author 陈霓清
*/
public class LuxuryEngine implements EngineInterface {

@Override
public void run() {
System.out.println("转得快!");
}

@Override
public void start() {
System.out.println("启动快!可以自动启停!");
}

}

低端引擎实现类:LowEngine.java

package shejimoshi.factory.abstractfactory.engine;

/**
* 低端引擎
*
* @author 陈霓清
*/
public class LowEngine implements EngineInterface {

@Override
public void run() {
System.out.println("转得慢!");
}

@Override
public void start() {
System.out.println("启动慢!");
}

}


座椅

座椅接口:SeatInterface.java

package shejimoshi.factory.abstractfactory.seat;

/**
* 座椅
*
* @author 陈霓清
*/
public interface SeatInterface {
void massage();
}

高端座椅实现类:LuxurySeat.java

package shejimoshi.factory.abstractfactory.seat;

/**
* 高端座椅
*
* @author 陈霓清
*/
public class LuxurySeat implements SeatInterface {

@Override
public void massage() {
System.out.println("可以自动按摩!");
}

}

低端座椅实现类:LowSeat.java

package shejimoshi.factory.abstractfactory.seat;

/**
* 低端座椅
*
* @author 陈霓清
*/
public class LowSeat implements SeatInterface {

@Override
public void massage() {
System.out.println("不能按摩!");
}

}


轮胎

轮胎接口:TyreInterface.java

package shejimoshi.factory.abstractfactory.tyre;

/**
* 轮胎
*
* @author 陈霓清
*/
public interface TyreInterface {
void revolve();
}

高端轮胎实现类:LuxuryTyre.java

package shejimoshi.factory.abstractfactory.tyre;

/**
* 高端轮胎
*
* @author 陈霓清
*/
public class LuxuryTyre implements TyreInterface {

@Override
public void revolve() {
System.out.println("旋转不磨损!");
}

}

低端轮胎实现类:LowTyre.java

package shejimoshi.factory.abstractfactory.tyre;

/**
* 低端轮胎
*
* @author 陈霓清
*/
public class LowTyre implements TyreInterface {

@Override
public void revolve() {
System.out.println("旋转磨损快!");
}

}


汽车工厂

汽车接口:CarInterface.java

package shejimoshi.factory.abstractfactory.car;

import shejimoshi.factory.abstractfactory.engine.EngineInterface;
import shejimoshi.factory.abstractfactory.seat.SeatInterface;
import shejimoshi.factory.abstractfactory.tyre.TyreInterface;

/**
* 汽车接口
*
* @author 陈霓清
*/
public interface CarInterface {
EngineInterface createEngine();

SeatInterface createSeat();

TyreInterface createTyre();
}

高端汽车工厂实现类:LuxuryCarFactory.java

package shejimoshi.factory.abstractfactory.car;

import shejimoshi.factory.abstractfactory.engine.EngineInterface;
import shejimoshi.factory.abstractfactory.engine.LuxuryEngine;
import shejimoshi.factory.abstractfactory.seat.LuxurySeat;
import shejimoshi.factory.abstractfactory.seat.SeatInterface;
import shejimoshi.factory.abstractfactory.tyre.LuxuryTyre;
import shejimoshi.factory.abstractfactory.tyre.TyreInterface;

/**
* 高端汽车
*
* @author 陈霓清
*/
public class LuxuryCarFactory implements CarInterface {

@Override
public EngineInterface createEngine() {
return new LuxuryEngine();
}

@Override
public SeatInterface createSeat() {
return new LuxurySeat();
}

@Override
public TyreInterface createTyre() {
return new LuxuryTyre();
}

}

低端汽车工厂实现类LowCarFactory.java

package shejimoshi.factory.abstractfactory.car;

import shejimoshi.factory.abstractfactory.engine.EngineInterface;
import shejimoshi.factory.abstractfactory.engine.LowEngine;
import shejimoshi.factory.abstractfactory.seat.LowSeat;
import shejimoshi.factory.abstractfactory.seat.SeatInterface;
import shejimoshi.factory.abstractfactory.tyre.LowTyre;
import shejimoshi.factory.abstractfactory.tyre.TyreInterface;

/**
* 低端汽车
*
* @author 陈霓清
*/
public class LowCarFactory implements CarInterface {

@Override
public EngineInterface createEngine() {
return new LowEngine();
}

@Override
public SeatInterface createSeat() {
return new LowSeat();
}

@Override
public TyreInterface createTyre() {
return new LowTyre();
}

}


调用代码:Client.java

package shejimoshi.factory.abstractfactory;

import shejimoshi.factory.abstractfactory.car.CarInterface;
import shejimoshi.factory.abstractfactory.car.LuxuryCarFactory;
import shejimoshi.factory.abstractfactory.engine.EngineInterface;

public class Client {

public static void main(String[] args) {
CarInterface car = new LuxuryCarFactory();
EngineInterface e = car.createEngine();

e.run();
e.start();
}
}


回到顶部