Java工厂模式

67 阅读4分钟

Java工厂模式

Java中的工厂模式是一种常用的设计模式,它属于创建型模式的一种。工厂模式主要用于创建对象,特别是在创建对象时需要考虑到系统的灵活性和可扩展性的情况下。它通过定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂模式使得一个类的实例化延迟到其子类。

工厂模式主要有三种变体:

  1. 简单工厂模式(Simple Factory): 不是设计模式的正式成员,更多的是一种编程习惯。在这种模式中,一个工厂类根据传入的参数决定创建出哪一种产品类的实例。虽然不易于扩展,但它对于某些简单场景来说是足够的。
  2. 工厂方法模式(Factory Method): 定义一个用于创建对象的接口,让子类决定将哪一个类实例化。工厂方法使得一个类的实例化延迟到其子类。这样的模式可以应对更复杂的业务场景,每个具体的工厂类负责创建特定的产品对象。
  3. 抽象工厂模式(Abstract Factory): 提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。这种模式一般用于系统的产品族较多,而且产品等级结构清晰的情况。

下面的例子中,我们将使用同一个场景:创建不同类型的形状对象(如圆形、正方形、长方形)。

1. 简单工厂模式(Simple Factory)

简单工厂模式通过一个中心化的工厂类来决定创建哪种类型的对象。

// 步骤 1:创建一个接口。
interface Shape {
    void draw();
}

// 步骤 2:创建实现接口的实体类。
class Rectangle implements Shape {
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}

class Square implements Shape {
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

// 步骤 3:创建一个工厂类。
class ShapeFactory {
    // 使用 getShape 方法获取形状类型的对象
    public Shape getShape(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;
    }
}

// 步骤 4:演示类使用 ShapeFactory 来获取 Shape 对象。
public class SimpleFactoryDemo {
    public static void main(String[] args) {
        ShapeFactory factory = new ShapeFactory();

        Shape shape1 = factory.getShape("CIRCLE");
        shape1.draw();

        Shape shape2 = factory.getShape("RECTANGLE");
        shape2.draw();

        Shape shape3 = factory.getShape("SQUARE");
        shape3.draw();
    }
}

2. 工厂方法模式(Factory Method)

工厂方法模式让子类决定实例化哪一个类,工厂类在其子类中实现。

// 步骤 1:创建一个接口。
interface Shape {
    void draw();
}

// 步骤 2:创建实现接口的实体类。
class Rectangle implements Shape {
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}

class Square implements Shape {
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

// 步骤 3:创建抽象工厂类。
abstract class AbstractShapeFactory {
    abstract Shape getShape();
}

// 步骤 4:创建具体的工厂类,继承抽象工厂类。
class RectangleFactory extends AbstractShapeFactory {
    Shape getShape() {
        return new Rectangle();
    }
}

class SquareFactory extends AbstractShapeFactory {
    Shape getShape() {
        return new Square();
    }
}

class CircleFactory extends AbstractShapeFactory {
    Shape getShape() {
        return new Circle();
    }
}

// 步骤 5:演示类使用不同的工厂实例来获取不同类型的对象。
public class FactoryMethodDemo {
    public static void main(String[] args) {
        AbstractShapeFactory shapeFactory1 = new CircleFactory();
        Shape shape1 = shapeFactory1.getShape();
        shape1.draw();

        AbstractShapeFactory shapeFactory2 = new RectangleFactory();
        Shape shape2 = shapeFactory2.getShape();
        shape2.draw();

        AbstractShapeFactory shapeFactory3 = new SquareFactory();
        Shape shape3 = shapeFactory3.getShape();
        shape3.draw();
    }
}

3. 抽象工厂模式(Abstract Factory)

抽象工厂模式用于创建一系列相关或依赖对象的接口,而不指定具体类。

// 步骤 1:创建一个接口。
interface Shape {
    void draw();
}

// 步骤 2:创建实现接口的实体类。
class RoundedRectangle implements Shape {
    public void draw() {
        System.out.println("Inside RoundedRectangle::draw() method.");
    }
}

class RoundedSquare implements Shape {
    public void draw() {
        System.out.println("

Inside RoundedSquare::draw() method.");
    }
}

class Rectangle implements Shape {
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}

class Square implements Shape {
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

// 步骤 3:为 Shape 和 Color 对象创建抽象类来获取工厂。
abstract class AbstractFactory {
    abstract Shape getShape(String shapeType);
}

// 步骤 4:创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
class ShapeFactory extends AbstractFactory {
    Shape getShape(String shapeType) {
        if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        }
        return null;
    }
}

class RoundedShapeFactory extends AbstractFactory {
    Shape getShape(String shapeType) {
        if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new RoundedRectangle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new RoundedSquare();
        }
        return null;
    }
}

// 步骤 5:创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。
class FactoryProducer {
    static AbstractFactory getFactory(boolean rounded) {
        if (rounded) {
            return new RoundedShapeFactory();
        } else {
            return new ShapeFactory();
        }
    }
}

// 步骤 6:使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。
public class AbstractFactoryDemo {
    public static void main(String[] args) {
        AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
        Shape shape1 = shapeFactory.getShape("RECTANGLE");
        shape1.draw();

        Shape shape2 = shapeFactory.getShape("SQUARE");
        shape2.draw();

        AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
        Shape shape3 = shapeFactory1.getShape("RECTANGLE");
        shape3.draw();

        Shape shape4 = shapeFactory1.getShape("SQUARE");
        shape4.draw();
    }
}

在抽象工厂模式的示例中,我们创建了两种工厂:一个用于生成常规形状,另一个用于生成圆角形状。这展示了如何使用抽象工厂模式来处理多个产品系列。