抽象工厂模式


23种设计模式


抽象工厂模式,简单工厂模式的名字,在学校经常能够听到,可能是上学那会不懂得学习吧,一直没有系统的,完整的学习这些设计模式。现在有机会能系统的学习,做了一个较大的实例体验抽象工厂模式。

1.抽象工厂模式是什么

抽象工厂模式是由抽象工厂组装抽象零件形成抽象产品的过程。
很酷,全都是抽象。
不过这样做的目的就是创建复杂实例的过程与定义过程尽可能的分离,尽可能的解耦。

2.角色说明

抽象工厂:组装抽象零件,创建抽象产品
抽象零件:抽象产品中分离的一部分
抽象产品:抽象工厂的组装品。

能看懂吗,这太抽象了。

接下来我将使用一个例子(有关于车的)来说明,这些是例子中出现的角色:
工厂工具:这是一个工具类,作用就是根据传入的值通过反射找到对应的类,最后生成对应类的对象并返回。
车的行为接口:定义了作为一个车这个东西,应该具有的行为,这是一个接口。
车的属性实体:定义了车的一些基本的属性,这是一个普通类。
属性工厂:车的属性的工厂类,这是一个普通类。

注意:属性工厂实际意义上并不是工厂模式的实现,而是一个普通类,拥有一个方法用于返回创建的属性对象,因为属性对象是一个普通类,所以,本类也是一个普通类。
因为方法类似工厂类,所以叫做属性工厂类。

抽象零件工厂:所有实现零件工厂的基类或者接口(ps:我是使用基类继承机制实现的,不过也可以使用接口的方式实现)
抽象产品工厂:所有实现产品工厂的基类或者接口(同抽象零件工厂)

把车抽象了4种零件:
刹车
轮胎
发动机
外壳
测试类:Main方法主方法

3.抽象部分设计

3.1抽象产品–机动车


/**
* 机动车
* 定义一个机动车
*
*/
public abstract class Motor extends CarField implements Car{

public Motor(String name) {
super(name);
}

//最重要的一点,也是机动车和非机动车一般意义上最大的区别
public abstract void needOil();

public abstract void needPaper();

public abstract void saySelf();
}

3.2抽象产品–非机动车

/**
* 非机动车
*
*/
public abstract class NoMotor extends CarField implements Car{

public NoMotor(String name) {
super(name);
}

//非机动车环保
public abstract void environment();

public abstract void saySelf();
}

3.3抽象零件–刹车

//刹车
public interface Brake {

//刹车的类型
public abstract void type();

}

3.4抽象零件–轮胎

//车轮
public interface Cycl {

//车轮能干什么,车轮的一些特点
public abstract void show();

}

3.5抽象零件–发动机

//发动机
public interface Engine {

public abstract void performance();

}

3.6抽象零件–车外壳

//车外壳
public interface FrameCar {

//外壳的特点
public abstract void characteristic();

}

3.7抽象零件工厂

//抽象零件工厂(可以接口实现)
public abstract class PartFactory extends AbsFactory{

// 创建抽象零件
public abstract Brake createBrake();

public abstract Cycl createCycl();

public abstract Engine createEngine();

public abstract FrameCar createFrameCar();

}

3.8抽象产品工厂


import java.awt.Color;

