Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Sunday, 6 November 2016

Java Source World: Factory Pattern or Factory Method Pattern

Factory Pattern or Factory Method Pattern | Java Source World

                                                                             

                                                                           Java Source World      A Java quick reference blog


Factory Pattern (Known as the factory method pattern):




It is a creational design pattern.


A factory is a java class that is used to encapsulate object creation code.

A factory class instantiates and returns a particular type of Object based on data passed to the factory.

The different types of Objects that are returned from a factory typically are subclasses of a common parent class.

The data passed from the calling code to the factory can be passed either when the factory is created or when the method on the factory is called to create an object. This created method is often called something such as getInstance or getClass.

Example:  

Lets create an product factory class that will return a product object based on the source type of data input.

To start,here is an  Product interface.The factory will return an instantiated subclass of product. 

Product has an abstract method, createProduct().



Product.java

package com.javaconcept.designpattern.factory;

public interface Product {
      String createProduct();
}



Airline.java

package com.javaconcept.designpattern.factory;

public class Airline implements Product{

      @Override
      public String createProduct() {
            return "Airline";
      }

}


Logistics.java

package com.javaconcept.designpattern.factory;

public class Logistics implements Product{

      @Override
      public String createProduct() {
            return "Logistics";
      }

}



Above we have, Airline.java and Logistics.java are the two interfaces and represents two products.

Now let us implement our factory.

We will call our factory's object creation method createProduct(). This method takes string as a parameter. If the String is "A" it returns "Airline" object, or if the string is passed as "L" it returns "Logistics" object.



ProductFactory.java

package com.javaconcept.designpattern.factory;

public class ProductFactory {
      public static Product createProduct(String productType){
            if("A".equals(productType)){
                  return new Airline();
            }else if("L".equals(productType)){
                  return new Logistics();
            }
            return null;
      }
}

FactoryTest.java

package com.javaconcept.designpattern.factory;

public class FactoryTest {
      public static void main(String[] args) {
            Product p1 = ProductFactory.createProduct("A");
            System.out.println(p1.createProduct());     
                  Product p2 = ProductFactory.createProduct("L");
                  System.out.println(p2.createProduct());
      }
}    




From the above example we can understand that:

Factory Design pattern is based on Encapsulation object oriented concept. 

Factory method is used to create different object from factory often referred 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


When to use Factory design pattern in java:

  • Static Factory methods are common in frameworks where library code needs to create objects of types which may be sub classed by applications using the framework.

  • Some or all concrete products can be created in multiple ways, or we want to leave open the option that in the future there may be new ways to create the concrete product.

  • Factory method is used when Products don't need to know how they are created.

  • We can use factory pattern where we have to create an object of any one sub-classes depending on the data provided.

Other posts you may like to read:
OOPs concepts in java



                                                                                                                    Java Source World      A Java quick reference blog




Read More »

Monday, 11 May 2015

Java Source World: Front Controller Design Pattern

Front Controller Design Pattern | Java Source World

                                                                                                                                                    

                                                                                                                                                  Java Source World      A Java quick reference blog




 Front Controller Design Pattern:



The front controller design pattern means that all requests that come for a resource in an application will be handled by a single handler and then dispatched to the appropriate handler for that type of request. The front controller may use other helpers to achieve the dispatching mechanism.

While reading the brief overview you may have thought about ActionServlet in Struts 1 or  Dispatchers in Struts 2. Yes both of them are implementations of Front controller design pattern. But most of the developers that I have interacted to (specially the beginners) end their thought process of Front Controller at Struts only.

The Front Controller design pattern is applicable and useful for all kind of applications be it web or desktop applications and is not limited to any single programming language or framework. Before the rollout of MVC frameworks in the market, the design pattern was still in use.

The only downside was that not all application could make use of it because of the effort involved. Only the applications which had custom framework in place were using the design pattern.

So for example in case of a Java based web application, which is not using any MVC framework, all the requests would be mapped to a single servlet in web.xml file. This we know can be done by using the servlet mapping (same as we do for ActionServlet in case of Struts 1).

<web-app>
 <servlet>
 <servlet-name> FrontServlet</servlet-name>
 <servlet-class> com.example.test.FrontServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>FrontServlet<servlet-name>
 <url-pattern>/*</url-pattern>
 <servlet-mapping>
</servlet-mapping></servlet-name></servlet-name></servlet-mapping></web-app>



 Once the request reached the FrontServlet, it uses some configuration files to decide the appropriate dispatcher for the request and thus forwards the request using forward mechanism.


Other posts you may like to read:
Factory Pattern or Factory Method Pattern
OOPs concepts in java





                                                                                                                                                  Java Source World      A Java quick reference blog





Read More »