工厂模式
本文参考于:工厂模式 | 菜鸟教程 (runoob.com),设计模式篇——工厂模式详解 - 知乎 (zhihu.com)
工厂方法
创建接口
Human
1 2 3 4 5 6
| public interface Human {
void printColor(); }
|
编写实体类
BlackHuman
1 2 3 4 5 6
| public class BlackHuman implements Human{ @Override public void printColor() { System.out.println("黑种人"); } }
|
WhiteHuman
1 2 3 4 5 6
| public class WhiteHuman implements Human{ @Override public void printColor() { System.out.println("白种人"); } }
|
YellowHuman
1 2 3 4 5 6
| public class YellowHuman implements Human{ @Override public void printColor() { System.out.println("黄种人"); } }
|
创建工厂
HumanFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class HumanFactory { public Human createHuman(String color){ if (color==null) { return null; } switch (color) { case "BlackHuman": return new BlackHuman(); case "WhiteHuman": return new WhiteHuman(); case "YellowHuman": return new YellowHuman(); default: return null; } } }
|
测试
Test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Test { public static void main(String[] args) { HumanFactory factory = new HumanFactory(); Human blackHuman = factory.createHuman("BlackHuman"); blackHuman.printColor(); Human whiteHuman = factory.createHuman("WhiteHuman"); whiteHuman.printColor(); Human yellowHuman = factory.createHuman("YellowHuman"); yellowHuman.printColor(); } }
|
优缺点
优点: 1、一个调用者想创建一个对象,只要知道其名称就可以了。 2、扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。 3、屏蔽产品的具体实现,调用者只关心产品的接口。
缺点:每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。
抽象工厂
创建接口
Shape
1 2 3 4 5 6
| public interface Shape {
void printShape(); }
|
Color
1 2 3 4 5 6
| public interface Color {
void printColor(); }
|
编写实体类
Shape的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class Rectangle implements Shape{ @Override public void printShape() { System.out.println("矩形"); } }
public class Triangle implements Shape{ @Override public void printShape() { System.out.println("三角形"); } }
public class Circle implements Shape{ @Override public void printShape() { System.out.println("圆形"); } }
|
Color的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class Red implements Color{ @Override public void printColor() { System.out.println("红色"); } }
public class Yellow implements Color{ @Override public void printColor() { System.out.println("黄色"); } } public class Blue implements Color{ @Override public void printColor() { System.out.println("蓝色"); } }
|
创建抽象工厂
AbstractFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public abstract class AbstractFactory {
public abstract Shape getShape(String shape);
public abstract Color getColor(String color); }
|
创建具体工厂
ShapeFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class ShapeFactory extends AbstractFactory{ @Override public Shape getShape(String shape) { if (shape==null){ return null; } switch (shape) { case "Rectangle": return new Rectangle(); case "Triangle": return new Triangle(); case "Circle": return new Circle(); default: return null; } }
@Override public Color getColor(String color) { return null; }
}
|
ColorFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class ColorFactory extends AbstractFactory { @Override public Shape getShape(String shape) { return null; }
@Override public Color getColor(String color) { if (color==null){ return null; } switch (color) { case "Red": return new Red(); case "Yellow": return new Yellow(); case "Blue": return new Blue(); default: return null; } } }
|
获取工厂
FactoryProducer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class FactoryProducer { public AbstractFactory getFactory(String choice){ if (choice==null) { return null; } switch (choice) { case "Shape": return new ShapeFactory(); case "Color": return new ColorFactory(); default: return null; } } }
|
测试
Test
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Test { public static void main(String[] args) { FactoryProducer factoryProducer = new FactoryProducer(); AbstractFactory shapeFactory = factoryProducer.getFactory("Shape"); Shape triangle = shapeFactory.getShape("Triangle"); triangle.printShape();
AbstractFactory colorFactory = factoryProducer.getFactory("Color"); Color blue = colorFactory.getColor("Blue"); blue.printColor(); } }
|
优缺点
优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。