Factory Pattern implementation.

Factory Patterns is a Creational Design Pattern. The Simplest way by which we can define this pattern is:

 Consider if we have a superclass and n sub-classes, and based on the input provided, we have to return the object of one of the sub-classes, we use a factory pattern.

Factory pattern is one of most used design pattern in Java. This pattern provides one of the best ways to create an object. Factory Design pattern is based on Encapsulation of object oriented concept. Factory method is used to create different object from factory often refereed as Item and it encapsulate the creation code. So instead of having object creation code on client side we encapsulate inside Factory method in Java.

Intent

  • Creates objects without exposing the creation logic to the client.
  • Returns a new Object derived from an abstract class/ Interface.

Practical Problem Example

For example we are developing an Application for Training Institute. And there is an option to create a Course from a list of available courses like Java/J2EE, .Net, C, C++ etc. All the concrete courses like Java/J2EE and others are derived from an Abstract Course class (or interface). Let us assume a command is selected from the menu to create a new Course. The framework receives the course type/ name as a string parameter; it asks the factory to create a new course sending the parameter received from menu. The factory creates a new course (like Java) and returns it to the framework, casted to an Abstract Course. Then the framework uses the object as casted to the abstract class without being aware of the concrete object type.

Implementation

  • The client needs a Course of a specific type, but instead of creating it directly using the new operator, it asks the factory to create the course by providing the necessary information.
  • The factory instantiates a new concrete course and then returns to the client the newly created course (casted to abstract Course class).
  • The client uses the course as abstract Course without being aware about their concrete implementation.

Following is our abstract Course class

package com.design.factory;
public abstract class Course
{
    protected String courseId;
    protected String name;
    protected Instructor instructor;
    // continued

    public String getCourseId() {
        return courseId;
    }

    public void setCourseId(String courseId) {
        this.courseId = courseId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Instructor getInstructor() {
        return instructor;
    }
    public void setInstructor(Instructor instructor) {
        this.instructor = instructor;
    }

    public abstract Instructor
        getCourseInstructor(InstructorSearchCriteria ins);

    // other code

}

Now Java and C are the Concrete Implementation of Course class. Which are the Real Course objects.

package com.design.factory;
public class Java extends Course
{
  @Override
  public Instructor getCourseInstructor(InstructorSearchCriteria ins)
  {
    // TODO Auto-generated method stub
    Instructor instructor = getInstructor(ins);
    return instructor;
  }
}
package com.design.factory;
public class C extends Course
{
  @Override
  public Instructor getCourseInstructor(InstructorSearchCriteria ins)
  {
    // TODO Auto-generated method stub
    Instructor instructor = getInstructor(ins);
    return instructor;
  }
}

Now Create a Factory to generate object of concrete class based on given information.Depending upon the input provided the Factory class creates and returns a concrete Course implementation. The Factory class which is creating the concrete implementation should not be considered under Factory design pattern.

package com.design.factory;

/**
 * Depending upon the input provided the Factory class creates
 * and returns a concrete Course implementation. The Factory
 * class which is creating the concrete implementation should
 * not be considered under Factory design pattern.
 * @author NinjaAD
 *
 */
public class CourseFactory {

  public Course getCourse(String courseName)
  {
      if("java".equalsIgnoreCase(courseName))
      {
       return new Java(requiredParams,...);
      }
      else if("c".equalsIgnoreCase(courseName))
      {
       return new C(requiredParams,...);
      }
      // when no Course matches
      return null;
   }
}
CORE JAVA CREATIONAL-PATTERN