.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Tag Archives: factory

Using factory pattern base on class name


Factory pattern helps you to centralize your object construction. Hence, you have an easy way to create object while developing. Otherwise, it also create a good infrastructure design for maintenance. I mentioned the simple way to create a factory design pattern before, so this is another way to use factory pattern. It maybe more automatic on creating multiple objects.

The mechanism of this method is the at one url location, the factory will recursively search for all class file with any predefined name at all the class paths of the sub-packages. Then it will create a new instance of each found java class file. So, prerequisites for this factory way is the way you name your classes which are required to be constructed.
Read more of this post

Simple factory design pattern example


Factory design pattern comes in three flavors:

  • Simple factory
  • Factory method
  • Abstract factory
  • The factory encapsulates the object instantiation into a class which has a static method return the concrete object the you want. That means with factory design pattern, we can omit the creation object, we can just use an identifier of that object. Everything will be clear in the simple example below. In this example, we have the classes named: StudentHandler, DoctorHandler whose super class is AbstractPersonHandler.
    First we create an interface for the above classes:

    public interface IPersonHandler
    {
        boolean accept(final int personId);
    
        void process();
    }
    

    Read more of this post

    %d bloggers like this: