Haricharan Reddy
Haricharan Reddy

Reputation: 105

Why to use factory pattern

I am trying to understand the Factory Method design pattern and i have come across multiple questions similar to the one on my mind but my question still remains unanswered.

this is the article I am referring in my question

In the example mentioned in above article why do we need a concrete factory of HtmlDialog and WindowsDialog and at the main method choose one of the implementations of Dialog, instead of directly choosing the implementation of Button from the available HtmlButton and WindowsButton

Upvotes: 0

Views: 201

Answers (4)

R.Abbasi
R.Abbasi

Reputation: 711

In addition to @Schwern notes, the design of the example is not quite right I think. If you choose the HTML rendering strategy, all elements will be generated and rendered with this strategy. So the decision would be made once and all other elements will know how to create their needed elements from then on (with respect to rendering strategy).

The important thing that is not mentioned is that the elements wouldn't want to know how to create other elements with respect to their structure (not rendering strategy). In other words, all HTML elements would use HTML-rendering factory strategy. This leads to using an abstract factory and choosing the right factory for a rendering strategy.

Upvotes: 0

Guru Stron
Guru Stron

Reputation: 143443

why ... instead of directly choosing the implementation of Button from the available HtmlButton and WindowsButton

Patterns are not just used to solve the common problems in software design and one of the main goals of software design is to produce maintainable and extensible applications. And since the examples showing the patterns are usually quite minimalistic quite often they fail to show the maintainability and/or extensibility of the pattern.

In this case you can think about the Dialog as some piece of code which represents some business scenario in your app which can be invoked in several parts of it, in this case encapsulating the creation logic makes quite a lot of sense since you will not need to repeat the same logic everywhere (so DRY is respected). And in case of addition of some specific new platform (for example you will have special window for macOS) you will need to change only the factory.

Note that

P.S.

  • I would argue that Dialog in this case represents not just factory pattern but something more resembling template method pattern.
  • Check out other resources for the pattern explanation, like Wiki or The Baeldung

Upvotes: 0

Schwern
Schwern

Reputation: 165576

I think you're asking, why not do this:

public class Demo {
    private static Button button;

    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")) {
            button = new WindowsButton();
        } else {
            button = new HtmlButton();
        }
    }

    static void runBusinessLogic() {
        button.render();
    }
}

The example is a little too simplistic to illustrate the benefit of the Dialog factory, so you're right to ask that.

The idea is that you're not asking to render a button, you're asking to render a dialog which does something well defined. For example, you want a dialog which will prompt the user for a confirmation.

By using a Dialog, Demo makes no assumptions about what the dialog will render, so Dialog subclasses are free to change their implementations. One implementation could create a button. One could create two buttons. One could use speech recognition to prompt the user. These details are all the "product".

The Dialog factory hides all this from the calling code leaving the Dialog implementations free to decide how to render itself. The implementation can change from version to version and Demo still works.

Again, this would be more apparent if Dialog was more than a thin wrapper around creating a button.

Upvotes: 1

Why do we need a concrete factory of HtmlDialog and WindowsDialog?

Since HtmlDialog and WindowsDialog classes must override the createButton() method to create specific button objects that are special to themselves. You have to create an AWT/Swing Button for the WindowsDialog and an HTML <Button> for the HtmlDialog class.

For the second question, in the main method you typically select the concrete factory depending on a config value. And you have to start the selection from the top since the Dialog classes are parents/creators of the buttons.

Upvotes: 0

Related Questions