Factory Design Pattern in Java

Factory pattern comes into creational design pattern category, the main objective of the creational pattern is to instantiate an object and in Factory Pattern an interface is responsible for creating the object but the sub classes decides which class to instantiate. It is like the interface instantiate the appropriate sub-class depending upon the data passed. Here in this article we will understand how we can create an Factory Pattern in Java.

Now in the example that we are looking at for explaining the Factory Design Pattern is we are creating an object of Dog and Cat without calling the Dog or Cat class.

Singleton Design Pattern in Java

Mammal Class Code:

Now if you see this class, here we will be passing an argument to the getMammalObject function based on the argument passed we return the Mammals object of the class.

Reading Excel documents in Java

Abstract Class Code:

Here we have created an abstract class Mammals. This class will be used by the Dog and Cat class.

Cat Class Code:

Here we have created Cat class, this class extends from Mammals class so it is understandable that Cat needs to have the implementation for doWalking() method.

Invoke Class Methods using Reflection in Java

Dog Class Code:

Here we have created Dog class, this class extends from Mammals class so it is understandable that Dog needs to have the implementation for doWalking() method.

Sending Emails from Java Applications

Factory Method Client Code:

Here if you see i want to create an object for Dog class and for that we are not directly loading the Dog class. Instead we are instantiated the object for MammalsFactory class.

Factory Design Pattern in Java

Factory Design Pattern in Java

Tagged . Bookmark the permalink.

12 Responses to Factory Design Pattern in Java

  1. Spy Shopping says:

    This factory designing in java is the interesting part

  2. chakresh says:

    Thanks , for this article, it was good to learn about Factory pattern , UML diagram also helps to me in understanding.

  3. Lars says:

    Your code looks like the factory pattern but it isn’t the real one. Two points: 1. the factory should be an interface and 2. the client shouldn’t create the factory and use it afterwards – you can not switch the factory, it’s hardcoded.

    The factory pattern is related to dependency injection and loose coupling. It’s all about how to make classes configurable. Here’s my example (I hope it will keep the identations):

    public interface MammalFactory { Mammal createMammal(); }

    public class MammalClient {

    private final MammalFactory factory;

    public MammalClient(MammalFactory factory) {
    this.factory = factory;
    }
    public void createAndLetWalk() {
    factory.createMammal().doWalking();
    }
    }

    public class Main {
    private MammalFactory forDogs = new MammalFactory() {
    public Mammal createMammal() { return new Dog(); }
    };
    private MammalFactory forCats = new MammalFactory() {
    public Mammal createMammal() { return new Cat(); }
    };
    public static void main(String[] args) {
    new MammalClient(forDogs).createAndLetWalk();
    new MammalClient(forCats).createAndLetWalk();
    }
    }

    • Hi Lars,
      Thanks for your input, when i started learning Design Patterns i didn’t got enough examples, but i think the example you have mentioned is somewhat similar to Abstract Factory Pattern. Isn’t It?

      Thanks
      Hitesh Agarwal

  4. Veera says:

    This is really useful.

  5. Ravi says:

    It is good example to understand the Factory pattern

  6. Lakshmi says:

    I would say Client should not create an instance of Factory class. This factory class should be abstract class not concrete class.
    and the method to create an object should be static method.

  7. Manoj says:

    It is a good example of factory pattern to understand.

  8. Ramesh says:

    Nice article..simple to understand..

  9. Vinay says:

    Guys, just in case you have any confusion, I have also explained factory design pattern on my blog. Please visit the below link to have a look:-

    http://vinaymudgil007.blogspot.com/2010/09/factory-design-pattern-with-sample.html

    Note:- I havn’t pasted the code snippet here itself as I think it might loose the indentation.

  10. Mohammed Jani says:

    simple and nice concept ….. it helps alot…

  11. Debashish says:

    @ Lars Don’t confuse the readers, the examples above are correct for Factory pattern. Your example pertains to the of Abstract Factory design pattern.

Leave a Reply