创建型模式之工厂模式

x33g5p2x  于2022-02-12 转载在 其他  
字(2.6k)|赞(0)|评价(0)|浏览(152)

1.简介

工厂方法模式原理是在父类提供一个创建对象的方法,允许子类决定实例化对象的类型。

2.工厂方法结构

1)产品(Product)将会对接口进行声明。对于所有由创建者及其子类构建的对象,这些接口都是通用的。
2)具体产品(Concrete Products)是具体实现接口的产品。
3)创建者(Creator)类声明返回产品对象的工厂方法。该方法的返回对象类型必须与产品接口相匹配。
4)具体创建者(Concrete Creators)将会重写基础工厂方法,使其返回不同类型的产品。

注意,并不一定每次调用工厂方法都会创建新的实例。工厂方法也可以返回缓存,对象池或其他的已有对象。

3.案例

1).Buttons通用的按钮

buttons/Button.java:通用的产品接口

public interface Button{
    void render();
    void onClick();
}

HtmlButton.java:具体产品

public class HtmlButton implements Button{
    public void render(){
        System.out.println("<button>Test Button</button");
        onClick();
    }
}
public void onClick(){
    System.out.println("Click !Button says-'Hellow world'");
}

windowsbutton.java:另一个具体产品

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);
            }
        });
    }
}
2)Factory:工厂

HtmlDialog.java:具体创建者

public class HtmlDialog extends Dialog {

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

WindowDialog.java:另一个具体创建者

public class WindowsDialog extends Dialog {

    @Override
    public Button createButton() {
        return new WindowsButton();
    }
}
3).客户端代码
public class Demo {
    private static Dialog dialog;

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

    /**
     * The concrete factory is usually chosen depending on configuration or
     * environment options.
     */
    static void configure() {
        if (System.getProperty("os.name").equals("Windows 10")) {
            dialog = new WindowsDialog();
        } else {
            dialog = new HtmlDialog();
        }
    }

    /**
     * All of the client code should work with factories and products through
     * abstract interfaces. This way it does not care which factory it works
     * with and what kind of product it returns.
     */
    static void runBusinessLogic() {
        dialog.renderWindow();
    }
}

4.总结

  • 优点:简单工厂模式,使用工厂对象创建具体的产品对象,从而使得对象的使用和创建过程进行的分离。

客户端不需要关注对象是谁创建的,只要通过工厂中静态方法就可以直接获取其需要的对象。

  • 缺点:工厂类中需要选择创建具体某个对象,所以一旦添加新的产品则必须要对工厂中的选择逻辑进行修改,违背了开闭原则!

,使用工厂对象创建具体的产品对象,从而使得对象的使用和创建过程进行的分离。

客户端不需要关注对象是谁创建的,只要通过工厂中静态方法就可以直接获取其需要的对象。

  • 缺点:工厂类中需要选择创建具体某个对象,所以一旦添加新的产品则必须要对工厂中的选择逻辑进行修改,违背了开闭原则!
  • 适应场合:产品类相对较少的情况,使用简单工厂创建产品对象,这样即实现了生产者与消费者的分离,也不会在工厂类中出现太复杂的判断逻辑!

相关文章