【简单工厂和抽象工厂的区别以及Java代码实例】

简单工厂和抽象工厂是两种常见的设计模式,它们都属于工厂模式的变体。它们的区别在于,简单工厂只有一个工厂类,负责创建所有产品,而抽象工厂则有多个工厂类,每个工厂类负责创建一组相关的产品。

下面是两种设计模式的Java代码实例:

简单工厂

package com.lfsun.main.demoleetcode.other;

// 抽象产品类
interface Shape {
          
   
    void draw();
}

// 具体产品类
class Rectangle implements Shape {
          
   
    @Override
    public void draw() {
          
   
        System.out.println("Inside Rectangle::draw() method.");
    }
}

// 具体产品类
class Square implements Shape {
          
   
    @Override
    public void draw() {
          
   
        System.out.println("Inside Square::draw() method.");
    }
}

// 简单工厂类
class SimpleFactory {
          
   
    // 静态工厂方法 简单工厂只有一个工厂类,具体创建哪个需要在工厂类里面创建
    public static Shape getShape(String shapeType) {
          
   
        if (shapeType == null) {
          
   
            return null;
        }
        if (shapeType.equalsIgnoreCase("RECTANGLE")) {
          
   
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
          
   
            return new Square();
        }
        return null;
    }
}

// 客户端代码
public class SimpleFactoryMain {
          
   
    public static void main(String[] args) {
          
   
        Shape shape1 = SimpleFactory.getShape("RECTANGLE");
        shape1.draw();
        Shape shape2 = SimpleFactory.getShape("SQUARE");
        shape2.draw();
    }
}

抽象工厂

package com.lfsun.main.demoleetcode.other;

// 抽象产品类
interface Shape1 {
          
   
    void draw();
}

// 具体产品类
class Rectangle1 implements Shape {
          
   
    @Override
    public void draw() {
          
   
        System.out.println("Inside Rectangle::draw() method.");
    }
}

// 具体产品类
class Square1 implements Shape {
          
   
    @Override
    public void draw() {
          
   
        System.out.println("Inside Square::draw() method.");
    }
}

// 抽象工厂类
interface AbstractFactory {
          
   
    /**
     * 抽象工厂有多个工厂类。抽象工厂还可以进一步扩展,比如可以增加一个新的工厂类来创建新的产品。
     * @return
     */
    Shape getShape();
}

// 具体工厂类
class RectangleFactory implements AbstractFactory {
          
   
    @Override
    public Shape getShape() {
          
   
        return new Rectangle();
    }
}

// 具体工厂类
class SquareFactory implements AbstractFactory {
          
   
    @Override
    public Shape getShape() {
          
   
        return new Square();
    }
}

// 客户端代码
public class AbstractFactoryMain {
          
   
    public static void main(String[] args) {
          
   
        AbstractFactory abstractFactory1 = new RectangleFactory();
        Shape shape1 = abstractFactory1.getShape();
        shape1.draw();
        AbstractFactory abstractFactory2 = new SquareFactory();
        Shape shape2 = abstractFactory2.getShape();
        shape2.draw();
    }
}

简单工厂和抽象工厂都是创建型设计模式,用于创建对象。它们的主要区别在于:

简单工厂只有一个工厂类,通过该类的静态方法根据传入的参数创建不同的对象。而抽象工厂则有多个工厂类,每个工厂类负责创建一组相关的对象。
简单工厂的产品对象通常属于同一类别,而抽象工厂则可以创建不同类别的产品对象。
简单工厂的工厂类通常是静态的,而抽象工厂的工厂类是动态创建的。

总之,简单工厂适用于创建一组类似的对象,而抽象工厂则适用于创建一组不同类型的对象。

经验分享 程序员 微信小程序 职场和发展