public class Client {
public static void main(String[] args) {
Factory factoryA = new ConcreteFactoryA();
Product productA = factoryA.createProduct();
productA.operation();
Factory factoryB = new ConcreteFactoryB();
Product productB = factoryB.createProduct();
productB.operation();
}
}
在上面的示例中,Product 是抽象产品接口,定义了产品的操作方法。ConcreteProductA 和 ConcreteProductB 是具体产品类,实现了产品接口,并定义了各自的操作方法。Factory 是抽象工厂接口,定义了创建产品对象的抽象方法。ConcreteFactoryA 和 ConcreteFactoryB 是具体工厂类,实现了抽象工厂接口,负责创建具体产品的实例。
客户端通过调用具体工厂类的方法来创建具体产品的实例,并调用其操作方法。通过使用工厂方法模式,客户端可以通过工厂类来创建产品对象,而无需直接依赖具体产品类。这样可以实现代码的解耦和灵活性,使得系统更易于扩展和维护。
Abstract Factory Pattern
一个常见的实际应用抽象工厂模式的例子是图形用户界面(GUI)库。假设我们正在开发一个跨平台的GUI库,需要支持不同操作系统(如Windows、macOS和Linux)下的窗口和按钮。
在这种情况下,可以使用抽象工厂模式来创建一组相关的产品对象,包括窗口和按钮。首先,定义抽象产品接口:
// 抽象窗口产品
interface Window {
void render();
}
// 抽象按钮产品
interface Button {
void click();
}
然后,实现具体的窗口和按钮产品类,每个操作系统对应一个具体产品类:
// Windows窗口产品
class WindowsWindow implements Window {
public void render() {
System.out.println("Render Windows window");
}
}
// Windows按钮产品
class WindowsButton implements Button {
public void click() {
System.out.println("Click Windows button");
}
}
//----------------------------------------------------------
// macOS窗口产品
class MacOSWindow implements Window {
public void render() {
System.out.println("Render macOS window");
}
}
// macOS按钮产品
class MacOSButton implements Button {
public void click() {
System.out.println("Click macOS button");
}
}
//----------------------------------------------------------
// Linux窗口产品
class LinuxWindow implements Window {
public void render() {
System.out.println("Render Linux window");
}
}
// Linux按钮产品
class LinuxButton implements Button {
public void click() {
System.out.println("Click Linux button");
}
}
接下来,定义抽象工厂接口来创建窗口和按钮:
// 抽象GUI工厂
interface GUIFactory {
Window createWindow();
Button createButton();
}
然后,实现具体的GUI工厂类,每个操作系统对应一个具体工厂类:
// Windows GUI工厂
class WindowsGUIFactory implements GUIFactory {
public Window createWindow() {
return new WindowsWindow();
}
public Button createButton() {
return new WindowsButton();
}
}
// macOS GUI工厂
class MacOSGUIFactory implements GUIFactory {
public Window createWindow() {
return new MacOSWindow();
}
public Button createButton() {
return new MacOSButton();
}
}
// Linux GUI工厂
class LinuxGUIFactory implements GUIFactory {
public Window createWindow() {
return new LinuxWindow();
}
public Button createButton() {
return new LinuxButton();
}
}
现在,客户端可以通过抽象工厂来创建特定操作系统下的窗口和按钮,并使用它们:
public class Client {
public static void main(String[] args) {
// 创建Windows风格的GUI
GUIFactory windowsFactory = new WindowsGUIFactory();
Window windowsWindow = windowsFactory.createWindow();
Button windowsButton = windowsFactory.createButton();
windowsWindow.render();
windowsButton.click();
// 创建macOS风格的GUI
GUIFactory macosFactory = new MacOSGUIFactory();
Window macosWindow = macosFactory.createWindow();
Button macosButton = macosFactory.createButton();
macosWindow.render();
macosButton.click();
// 创建Linux风格的GUI
GUIFactory linuxFactory = new LinuxGUIFactory();
Window linuxWindow = linuxFactory.createWindow();
Button linuxButton = linuxFactory.createB