//抽象产品工厂
public abstract class ProductFactory extends AbsFactory {

// 抽象产品
// 创建机动车或者非机动车
// 创建机动车的步骤
// 这里的name具体车的name
public Motor createMotor(String name) {
// 1.确定车的名字
CarField carField = FieldFactory.createCarField(name, 4, Color.RED);
carField.saySelf();
// 2.车的零件
PartFactory partFactory = (PartFactory) PartFactory.getFactory(name
+ "PartFactory");
Brake brake = partFactory.createBrake();
if (brake == null)
System.out.println("ProductFactory#createMotor--no brake");
else
brake.type();
Cycl cycl = partFactory.createCycl();
if (cycl == null)
System.out.println("ProductFactory#createMotor--no cycl");
else
cycl.show();
Engine engine = partFactory.createEngine();
if (engine == null)
System.out.println("ProductFactory#createMotor--no engine");
else
engine.performance();
FrameCar frameCar = partFactory.createFrameCar();
if (frameCar == null)
System.out.println("ProductFactory#createMotor--no frameCar");
else
frameCar.characteristic();
// 3.组装车
Motor motor = getMotor(name + "Motor");
if (motor == null) {
System.out.println("ProductFactory#createMoto--no Motor");
return null;
}
motor.canRun();
motor.canCarryingPeople();
motor.canCarryingThing();
motor.needOil();
motor.needPaper();
motor.saySelf();
return motor;
}

protected abstract Motor getMotor(String name);

public NoMotor createNoMotor(String name) {
// 1.确定车的名字
CarField carField = FieldFactory.createCarField(name, 4, Color.RED);
carField.saySelf();
// 2.车的零件
PartFactory partFactory = (PartFactory) PartFactory.getFactory(name
+ "PartFactory");
Brake brake = partFactory.createBrake();
if (brake == null)
System.out.println("ProductFactory#createNoMotor--no brake");
else
brake.type();
Cycl cycl = partFactory.createCycl();
if (cycl == null)
System.out.println("ProductFactory#createNoMotor--no cycl");
else
cycl.show();
Engine engine = partFactory.createEngine();
if (engine == null)
System.out.println("ProductFactory#createNoMotor--no engine");
else
engine.performance();
FrameCar frameCar = partFactory.createFrameCar();
if (frameCar == null)
System.out.println("ProductFactory#createNoMotor--no frameCar");
else
frameCar.characteristic();
// 3.组装车
NoMotor noMotor = getNoMotor(name);
if(noMotor == null){
System.out.println("ProductFactory#createNoMotor--no NoMotor");
return null;
}
noMotor.canCarryingPeople();
noMotor.canCarryingThing();
noMotor.canRun();
noMotor.environment();
noMotor.saySelf();
return noMotor;
}

protected abstract NoMotor getNoMotor(String name);

}

4.实体类

4.1实体接口–车


/**
* 定义一个车的行为
* 1.能跑
* 2.能拉人
* 3.能拉货
*
*/

public interface Car {

void canRun();

void canCarryingPeople();

void canCarryingThing();

}

4.2实体类–车属性


import java.awt.Color;

