.:: CODE SNIPPET ::.

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

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();
    }
    


    Here, we have a method accept() to identify which class is concerned, and a process() method to do something in that class. Then, We create a class handles the creation of the derived class named AbstractPersonHandler:

    abstract class AbstractPersonHandler implements IPersonHandler
    {
        protected final Set<Integer> personIds = new HashSet<Integer>();
    
        @Override
        public boolean accept(int personId)
        {
            return personIds.contains(personId);
        }
    
        @Override
        public void process()
        {
    
        }
    }
    

    the personIds collection contains ids that is accepted in this handler.
    Next, we have the concrete handler of the Student and Doctor:

    public class DoctorHandler extends AbstractPersonHandler
    {
        public DoctorHandler()
        {
            super();
            this.personIds.add(IHuman.DOCTOR);
        }
    
        @Override
        public void process()
        {
            System.out.println("This is Doctor handler");
            super.process();
        }
    }
    

    The Student handler:

    public class StudentHandler extends AbstractPersonHandler
    {
        public StudentHandler()
        {
            this.personIds.add(IHuman.STUDENT);
        }
    
        @Override
        public void process()
        {
            System.out.println("This is Student handler");
            super.process();
        }
    }
    

    Now, the main class concerned in this topic is the PersonFactory.

    public class PersonFactory
    {
        public static final PersonFactory INSTANCE = new PersonFactory();
    
        public IPersonHandler createStudentHandler()
        {
            return new StudentHandler();
        }
    
        public IPersonHandler createDoctorHandler()
        {
            return new DoctorHandler();
        }
    }
    

    In a simple way, it just contains the method to create the concrete handler, after create the factory, you don’t need to take care of creation of the handler. This is cleared in the below test snippet:

    public class TestFactoryMedthod
    {
        public static void main(String[] args)
        {
            final List<IPersonHandler> listHandlers = new ArrayList<IPersonHandler>();
            listHandlers.add(PersonFactory.INSTANCE.createStudentHandler());
            listHandlers.add(PersonFactory.INSTANCE.createDoctorHandler());
    
            for (IPersonHandler handler : listHandlers)
            {
                if (handler.accept(IHuman.STUDENT))
                {
                    handler.process();
                }
            }
        }
    }
    
    Advertisements

    One response to “Simple factory design pattern example

    1. Pingback: Using factory pattern base on class name | .:: Code Snippet ::.

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: