工厂模式是一种对象创建型模式,它提供了一种创建对象的最佳实践。在工厂模式中,我们在创建对象时不使用 new 关键字,而是通过调用工厂方法来创建对象。工厂方法是一种在子类中定义的方法,该方法负责实例化对象。工厂方法可以返回不同的对象类型,因此工厂模式可以创建一组相关或不相关的对象。这样就可以将对象的创建和使用解耦。
简介
工厂模式有三种类型:简单工厂模式、工厂方法模式和抽象工厂模式。
工厂方法模式
抽象工厂模式
好处和坏处
工厂模式的好处有:
可以将对象的创建和使用解耦,从而提高系统的灵活性和可维护性。
工厂模式可以隐藏对象的创建细节,使客户端只关心对象的使用,从而降低系统的复杂度。
工厂模式可以实现开闭原则,当需要增加新的产品时,只需要增加相应的工厂类即可,无需修改原有代码。
工厂模式的坏处有:
应用场景
工厂模式适用于以下场景:
当需要创建的对象类型较多时,可以使用工厂模式来避免大量的 new 操作。
当需要根据不同的条件或参数来创建不同类型的对象时,可以使用工厂模式来实现条件分支的替代。
当需要将对象的创建和使用解耦时,可以使用工厂模式来实现依赖倒置原则。
Java 代码示例
以下是使用 Java 实现的三种工厂模式的代码示例。
简单工厂模式
假设我们有一个 Shape 接口和三个实现类 Circle、Rectangle 和 Square,我们想要根据给定的形状类型来创建相应的对象。
首先,我们定义一个 Shape 接口和三个实现类:
// Shape接口public interface Shape { void draw();}
// Circle类public class Circle implements Shape { @Override public void draw() { System.out.println("Draw a circle"); }}
// Rectangle类public class Rectangle implements Shape { @Override public void draw() { System.out.println("Draw a rectangle"); }}
// Square类public class Square implements Shape { @Override public void draw() { System.out.println("Draw a square"); }}
复制代码
然后,我们定义一个 ShapeFactory 类,它有一个静态方法 createShape,根据给定的形状类型来返回相应的对象:
// ShapeFactory类public class ShapeFactory { // 静态方法createShape public static Shape createShape(String shapeType) { 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; }}
复制代码
最后,我们可以在客户端使用 ShapeFactory 来创建不同类型的形状对象:
// 客户端public class Client { public static void main(String[] args) { // 创建一个圆形对象 Shape circle = ShapeFactory.createShape("circle"); // 调用圆形对象的draw方法 circle.draw(); // 创建一个矩形对象 Shape rectangle = ShapeFactory.createShape("rectangle"); // 调用矩形对象的draw方法 rectangle.draw(); // 创建一个正方形对象 Shape square = ShapeFactory.createShape("square"); // 调用正方形对象的draw方法 square.draw(); }}
复制代码
输出结果如下:
Draw a circleDraw a rectangleDraw a square
复制代码
工厂方法模式
假设我们还是有一个 Shape 接口和三个实现类 Circle、Rectangle 和 Square,我们想要根据给定的形状类型来创建相应的对象,但是我们不想使用一个工厂类来创建所有类型的对象,而是让每个形状类自己定义一个工厂类来创建自己的对象。
首先,我们定义一个 Shape 接口和三个实现类,这里和简单工厂模式一样:
// Shape接口public interface Shape { void draw();}
// Circle类public class Circle implements Shape { @Override public void draw() { System.out.println("Draw a circle"); }}
// Rectangle类public class Rectangle implements Shape { @Override public void draw() { System.out.println("Draw a rectangle"); }}
// Square类public class Square implements Shape { @Override public void draw() { System.out.println("Draw a square"); }}
复制代码
然后,我们定义一个抽象工厂类 ShapeFactory,它有一个抽象方法 createShape,返回一个 Shape 对象:
// 抽象工厂类ShapeFactorypublic abstract class ShapeFactory { // 抽象方法createShape public abstract Shape createShape();}
复制代码
接着,我们让每个形状类都定义一个工厂类,继承自 ShapeFactory,并实现 createShape 方法,返回自己的对象:
// CircleFactory类public class CircleFactory extends ShapeFactory { @Override public Shape createShape() { return new Circle(); }}
// RectangleFactory类public class RectangleFactory extends ShapeFactory { @Override public Shape createShape() { return new Rectangle(); }}
// SquareFactory类public class SquareFactory extends ShapeFactory { @Override public Shape createShape() { return new Square(); }}
复制代码
最后,我们可以在客户端使用不同的工厂类来创建不同类型的形状对象:
// 客户端public class Client { public static void main(String[] args) { // 创建一个圆形工厂对象 ShapeFactory circleFactory = new CircleFactory(); // 通过圆形工厂对象创建一个圆形对象 Shape circle = circleFactory.createShape(); // 调用圆形对象的draw方法 circle.draw(); // 创建一个矩形工厂对象 ShapeFactory rectangleFactory = new RectangleFactory(); // 通过矩形工厂对象创建一个矩形对象 Shape rectangle = rectangleFactory.createShape(); // 调用矩形对象的draw方法 rectangle.draw(); // 创建一个正方形工厂对象 ShapeFactory squareFactory = new SquareFactory(); // 通过正方形工厂对象创建一个正方形对象 Shape square = squareFactory.createShape(); // 调用正方形对象的draw方法 square.draw(); }}
复制代码
输出结果如下:
Draw a circleDraw a rectangleDraw a square
复制代码
抽象工厂模式
假设我们不仅有一个 Shape 接口和三个实现类 Circle、Rectangle 和 Square,还有一个 Color 接口和三个实现类 Red、Green 和 Blue,我们想要根据给定的类型来创建相应的形状或颜色对象。
首先,我们定义一个 Shape 接口和三个实现类,这里和前面两种模式一样:
// Shape接口public interface Shape { void draw();}
// Circle类public class Circle implements Shape { @Override public void draw() { System.out.println("Draw a circle"); }}
// Rectangle类public class Rectangle implements Shape { @Override public void draw() { System.out.println("Draw a rectangle"); }}
// Square类public class Square implements Shape { @Override public void draw() { System.out.println("Draw a square"); }}
复制代码
然后,我们定义一个 Color 接口和三个实现类:
// Color接口public interface Color { void fill();}
// Red类public class Red implements Color { @Override public void fill() { System.out.println("Fill with red"); }}
// Green类public class Green implements Color { @Override public void fill() { System.out.println("Fill with green"); }}
// Blue类public class Blue implements Color { @Override public void fill() { System.out.println("Fill with blue"); }}
复制代码
接着,我们定义一个抽象工厂类 AbstractFactory,它有两个抽象方法 createShape 和 createColor,分别返回一个 Shape 对象和一个 Color 对象:
// 抽象工厂类AbstractFactorypublic abstract class AbstractFactory { // 抽象方法createShape public abstract Shape createShape(String shapeType); // 抽象方法createColor public abstract Color createColor(String colorType);}
复制代码
然后,我们定义三个具体工厂类,分别是 ShapeFactory、ColorFactory 和 MixedFactory,它们都继承自 AbstractFactory,并实现其抽象方法。其中,ShapeFactory 只负责创建形状对象,ColorFactory 只负责创建颜色对象,MixedFactory 既可以创建形状对象又可以创建颜色对象:
// ShapeFactory类public class ShapeFactory extends AbstractFactory { @Override public Shape createShape(String shapeType) { 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 createColor(String colorType) { // 不支持创建颜色对象,返回null return null; }}
// ColorFactory类public class ColorFactory extends AbstractFactory { @Override public Shape createShape(String shapeType) { // 不支持创建形状对象,返回null return null; }
@Override public Color createColor(String colorType) { if (colorType == null) { return null; } if (colorType.equalsIgnoreCase("red")) { return new Red(); } else if (colorType.equalsIgnoreCase("green")) { return new Green(); } else if (colorType.equalsIgnoreCase("blue")) { return new Blue(); } return null; }}
// MixedFactory类public class MixedFactory extends AbstractFactory { @Override public Shape createShape(String shapeType) { // 可以创建形状对象,逻辑同ShapeFactory类 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 createColor(String colorType) { // 可以创建颜色对象,逻辑同ColorFactory类 if (colorType == null) { return null; } if (colorType.equalsIgnoreCase("red")) { return new Red(); } else if (colorType.equalsIgnoreCase("green")) { return new Green(); } else if (colorType.equalsIgnoreCase("blue")) { return new Blue(); } return null; }}
复制代码
最后,我们可以在客户端使用不同的工厂类来创建不同类型的形状或颜色对象:
// 客户端public class Client { public static void main(String[] args) { // 创建一个形状工厂对象 AbstractFactory shapeFactory = new ShapeFactory(); // 通过形状工厂对象创建一个圆形对象 Shape circle = shapeFactory.createShape("circle"); // 调用圆形对象的draw方法 circle.draw(); // 通过形状工厂对象创建一个矩形对象 Shape rectangle = shapeFactory.createShape("rectangle"); // 调用矩形对象的draw方法 rectangle.draw(); // 通过形状工厂对象创建一个正方形对象 Shape square = shapeFactory.createShape("square"); // 调用正方形对象的draw方法 square.draw();
// 创建一个颜色工厂对象 AbstractFactory colorFactory = new ColorFactory(); // 通过颜色工厂对象创建一个红色对象 Color red = colorFactory.createColor("red"); // 调用红色对象的fill方法 red.fill(); // 通过颜色工厂对象创建一个绿色对象 Color green = colorFactory.createColor("green"); // 调用绿色对象的fill方法 green.fill(); // 通过颜色工厂对象创建一个蓝色对象 Color blue = colorFactory.createColor("blue"); // 调用蓝色对象的fill方法 blue.fill();
// 创建一个混合工厂对象 AbstractFactory mixedFactory = new MixedFactory(); // 通过混合工厂对象创建一个圆形和红色的组合对象 Shape circleRed = mixedFactory.createShape("circle"); Color redRed = mixedFactory.createColor("red"); // 调用组合对象的draw和fill方法 circleRed.draw(); redRed.fill(); }}
复制代码
输出结果如下:
Draw a circleDraw a rectangleDraw a squareFill with redFill with greenFill with blueDraw a circleFill with red
复制代码
Spring 代码示例
这里对上述的抽象工厂代码示进行 Spring 框架下的改造。
先改造 Shape 接口的实现类加上 @Component 注解
// Shape接口public interface Shape { void draw();}
// Circle类@Componentpublic class Circle implements Shape { @Override public void draw() { System.out.println("Draw a circle"); }}
// Rectangle类@Componentpublic class Rectangle implements Shape { @Override public void draw() { System.out.println("Draw a rectangle"); }}
// Square类@Componentpublic class Square implements Shape { @Override public void draw() { System.out.println("Draw a square"); }}
复制代码
然后在改造 Color 接口的三个实现类加上 @Component 注解
// Color接口public interface Color { void fill();}
// Red类@Componentpublic class Red implements Color { @Override public void fill() { System.out.println("Fill with red"); }}
// Green类@Componentpublic class Green implements Color { @Override public void fill() { System.out.println("Fill with green"); }}
// Blue类@Componentpublic class Blue implements Color { @Override public void fill() { System.out.println("Fill with blue"); }}
复制代码
抽象工厂类 AbstractFactory 的具体工厂 MixedFactory 类加上 @Component 注解
// 抽象工厂类AbstractFactorypublic abstract class AbstractFactory { // 抽象方法createShape public abstract Shape createShape(String shapeType); // 抽象方法createColor public abstract Color createColor(String colorType);}
// MixedFactory类@Componentpublic class MixedFactory extends AbstractFactory { @Autowired private Map<String, Shape> shapeMap; @Autowired private Map<String, Color> colorMap;
@Override public Shape createShape(String shapeType) { // 可以创建形状对象,逻辑同ShapeFactory类 if (shapeType == null) { return null; } return shapeMap.getOrDefault(shapeType, null); }
@Override public Color createColor(String colorType) { // 可以创建颜色对象,逻辑同ColorFactory类 if (colorType == null) { return null; } return colorMap.getOrDefault(colorType, null); }}
复制代码
最后我们可以在客户端使用不同的工厂类来创建不同类型的形状或颜色对象:
// 客户端// 测试类@SpringBootTest@RunWith(SpringRunner.class)public class FactoryTest {
// 从Spring容器中获取Context对象 @Autowired private MixedFactory mixedFactory;
@Test public void test() { Shape circle = mixedFactory.createShape("circle"); circle.draw(); Shape rectangle = mixedFactory.createShape("rectangle"); rectangle.draw(); Shape square = mixedFactory.createShape("square"); square.draw();
Color red = mixedFactory.createColor("red"); red.fill(); Color green = mixedFactory.createColor("green"); green.fill(); Color blue = mixedFactory.createColor("blue"); blue.fill(); }}
复制代码
输出结果如下:
Draw a circleDraw a rectangleDraw a squareFill with redFill with greenFill with blue
复制代码
总结
总之工厂模式通过引入一个工厂类来负责实例化其他类,可以实现解耦、扩展性好和提高代码可读性。它是 Java 开发中最常用和最基础的设计模式之一。熟练使用工厂模式可以大大提高我们的代码质量和开发效率。
关注公众号【waynblog】每周分享技术干货、开源项目、实战经验、高效开发工具等,您的关注将是我的更新动力!
评论