Factory pattern – Design Patterns in Java/J2EE

Creational pattern deals with the creation of objects and it hides the complex logic involved in the object creation object from the clients. One of the common creational patterns is the Factory pattern and it is used extensively in Applications. Let us understand what factors should be considered in mind while designing a Factory pattern or a Factory method pattern.

also read:

Depending on the client input, the Factory pattern may create and return an object which is one of the several sub-classes in consideration. But the reverse is not always true. Any classes that creates and returns objects should not be considered as Factory classes.
Let us take an example of a Factory class which is used to create Button objects. Assume that we have two variations of Button objects, one is the WindowsButton in an typical Windows Operating system and the other is the LinuxButton in Linux operating System.
Consider the following Button class, which has a single draw() method. Since this class is a generic class, we have made it as abstract.
Button.java

package tips.pattern.factory;
public abstract class Button {
    public abstract void draw();
}

Given below are the concrete implementations of the Button class, WindowsButton and LinuxButton, each providing a much simplified implementation for the draw() method.
WindowsButton.java

package tips.pattern.factory;
public class WindowsButton extends Button{
    @Override
    public void draw() {
        System.out.println("Drawing Windows Button");
    }
}

LinuxButton.java

package tips.pattern.factory;
public class LinuxButton extends Button{
    @Override
    public void draw() {
        System.out.println("Drawing Linux Button");
    }
}

Now let us come to the core implementation, the Factory class itself. The ButtonFactory class has one static method called createButton() which the clients can invoke to get the Button object. Note the return type of the method, it is neither WindowsButton nor LinuxButton, but the super type of the both, i.e, Button. Whether the return type of method is WindowsButton or LinuxButton is decided based on the input operating system.
ButtonFactory.java

package tips.pattern.factory;
public class ButtonFactory {
    public static Button createButton(String os){
        if (os.equals("Windows")){
            return new WindowsButton();
        }else if (os.equals("Linux")){
            return new LinuxButton();
        }
        return null;
    }
}

Given below is the client Application that makes use of the above ButtonFactory class. The client is un-aware of the fact there is multiple implementations of the Button class. It accesses the draw() operation through a single unified type Button.
FactoryClient.java

package tips.pattern.factory;
public class FactoryClient {
    public static void main(String[] args) {
        Button windowsButton =
            ButtonFactory.createButton("Windows");
        windowsButton.draw();
        Button linuxButton =
            ButtonFactory.createButton("Linux");
        linuxButton.draw();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!