# Java如何实现抽象工厂模式 ## 1. 模式概述 ### 1.1 定义 抽象工厂模式(Abstract Factory Pattern)是一种创建型设计模式,它提供了一种封装一组具有共同主题的独立工厂的方式,而无需指定它们的具体类。该模式允许客户端使用抽象的接口来创建一组相关或依赖对象,而无需关心实际创建的具体对象是什么。 ### 1.2 核心角色 - **抽象工厂(AbstractFactory)**:声明创建抽象产品对象的接口 - **具体工厂(ConcreteFactory)**:实现抽象工厂的接口,负责创建具体产品对象 - **抽象产品(AbstractProduct)**:声明产品对象的接口 - **具体产品(ConcreteProduct)**:实现抽象产品接口的具体类 - **客户端(Client)**:使用抽象工厂和抽象产品接口 ## 2. 模式结构 ```java // 抽象产品A interface ProductA { void operationA(); } // 抽象产品B interface ProductB { void operationB(); } // 具体产品A1 class ConcreteProductA1 implements ProductA { public void operationA() { System.out.println("ConcreteProductA1 operation"); } } // 具体产品B1 class ConcreteProductB1 implements ProductB { public void operationB() { System.out.println("ConcreteProductB1 operation"); } } // 抽象工厂 interface AbstractFactory { ProductA createProductA(); ProductB createProductB(); } // 具体工厂1 class ConcreteFactory1 implements AbstractFactory { public ProductA createProductA() { return new ConcreteProductA1(); } public ProductB createProductB() { return new ConcreteProductB1(); } } // 抽象按钮 interface Button { void render(); void onClick(); } // Windows按钮 class WindowsButton implements Button { public void render() { System.out.println("Render Windows style button"); } public void onClick() { System.out.println("Windows button clicked"); } } // MacOS按钮 class MacOSButton implements Button { public void render() { System.out.println("Render MacOS style button"); } public void onClick() { System.out.println("MacOS button clicked"); } } // 抽象复选框 interface Checkbox { void render(); void toggle(); } // Windows复选框 class WindowsCheckbox implements Checkbox { public void render() { System.out.println("Render Windows style checkbox"); } public void toggle() { System.out.println("Windows checkbox toggled"); } } // MacOS复选框 class MacOSCheckbox implements Checkbox { public void render() { System.out.println("Render MacOS style checkbox"); } public void toggle() { System.out.println("MacOS checkbox toggled"); } } // 抽象GUI工厂 interface GUIFactory { Button createButton(); Checkbox createCheckbox(); } // Windows工厂 class WindowsFactory implements GUIFactory { public Button createButton() { return new WindowsButton(); } public Checkbox createCheckbox() { return new WindowsCheckbox(); } } // MacOS工厂 class MacOSFactory implements GUIFactory { public Button createButton() { return new MacOSButton(); } public Checkbox createCheckbox() { return new MacOSCheckbox(); } } // 客户端代码 class Application { private Button button; private Checkbox checkbox; public Application(GUIFactory factory) { button = factory.createButton(); checkbox = factory.createCheckbox(); } public void render() { button.render(); checkbox.render(); } } // 使用示例 public class AbstractFactoryDemo { public static void main(String[] args) { // 创建Windows风格UI GUIFactory windowsFactory = new WindowsFactory(); Application windowsApp = new Application(windowsFactory); windowsApp.render(); // 创建MacOS风格UI GUIFactory macFactory = new MacOSFactory(); Application macApp = new Application(macFactory); macApp.render(); } } // 抽象连接 interface Connection { void connect(); void executeQuery(String query); } // MySQL连接 class MySQLConnection implements Connection { public void connect() { System.out.println("Connected to MySQL database"); } public void executeQuery(String query) { System.out.println("Executing MySQL query: " + query); } } // Oracle连接 class OracleConnection implements Connection { public void connect() { System.out.println("Connected to Oracle database"); } public void executeQuery(String query) { System.out.println("Executing Oracle query: " + query); } } // 抽象语句 interface Statement { void prepare(); void execute(); } // MySQL语句 class MySQLStatement implements Statement { public void prepare() { System.out.println("Preparing MySQL statement"); } public void execute() { System.out.println("Executing MySQL statement"); } } // Oracle语句 class OracleStatement implements Statement { public void prepare() { System.out.println("Preparing Oracle statement"); } public void execute() { System.out.println("Executing Oracle statement"); } } // 抽象数据库工厂 interface DatabaseFactory { Connection createConnection(); Statement createStatement(); } // MySQL工厂 class MySQLFactory implements DatabaseFactory { public Connection createConnection() { return new MySQLConnection(); } public Statement createStatement() { return new MySQLStatement(); } } // Oracle工厂 class OracleFactory implements DatabaseFactory { public Connection createConnection() { return new OracleConnection(); } public Statement createStatement() { return new OracleStatement(); } } // 客户端代码 class DatabaseClient { private Connection connection; private Statement statement; public DatabaseClient(DatabaseFactory factory) { connection = factory.createConnection(); statement = factory.createStatement(); } public void executeOperations() { connection.connect(); statement.prepare(); statement.execute(); } } // 使用示例 public class DatabaseDemo { public static void main(String[] args) { // 使用MySQL DatabaseFactory mysqlFactory = new MySQLFactory(); DatabaseClient mysqlClient = new DatabaseClient(mysqlFactory); mysqlClient.executeOperations(); // 使用Oracle DatabaseFactory oracleFactory = new OracleFactory(); DatabaseClient oracleClient = new DatabaseClient(oracleFactory); oracleClient.executeOperations(); } } 抽象工厂模式通过引入抽象层解决了客户端代码与具体类实现的强耦合问题,使得系统能够方便地切换整个产品族。在Java中实现该模式需要明确定义产品等级结构和产品族,通过接口和实现类清晰地分离抽象与具体。虽然该模式在增加新产品时不够灵活,但在需要保证产品一致性和可配置性的场景下,它仍然是不可替代的优秀设计模式。 “`
这篇文章详细介绍了Java中实现抽象工厂模式的方法,包含: 1. 模式定义和核心概念 2. 完整代码示例(GUI组件和数据库连接两个案例) 3. 模式优势与适用场景分析 4. 与其他设计模式的关系 5. 实际应用建议 6. 总结
全文约2400字,采用Markdown格式,包含清晰的代码示例和结构化说明。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。