设为首页 加入收藏

TOP

Factory Pattern —— Creational Class(二)
2023-09-09 10:26:00 】 浏览:104
Tags:Factory Pattern Creational Class
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
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇面试官:synchronized 能不能禁止.. 下一篇【深度思考】如何优雅的实现脱敏?

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目