工厂模式简介
工厂模式是我们日常开发工作中经常使用的设计模式,它属于创建型设计模式,我们常用的工厂模式有:简单工厂模式
,工厂方法模式
,抽象工厂模式
三种类型;下面我们一起来领略一下它们各自的特点。
简单工厂模式
简单工厂模式,又称为静态工厂模式;其核心是工厂类,包含了必要的处理逻辑,可以根据外界跟定的信息,返回指定的结果。参考以下示例:
Car
//定义Car
public interface Car {
String getName();
}
//Jeep实现类
public class Jeep implements Car {
@Override
public String getName() {
return "Jeep";
}
}
//Truck实现类
public class Truck implements Car {
@Override
public String getName() {
return "Truck";
}
}
//Cooper实现类
public class Cooper implements Car {
@Override
public String getName() {
return "Cooper";
}
}
Factory
//Car 工厂类,根据传入的name,生产具体的Car
public class CarFactory {
public Car getCar(String name) {
switch (name) {
case "Jeep":
return new Jeep();
case "Truck":
return new Truck();
case "Cooper":
return new Cooper();
default:
throw new RuntimeException();
}
}
}
演示Demo
public class Demo {
public static void main(String[] args) {
CarFactory factory = new CarFactory();
Car jeep = factory.getCar("Jeep");
System.out.println(jeep.getName());
Car truck = factory.getCar("Truck");
System.out.println(truck.getName());
Car cooper = factory.getCar("Cooper");
System.out.println(cooper.getName());
}
}
当然,简单工厂的缺点也很明显:
- 增加新的产品,需要修改Factory的代码
- 不存在所有Object都可以生产的工厂
工厂方法模式的出现,对这些缺点有了一定的克服。
工厂方法模式
工厂方法模式的特点是在工厂类中定义了创建产品的接口,而具体的实现交由子类工厂去实现;通过此种方式将变化
封装了起来,遵守开闭原则
,实现了可扩展;参看以下示例。
public interface CarFactory {
// 定义了获取Car接口
Car getCar();
}
//Jeep工厂类
public class JeepFactory implements CarFactory {
@Override
public Car getCar() {
return new Jeep();
}
}
//Cooper工厂类
public class CooperFactory implements CarFactory {
@Override
public Car getCar() {
return new Cooper();
}
}
//Truck工厂类
public class TruckFactory implements CarFactory {
@Override
public Car getCar() {
return new Truck();
}
}
演示Demo
public class Demo {
public static void main(String[] args) {
CarFactory jeepFactory = new JeepFactory();
Car jeep = jeepFactory.getCar();
System.out.println(jeep.getName());
TruckFactory truckFactory = new TruckFactory();
Car truck = truckFactory.getCar();
System.out.println(truck.getName());
CooperFactory cooperFactory = new CooperFactory();
Car cooper = cooperFactory.getCar();
System.out.println(cooper.getName());
}
}
工厂方法模式是对简单工厂做了抽象,工厂方法模式是典型的模板模式的应用。通过代码,我们发现:工厂方法模式的较简单工厂而言实现比较复杂
,对于可以形成产品族的情况较难处理
。
其中针对对于可以形成产品族的情况较难处理
这里情况,在抽象工厂模式
中得到一定的处理。
抽象工厂模式
抽象工厂模式是为一系列相关的,或者有依赖关系的对象创建提供接口;我们可以参考一下代码:
public interface Shape {
String draw();
}
public interface Color {
String fill();
}
public class Circle implements Shape {
@Override
public String draw() {
return "circle";
}
}
public class Square implements Shape {
@Override
public String draw() {
return "square";
}
}
public class Rectangle implements Shape {
@Override
public String draw() {
return "rectangle";
}
}
public class Green implements Color {
@Override
public String fill() {
return "green";
}
}
public class Red implements Color {
@Override
public String fill() {
return "red";
}
}
public class Blue implements Color {
@Override
public String fill() {
return "blue";
}
}
AbstractFactory
public interface AbstractFactory {
Shape getShape(String shape);
Color getColor(String color);
}
public class ShapeFactory implements AbstractFactory {
@Override
public Shape getShape(String shape) {
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
@Override
public Color getColor(String color){
return null;
}
}
public class ColorFactory implements AbstractFactory {
@Override
public Shape getShape(String shape) {
return null;
}
@Override
public Color getColor(String color){
if(color == null){
return null;
}
if(color.equalsIgnoreCase("RED")){
return new Red();
} else if(color.equalsIgnoreCase("GREEN")){
return new Green();
} else if(color.equalsIgnoreCase("BLUE")){
return new Blue();
}
return null;
}
}
FactoryProducer.java
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}
return null;
}
}
Demo.java
public class Demo {
public static void main(String[] args) {
//获取形状工厂
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//获取形状为 Circle 的对象
Shape shape1 = shapeFactory.getShape("CIRCLE");
//调用 Circle 的 draw 方法
shape1.draw();
//获取形状为 Rectangle 的对象
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//调用 Rectangle 的 draw 方法
shape2.draw();
//获取形状为 Square 的对象
Shape shape3 = shapeFactory.getShape("SQUARE");
//调用 Square 的 draw 方法
shape3.draw();
//获取颜色工厂
AbstractFactory colorFactory =FactoryProducer.getFactory("COLOR");
//获取颜色为 Red 的对象
Color color1 = colorFactory.getColor("RED");
//调用 Red 的 fill 方法
color1.fill();
//获取颜色为 Green 的对象
Color color2 = colorFactory.getColor("GREEN");
//调用 Green 的 fill 方法
color2.fill();
//获取颜色为 Blue 的对象
Color color3 = colorFactory.getColor("BLUE");
//调用 Blue 的 fill 方法
color3.fill();
}
}
相较于工厂方法模式,抽象工厂模式对产品族(相关联的对象)支持的较好,但是还是没有解决实现复杂
的问题;而针对这一问题,我们可以通过与简单工厂的结合使用,或者与反射的结合使用来降低实现的复杂度。
- 本文链接: https://www.sunce.wang/archives/设计模式之构建万物的工厂模式
- 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!