public class CarField {

//车的名字
private String name;

// 车轮的个数
private Integer wheel;

// 车的颜色
private Color color;

public void saySelf(){
System.out.println("CarField#sayself");
System.out.println("name:"+name);
System.out.println("wheel:"+wheel);
System.out.println("color:"+color.toString());
}

// 机动车的名字从出厂的那一刻起,就确定了
public CarField(String name) {
this.name = name;
}

// 某些车只有车头,可以更换车厢之类的
public Integer getWheel() {
return wheel;
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

public void setWheel(Integer wheel) {
this.wheel = wheel;
}

public String getName() {
return name;
}
}

4.3实体类–车属性工厂

import java.awt.Color;
//属性工厂
public class FieldFactory{

// 创建属性
public static CarField createCarField(String name, Integer wheel, Color color) {
CarField carField = new CarField(name);
carField.setWheel(wheel);
carField.setColor(color);
return carField;
}

}

4.4实体类–工厂工具


"cn.com.startimes.AbstractFactory." 这段字符串就是你的包的名称,注意还有


//工厂工具类
public class AbsFactory {

public static AbsFactory getFactory(String className){
AbsFactory absFactory = null;
try{
absFactory = (AbsFactory) Class.forName("cn.com.startimes.AbstractFactory."+className).newInstance();
} catch (ClassNotFoundException exception){
System.err.println("ClassNotFound");
} catch (InstantiationException e) {
System.err.println("Instantiation");
} catch (IllegalAccessException e) {
System.err.println("IllegalAccess");
}
return absFactory;
}

}

5.零件具体实现

5.1 刹车零件–盘刹

//盘刹
public class PanBrake implements Brake{

@Override
public void type() {
System.out.println("PanBrake#type");
}

}

5.2 刹车零件–鼓刹

//鼓刹
public class GuBrake implements Brake{

@Override
public void type() {
System.out.println("GuBrake#type");
}

}

5.3 轮胎零件–真空胎

//真空胎
public class VacuoCycl implements Cycl{

@Override
public void show() {
System.out.println("VacuoCycl#show");
}

}

5.4 轮胎零件–内外胎

//内外胎
public class DoubleCycl implements Cycl{

@Override
public void show() {
System.out.println("DoubleCycl#show");
}

}

5.5 发动机零件–汽油

//汽油发动机
public class GasEngine implements Engine{

@Override
public void performance() {
System.out.println("GasEngine#performance");
}

}

5.6发动机零件–柴油

//柴油发动机
public class DervEngine implements Engine{

@Override
public void performance() {
System.out.println("DervEngine#performance");
}

}

5.7 车外壳零件–钢铁

//钢铁外壳
public class SteelFrameCar implements FrameCar{

@Override
public void characteristic() {
System.out.println("SteelFrameCar#characteristic");
}

}

5.8 车外壳零件–橡胶

//橡胶外壳
public class RubberFrameCar implements FrameCar{

@Override
public void characteristic() {
System.out.println("RubberFrameCar#characteristic");
}

}

6.零件工厂实现

6.1BMW

//BMW车
public class BMWPartFactory extends PartFactory{

@Override
public Brake createBrake() {
Brake brake = new PanBrake();
brake.type();
System.out.println("BMWPartFactory#createBrake");
return brake;
}

@Override
public Cycl createCycl() {
Cycl cycl = new VacuoCycl();
cycl.show();
System.out.println("BMWPartFactory#createCycl");
return cycl;
}

@Override
public Engine createEngine() {
Engine engine = new GasEngine();
engine.performance();
System.out.println("BMWPartFactory#createEngine");
return engine;
}

@Override
public FrameCar createFrameCar() {
FrameCar frameCar = new RubberFrameCar();
frameCar.characteristic();
System.out.println("BMWPartFactory#createFrameCar");
return frameCar;
}

}

6.2 自行车

//自行车
public class BikePartFactory extends PartFactory{

@Override
public Brake createBrake() {
Brake brake = new GuBrake();
brake.type();
System.out.println("BikePartFactory#createBrake");
return brake;
}

@Override
public Cycl createCycl() {
Cycl cycl = new DoubleCycl();
cycl.show();
System.out.println("BikePartFactory#createCycl");
return cycl;
}

@Override
public Engine createEngine() {
System.out.println("BikePartFactory#createEngine");
return null;
}

@Override
public FrameCar createFrameCar() {
System.out.println("BikePartFactory#createFrameCar");
return null;
}

}

7.抽象产品实现

7.1机动车–BMW

//BMW机动车
public class BMWMotor extends Motor{

public BMWMotor(String name) {
super(name);
System.out.println("BMWMotor#BMWMotor");
}

@Override
public void canRun() {
System.out.println("BMWMotor#canRun");
}

@Override
public void canCarryingPeople() {
System.out.println("BMWMotor#canCarryingPeople");
}

@Override
public void canCarryingThing() {
System.out.println("BMWMotor#canCarryingThing");
}

@Override
public void needOil() {
System.out.println("BMWMotor#needOil");
}

@Override
public void needPaper() {
System.out.println("BMWMotor#needPaper");
}

@Override
public void saySelf() {
System.out.println("BMWMotor#saySelf");
System.out.println();
}

}

7.2非机动车–BMW

//BMW非机动车
public class BMWNoMotor extends NoMotor{

public BMWNoMotor(String name) {
super(name);
System.out.println("BMWNoMotor#BMWNoMotor");
}

@Override
public void canRun() {
System.out.println("BMWNoMotor#canRun");
}

@Override
public void canCarryingPeople() {
System.out.println("BMWNoMotor#canCarryingPeople");
}

@Override
public void canCarryingThing() {
System.out.println("BMWNoMotor#canCarryingThing");
}

@Override
public void environment() {
System.out.println("BMWNoMotor#environment");
}

@Override
public void saySelf() {
System.out.println("BMWNoMotor#saySelf");
System.out.println();
}

}

7.3 非机动车–自行车


public class BikeNoMotor extends NoMotor{

public BikeNoMotor(String name) {
super(name);
System.out.println("BikeNoMotor#BikeNoMotor");
}

@Override
public void canRun() {
System.out.println("BikeNoMotor#canRun");
}

@Override
public void canCarryingPeople() {
System.out.println("BikeNoMotor#canCarryingPeople");
}

@Override
public void canCarryingThing() {
System.out.println("BikeNoMotor#canCarryingThing");
}

@Override
public void environment() {
System.out.println("BikeNoMotor#environment");
}

@Override
public void saySelf() {
System.out.println("BikeNoMotor#saySelf");
System.out.println();
}

}

8.产品工厂实现

8.1产品工厂–BMW

//BMW产品工厂
public class BMWProductFactory extends ProductFactory{

@Override
protected Motor getMotor(String name) {
System.out.println("BMWProductFactory#getMotor");
return new BMWMotor(name);
}

@Override
protected NoMotor getNoMotor(String name) {
System.out.println("BMWProductFactory#getNoMotor");
return new BMWNoMotor(name);
}

}

8.2产品工厂–自行车


public class BikeProductFactory extends ProductFactory{

@Override
protected Motor getMotor(String name) {
System.out.println("BikeProductFactory#getMotor");
return null;
}

@Override
protected NoMotor getNoMotor(String name) {
System.out.println("BikeProductFactory#getNoMotor");
return new BikeNoMotor(name);
}

}

9.测试类

Main:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
wannaCar(scanner.next());
}
scanner.close();
}
//这里的name为具体的工厂类的名字
private static void wannaCar(String name){
ProductFactory productFactory = (ProductFactory)AbsFactory.getFactory(name+"ProductFactory");
Motor motor = productFactory.createMotor(name);
if(motor == null)
System.out.println("Main#wannaCar--no Motor");
else
motor.saySelf();
NoMotor noMotor = productFactory.createNoMotor(name);
if(noMotor == null)
System.out.println("Main#wannaCar--no NoMotor");
else
noMotor.saySelf();
}

}

