工厂方法设计模式

2022/11/15 设计模式

# 定义

# 工厂方法是一种创建型设计模式, 解决了在不指定具体类的情况下创建产品对象的问题。

工厂方法定义了一个方法, 且必须使用该方法代替通过直接调用构造函数来创建对象 ( new操作符) 的方式。 子类可重写该方法来更改将被创建的对象所属类。

# 示例

# 生成跨平台的GUI元素


# 1. 定义通用产品接口: Button.java

public interface Button {

    void render();

    void onClick();

}
1
2
3
4
5
6
7

# 2. 定义不同平台的具体产品

  • Web平台: HtmlButton.java
public class HtmlButton implements Button {

    public void render() {
        System.out.println("<button> Test Button</button>");
    }

    public void onClick() {
        System.out.println("Click! This is a html buttom.");
    }

}
1
2
3
4
5
6
7
8
9
10
11
  • Windows平台: WindowsButton.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WindowsButton implements Button {

    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    JButton button;

    public void render() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("Hello World!");
        label.setOpaque(true);
        label.setBackground(new Color(235, 233, 126));
        label.setFont(new Font("Dialog", Font.BOLD, 44));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.getContentPane().add(panel);
        panel.add(label);
        onClick();
        panel.add(button);

        frame.setSize(320, 200);
        frame.setVisible(true);
        onClick();
    }

    public void onClick() {
        button = new JButton("Exit");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                System.exit(0);
            }
        });
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# 3. 定义基础创建者: Dialog.java

public abstract class Dialog {

    public void renderWindow(){
        Button button = createButton();
        button.render();
    }

    //子类需要重写此方法
    //不同平台创建不同的button
    public abstract Button createButton();

}

1
2
3
4
5
6
7
8
9
10
11
12
13

# 4. 定义不同平台的创建者

  • Web平台: HtmlButton.java
public class HtmlDialog extends Dialog {

    @Override
    public Button createButton() {
        return new HtmlButton();
    }

}

1
2
3
4
5
6
7
8
9
  • Windows平台: WindowsDialog
public class WindowsDialog extends Dialog {

    @Override
    public Button createButton() {
        return new WindowsButton();
    }

}

1
2
3
4
5
6
7
8
9

# 5. 客户端代码 Demo.java

public class Demo {

    public static Dialog dialog;

    public static void main(String[] args) {
        configure();
        runBusniessLogic();
    }

    /**
     * 不同的环境使用不同的工厂
     */
    public static void configure() {
        if (System.getProperty("os.name").contains("Windows")) {
            dialog = new WindowsDialog();
        } else {
            dialog = new HtmlDialog();
        }
    }

    /**
     * 业务逻辑代码不关心具体使用的工厂
     */
    public static void runBusniessLogic() {
        dialog.renderWindow();
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Last Updated: 2022/11/14 22:34:46