在软件开发中,简单工厂模式和工厂方法模式是两种常用的创建型设计模式。尽管它们都用于创建对象,但它们的实现方式和应用场景有所不同。本文将详细探讨这两种模式的区别,帮助你更好地理解和应用它们。
简单工厂模式,又称静态工厂方法模式,它通过一个工厂类的静态方法,根据传入的参数来决定创建哪一种产品对象。
// 产品接口 public interface Shape { void draw(); } // 具体产品类 public class Circle implements Shape { @Override public void draw() { System.out.println("Drawing a Circle"); } } public class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing a Rectangle"); } } // 工厂类 public class ShapeFactory { 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(); } return null; } } // 客户端 public class Client { public static void main(String[] args) { Shape shape1 = ShapeFactory.createShape("CIRCLE"); shape1.draw(); Shape shape2 = ShapeFactory.createShape("RECTANGLE"); shape2.draw(); } }
工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。它使一个类的实例化延迟到其子类。
// 产品接口 public interface Shape { void draw(); } // 具体产品类 public class Circle implements Shape { @Override public void draw() { System.out.println("Drawing a Circle"); } } public class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing a Rectangle"); } } // 抽象工厂类 public abstract class ShapeFactory { public abstract Shape createShape(); } // 具体工厂类 public class CircleFactory extends ShapeFactory { @Override public Shape createShape() { return new Circle(); } } public class RectangleFactory extends ShapeFactory { @Override public Shape createShape() { return new Rectangle(); } } // 客户端 public class Client { public static void main(String[] args) { ShapeFactory circleFactory = new CircleFactory(); Shape shape1 = circleFactory.createShape(); shape1.draw(); ShapeFactory rectangleFactory = new RectangleFactory(); Shape shape2 = rectangleFactory.createShape(); shape2.draw(); } }
复杂度:
扩展性:
灵活性:
在软件设计中,工厂方法模式(Factory Method Pattern)和抽象工厂模式(Abstract Factory Pattern)都是创建型设计模式,它们都用于创建对象,但它们的实现方式和应用场景有所不同。本文将详细探讨这两种模式的区别,帮助你更好地理解和应用它们。
工厂方法模式通过定义一个创建对象的接口,让子类决定实例化哪一个类。这样,工厂方法模式让类的实例化延迟到子类。
// 抽象产品接口 public interface Shape { void draw(); } // 具体产品类 public class Circle implements Shape { @Override public void draw() { System.out.println("Drawing a Circle"); } } public class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing a Rectangle"); } } // 抽象工厂类 public abstract class ShapeFactory { public abstract Shape createShape(); } // 具体工厂类 public class CircleFactory extends ShapeFactory { @Override public Shape createShape() { return new Circle(); } } public class RectangleFactory extends ShapeFactory { @Override public Shape createShape() { return new Rectangle(); } } // 客户端 public class Client { public static void main(String[] args) { ShapeFactory circleFactory = new CircleFactory(); Shape shape1 = circleFactory.createShape(); shape1.draw(); ShapeFactory rectangleFactory = new RectangleFactory(); Shape shape2 = rectangleFactory.createShape(); shape2.draw(); } }
抽象工厂模式提供一个接口,用于创建一系列相关或依赖对象的家族,而无需指定具体类。
关键在于客户端代码使用工厂时不需要关心具体的工厂和产品类是什么,而是通过工厂接口或抽象类来创建产品
// 抽象产品接口 public interface Shape { void draw(); } public interface Color { void fill(); } // 具体产品类 public class Circle implements Shape { @Override public void draw() { System.out.println("Drawing a Circle"); } } public class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing a Rectangle"); } } public class Red implements Color { @Override public void fill() { System.out.println("Filling with Red"); } } public class Blue implements Color { @Override public void fill() { System.out.println("Filling with Blue"); } } // 抽象工厂接口 public interface AbstractFactory { Shape createShape(String shapeType); Color createColor(String colorType); } // 具体工厂类 public class ShapeFactory implements 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(); } return null; } @Override public Color createColor(String colorType) { return null; // ShapeFactory 不负责创建颜色对象 } } public class ColorFactory implements AbstractFactory { @Override public Shape createShape(String shapeType) { return null; // ColorFactory 不负责创建形状对象 } @Override public Color createColor(String colorType) { if (colorType == null) { return null; } if (colorType.equalsIgnoreCase("RED")) { return new Red(); } else if (colorType.equalsIgnoreCase("BLUE")) { return new Blue(); } return null; } } // 客户端 public class Client { public static void main(String[] args) { AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE"); Shape shape1 = shapeFactory.createShape("CIRCLE"); shape1.draw(); Shape shape2 = shapeFactory.createShape("RECTANGLE"); shape2.draw(); AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR"); Color color1 = colorFactory.createColor("RED"); color1.fill(); Color color2 = colorFactory.createColor("BLUE"); color2.fill(); } } // 工厂生成器类 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; } }
产品维度:
工厂接口的复杂度:
扩展性:
应用场景:
工厂方法模式和抽象工厂模式都是用于创建对象的设计模式,但它们在复杂度和应用场景上有所不同。工厂方法模式适用于单一产品的创建,而抽象工厂模式适用于创建一组相关的产品对象。在实际开发中,选择合适的模式可以提高代码的灵活性和可维护性。希望通过这篇文章,你能更好地理解这两种模式,并在合适的场景中应用它们。