java设计模式:02-04-抽象工厂模式

抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式(Abstract Factory Pattern)提供一个接口,用于创建一系列相关或相互依赖的对象,而无需指定它们的具体类。它属于创建型设计模式。

抽象工厂模式的应用场景

  • 当一个系统要独立于它的产品创建、组合和表示时:系统不应依赖于产品类如何被创建、组合和表示。
  • 当一个系统要由多个产品系列中的一个来配置时:例如,图形界面库中不同的主题(Windows风格、Mac风格)需要不同的控件样式。
  • 当需要提供一个产品类库,而只需显示其接口而不需要实现时:例如,数据库驱动程序的实现。

抽象工厂模式的实现方式

1. 传统实现方式

思想:通过定义抽象工厂接口以及具体工厂类,每个具体工厂负责创建一系列相关的产品。

实现方式

// 产品接口
interface Button {
    void paint();
}

interface Checkbox {
    void paint();
}

// 具体产品类
class WindowsButton implements Button {
    public void paint() {
        System.out.println("Rendering a button in Windows style.");
    }
}

class MacOSButton implements Button {
    public void paint() {
        System.out.println("Rendering a button in MacOS style.");
    }
}

class WindowsCheckbox implements Checkbox {
    public void paint() {
        System.out.println("Rendering a checkbox in Windows style.");
    }
}

class MacOSCheckbox implements Checkbox {
    public void paint() {
        System.out.println("Rendering a checkbox in MacOS style.");
    }
}

// 抽象工厂接口
interface GUIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

// 具体工厂类
class WindowsFactory implements GUIFactory {
    public Button createButton() {
        return new WindowsButton();
    }

    public Checkbox createCheckbox() {
        return new WindowsCheckbox();
    }
}

class MacOSFactory implements GUIFactory {
    public Button createButton() {
        return new MacOSButton();
    }

    public Checkbox createCheckbox() {
        return new MacOSCheckbox();
    }
}

// 客户端代码
public class AbstractFactoryPattern {
    private static Application configureApplication() {
        Application app;
        GUIFactory factory;
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("mac")) {
            factory = new MacOSFactory();
            app = new Application(factory);
        } else {
            factory = new WindowsFactory();
            app = new Application(factory);
        }
        return app;
    }

    public static void main(String[] args) {
        Application app = configureApplication();
        app.paint();
    }
}

// 应用类
class Application {
    private Button button;
    private Checkbox checkbox;

    public Application(GUIFactory factory) {
        button = factory.createButton();
        checkbox = factory.createCheckbox();
    }

    public void paint() {
        button.paint();
        checkbox.paint();
    }
}

优点

  • 分离具体类的创建,使得客户端代码与具体实现解耦。
  • 符合开闭原则(对扩展开放,对修改关闭),可以通过增加具体工厂来扩展产品系列。

缺点

  • 增加了系统的复杂度,增加了新的抽象层。
  • 当产品系列需要频繁变化时,会导致类爆炸(类数量急剧增加)。
2. 使用反射实现抽象工厂

思想:通过反射机制,根据配置文件或其他方式动态加载具体工厂类,避免在代码中硬编码具体工厂类。

实现方式

import java.util.Properties;
import java.io.InputStream;

// 产品接口
interface Button {
    void paint();
}

interface Checkbox {
    void paint();
}

// 具体产品类
class WindowsButton implements Button {
    public void paint() {
        System.out.println("Rendering a button in Windows style.");
    }
}

class MacOSButton implements Button {
    public void paint() {
        System.out.println("Rendering a button in MacOS style.");
    }
}

class WindowsCheckbox implements Checkbox {
    public void paint() {
        System.out.println("Rendering a checkbox in Windows style.");
    }
}

class MacOSCheckbox implements Checkbox {
    public void paint() {
        System.out.println("Rendering a checkbox in MacOS style.");
    }
}

// 抽象工厂接口
interface GUIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

// 具体工厂类
class WindowsFactory implements GUIFactory {
    public Button createButton() {
        return new WindowsButton();
    }

    public Checkbox createCheckbox() {
        return new WindowsCheckbox();
    }
}

class MacOSFactory implements GUIFactory {
    public Button createButton() {
        return new MacOSButton();
    }

    public Checkbox createCheckbox() {
        return new MacOSCheckbox();
    }
}

// 工厂生成器类
class FactoryGenerator {
    public static GUIFactory getFactory() {
        GUIFactory factory = null;
        try {
            Properties prop = new Properties();
            InputStream input = FactoryGenerator.class.getClassLoader().getResourceAsStream("config.properties");
            prop.load(input);
            String factoryClass = prop.getProperty("factory.class");
            factory = (GUIFactory) Class.forName(factoryClass).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return factory;
    }
}

// 客户端代码
public class AbstractFactoryPatternReflection {
    private static Application configureApplication() {
        GUIFactory factory = FactoryGenerator.getFactory();
        return new Application(factory);
    }

    public static void main(String[] args) {
        Application app = configureApplication();
        app.paint();
    }
}

// 应用类
class Application {
    private Button button;
    private Checkbox checkbox;

    public Application(GUIFactory factory) {
        button = factory.createButton();
        checkbox = factory.createCheckbox();
    }

    public void paint() {
        button.paint();
        checkbox.paint();
    }
}

优点

  • 动态加载具体工厂类,提升了灵活性。
  • 减少了硬编码依赖,提升了代码的可维护性。

缺点

  • 依赖配置文件和反射机制,增加了配置和调试的复杂度。
  • 反射机制在一定程度上影响性能。

总结

实现方式优点缺点
传统实现方式分离具体类的创建,符合开闭原则,扩展性好增加了系统的复杂度,增加了新的抽象层,类数量增加
使用反射实现动态加载具体工厂类,减少硬编码依赖,提升灵活性依赖配置文件和反射机制,增加了配置和调试的复杂度,影响性能

选择哪种实现方式应根据具体的需求和系统的复杂度来决定。如果系统对产品系列的扩展性要求较高且变化频繁,传统实现方式更适合。如果系统需要动态加载具体工厂类以提高灵活性,可以考虑使用反射机制实现。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值