10.测试结果


输入:BMW

输出:

BMW
CarField#sayself
name:BMW
wheel:4
color:java.awt.Color[r=255,g=0,b=0]
PanBrake#type
BMWPartFactory#createBrake
PanBrake#type
VacuoCycl#show
BMWPartFactory#createCycl
VacuoCycl#show
GasEngine#performance
BMWPartFactory#createEngine
GasEngine#performance
RubberFrameCar#characteristic
BMWPartFactory#createFrameCar
RubberFrameCar#characteristic
BMWProductFactory#getMotor
BMWMotor#BMWMotor
BMWMotor#canRun
BMWMotor#canCarryingPeople
BMWMotor#canCarryingThing
BMWMotor#needOil
BMWMotor#needPaper
BMWMotor#saySelf

BMWMotor#saySelf

CarField#sayself
name:BMW
wheel:4
color:java.awt.Color[r=255,g=0,b=0]
PanBrake#type
BMWPartFactory#createBrake
PanBrake#type
VacuoCycl#show
BMWPartFactory#createCycl
VacuoCycl#show
GasEngine#performance
BMWPartFactory#createEngine
GasEngine#performance
RubberFrameCar#characteristic
BMWPartFactory#createFrameCar
RubberFrameCar#characteristic
BMWProductFactory#getNoMotor
BMWNoMotor#BMWNoMotor
BMWNoMotor#canCarryingPeople
BMWNoMotor#canCarryingThing
BMWNoMotor#canRun
BMWNoMotor#environment
BMWNoMotor#saySelf

BMWNoMotor#saySelf

输入:Bike

输出:

Bike
CarField#sayself
name:Bike
wheel:4
color:java.awt.Color[r=255,g=0,b=0]
GuBrake#type
BikePartFactory#createBrake
GuBrake#type
DoubleCycl#show
BikePartFactory#createCycl
DoubleCycl#show
BikePartFactory#createEngine
ProductFactory#createMotor--no engine
BikePartFactory#createFrameCar
ProductFactory#createMotor--no frameCar
BikeProductFactory#getMotor
ProductFactory#createMoto--no Motor
Main#wannaCar--no Motor
CarField#sayself
name:Bike
wheel:4
color:java.awt.Color[r=255,g=0,b=0]
GuBrake#type
BikePartFactory#createBrake
GuBrake#type
DoubleCycl#show
BikePartFactory#createCycl
DoubleCycl#show
BikePartFactory#createEngine
ProductFactory#createNoMotor--no engine
BikePartFactory#createFrameCar
ProductFactory#createNoMotor--no frameCar
BikeProductFactory#getNoMotor
BikeNoMotor#BikeNoMotor
BikeNoMotor#canCarryingPeople
BikeNoMotor#canCarryingThing
BikeNoMotor#canRun
BikeNoMotor#environment
BikeNoMotor#saySelf

BikeNoMotor#saySelf


注意点:抽象零件工厂、抽象产品工厂、抽象产品的实现类的命名规则 车名+抽象零件(产品)工厂名字 eg: PartFactory BMWPartFactory

11.总结

上述整个项目还是非常容易扩展的,扩展一个新的车只需要实现零件工厂、产品工厂、产品。

如果需要扩充零件,也是容易的,需要增加新的零件,然后在零件工厂的create方法中返回即可(此时增加的只是原有类型的衍生类).

如果需要增加新的零件部分,则需要对抽象产品工厂进行修改。

抽象工厂模式非常的清晰,什么类做什么事情分的非常的清晰,零件类,产品类、工厂类。。。需要什么就增加什么。