Search This Blog

Wednesday 22 February 2017

Builder Design Pattern in Java

Builder Design Pattern in Java
Intent
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Motivation
As an application grows complex, the complexity of the classes and objects used also increases. Complex objects are constructed by using other objects and needs special handling when being constructed.
An application might need a mechanism for building complex objects that is independent from the ones that make up the object. In this scenario, one might want to try using the Builder design pattern.
Builder pattern allows a client object to construct a complex object by specifying only its type and content, being shielded from the details related to the objects representation. This way the construction process can be used to create different representations. The logic of this process is isolated form the actual steps used in creating the complex object, so the process can be used again to create a different object form the same set of simple objects as the first one.

Applicability
Use the Builder pattern when
·         The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
·         The construction process must allow different representations for the object that's constructed.

Consequences
·         Builder pattern lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product. It also hides how the product gets assembled.
·         Builder pattern isolates code for construction and representation. The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented.
·         Builder pattern gives you finer control over the construction process.

Implementation
We will implement Builder Pattern by using following classes.
1.      Our first class would be Meal class, which represents food items in a meal. It represents a drink, main course, and side.
public class Meal {

      private String drink;
      private String mainCourse;
      private String side;

      public String getDrink() {
            return drink;
      }

      public void setDrink(String drink) {
            this.drink = drink;
      }

      public String getMainCourse() {
            return mainCourse;
      }

      public void setMainCourse(String mainCourse) {
            this.mainCourse = mainCourse;
      }

      public String getSide() {
            return side;
      }

      public void setSide(String side) {
            this.side = side;
      }

      public String toString() {
            return "Drink : " + drink + ", Main Course : " + mainCourse + ", Side Dish : " + side;
      }

}

2.      Here we create a Builder interface, MealBuilder. It methods will be used to build a meal and to retrieve the meal.
public interface MealBuilder {
   public void buildDrink();

   public void buildMainCourse();

   public void buildSide();

   public Meal getMeal();
}

3.      Our first implementation of MealBuilder is ItalianMealBuilder. Its constructor creates a meal. Its methods are implemented to build the various parts of the meal. It returns the meal using getMeal() method.
public class ItalianMealBuilder implements MealBuilder {

   private Meal meal;

   public ItalianMealBuilder() {
         meal = new Meal();
   }

   @Override
   public void buildDrink() {
         meal.setDrink("Martini");
   }

   @Override
   public void buildMainCourse() {
         meal.setMainCourse("Simple Spinach Lasagna");
   }

   @Override
   public void buildSide() {
         meal.setSide("Spring Green Risotto");
   }

   @Override
   public Meal getMeal() {
         return meal;
   }
}

4.      Similarly second implementation of MealBuilder is JapaneseMealBuilder. Its constructor creates a meal. Its methods are implemented to build the various parts of the meal. It returns the meal using getMeal() method.
public class JapaneseMealBuilder implements MealBuilder{
   private Meal meal;

   public JapaneseMealBuilder() {
         meal = new Meal();
   }

   @Override
   public void buildDrink() {
         meal.setDrink("Chuhai");
   }

   @Override
   public void buildMainCourse() {
         meal.setMainCourse("Tofu and Sweet Potato");
   }

   @Override
   public void buildSide() {
         meal.setSide("Onigiri");
   }

   @Override
   public Meal getMeal() {
         return meal;
   }
}





5.      The MealDirector class takes a MealBuilder as a parameter in its constructor. A different type of meal will be assembled by the MealDirector depending on the Concrete Builder passed in to the constructor.
public class MealDirector {
   private MealBuilder mealBuilder = null;

   public MealDirector(MealBuilder mealBuilder) {
         this.mealBuilder = mealBuilder;
   }

   public void constructMeal() {
         mealBuilder.buildDrink();
         mealBuilder.buildMainCourse();
         mealBuilder.buildSide();
   }

   public Meal getMeal() {
         return mealBuilder.getMeal();
   }
}

6.      The Main class is used as entry point to the application and demonstrate our builder pattern code.
public class Main {
   public static void main(String[] args) {

         MealBuilder mealBuilder = new ItalianMealBuilder();
         MealDirector mealDirector = new MealDirector(mealBuilder);
         mealDirector.constructMeal();
         Meal meal = mealDirector.getMeal();
         System.out.println("Italian Meal : " + meal);
        
         mealBuilder = new JapaneseMealBuilder();
         mealDirector = new MealDirector(mealBuilder);
         mealDirector.constructMeal();
         meal = mealDirector.getMeal();
         System.out.println("Japanese Meal : " + meal);
   }
}


When we execute the Main class we get the following output

Builder Design Pattern in Java
Builder Design Pattern in Java

Monday 20 February 2017

JSP Lifecycle

JSP Lifecycle

The lifecycle of a JSP is the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step, which translates the JSP into a Servlet.
Following are the JSP Lifecycle phases.
1.     Translation of JSP to Servlet code.
2.     Compilation of Servlet to bytecode.
3.     Loading Servlet class.
4.     Creating servlet instance.
5.     Initialization by calling jspInit() method
6.     Request Processing by calling _jspService() method

7.     Destroying by calling jspDestroy() method
JSP Lifecycle
JSP Lifecycle

JSP Translation and Compilation
When a user request for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine does the following
1.   Parse the JSP
2.   Transforms the JSP to a Servlet
3.   Compile the Servlet (.java) file to a class file.


JSP Initialization
Before servicing any request the web container loads the JSP and invokes jspInit() method. Initialization is performed only once, just as with servlets init() method. If you need to perform any JSP-specific initialization, you need yo override the jspInit() method.

JSP Service
Once the JSP page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.
The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters.
The _jspService() method of a JSP is invoked once per request and is responsible for generating the response for that request.

JSP Destruction
The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.
The jspDestroy() method is the JSP similar to the destroy method for servlets. We need to Override jspDestroy() method when we need to perform any cleanup operation, such as releasing database connections or closing files which were opened.