design pattern

Factory Design Pattern : A Creational Design Pattern

Factory design patterns provides an interface to create an object. Object creation is not just allocating some memory, in a complex systems the object creation will be a complex process. Setting certain defaults, following certain steps and initialisation of an object many more aspects will be taken care.

Let us take an example to understand it better, when we handle payment gateway transactions, a transaction object is not a simple object, it has much more complexities. It may be handling specific details for Master cards, Visa cards, or it may be related to income tax etc.

There are multiple ways of handling factory design patter, Factory Method pattern and Abstract Factory class are two different approaches.

Factory Method Pattern

Without a design pattern the code looks as fallow

<code>processPayment() {
  switch(paymentGateway) {
    case 'RazorPay': 
       transaction = new RazorPayTransaction();
    break;
    case 'Paypal': 
       transaction = new PaypalTransaction();
    break; 
    case 'Paypal': 
       transaction = new StripeTransaction();
    break; 
  }
}</code>

However the above code looks more cleaner when we apply design pattern the code looks as fallow

<code>createRazorPayTransaction() {
  transaction = new RazorPayTransaction();
  return transaction;
}
createPaypalTransaction() {
  transaction = new PaypalTransaction();
  return transaction;
}
createTransactionFactory(paymentGateway) {
  switch(paymentGateway) {
    case 'RazorPay': 
       createRazorPayTransaction();
    break;
    case 'Paypal': 
       createPaypalTransaction();
    break; 
    case 'Paypal': 
    break; 
  }
}
processPayment() {
  transaction = createTransactionFactory('RazorPay');
}</code>

the above code looks simple and we may not understand the significance of it with a small scale application. However the creational design pattern brings in lot of flexibility, like any complexity with respect to a particular payment gateway will be handled with respective creation method.

for example if I try to elaborate little about one particular method it may looks as below, and this shows how a particular method becomes complex while considering various cases.

<code>createPaypalTransaction() {
  transaction = new PaypalTransaction();
   if (cardType === 'Master') {
     if (country === 'India') {
        if (cardKind === 'Chip') {

        }
      }
    }
  return transaction;
}</code>

Abstract Factory Pattern

Abstract factory pattern simplifies and makes the pattern into more object oriented and enforces future extendability to follow certain strict guidelines by defining interfaces.

Taking an example of developing a super software to handle a robotic machines. There are various types of robotic machines like marine explorer, space explorer and terrain explorer. Its great to have abstract class which enforces and defines basic structure of the factory class. Let us see how does it look

<code>abstract class RoboticMachineFactory {
  createCrawlingRobot();
  createRunningRobot();
  createFlyingRobot();
}

class SpaceExplorerRobotFactory extends RoboticMachineFactory {
  createCrawlingRobot();
  createRunningRobot();
  createFlyingRobot();
}

class TerrainExplorerRobotFactory extends RoboticMachineFactory {
  createCrawlingRobot();
  createRunningRobot();
  createFlyingRobot();
}

class MarineExplorerRobotFactory extends RoboticMachineFactory {
  createCrawlingRobot();
}</code>

Read Classification of Design Patterns

Introduction to Struts

Struts is an open source web application framework which will be used to develop the web-
based applications. Struts framework was implemented based on 3 popular design patterns.

  • MVC design pattern.
  • Front Controller design pattern.
  • Application Controller design pattern.

MVC design pattern :-

There are 3 layers in MVC Design pattern

I.  Presentation Layer

II.  Controller Layer

III.  Model Layer

Capture1

I Presentation Layer :-

It contains the presentation logic and can be implemented with JSP or with any other presentation frameworks like flex, velocity etc. Struts Presentation Layer contains following components.

1)     JSPs/HTMLs
2)      Struts custom tags
3)      Form Beans
4)      Message Bundles

1)     JSPs/HTMLs:

In struts presentation layer JSPs will be used to display the data to the client and also used to receive the input data from the client.

2)      Struts custom tags:

Apache has implemented various custom tags for the struts based application development and are categorized as follows.

  • Html tag libraries
  • Bean tag libraries
  • Logic tag libraries
  • Tiles tag libraries
  • Template tag libraries
  • Nested tag libraries
  1. Form Beans:

Form bean is a simple java bean styled class which stores the data. When we are writing the form bean java class, the bean class must be a subclass of one of the following.

  • ActionForm
  • DynaActionForm
  • ValidatorForm
  • DynaValidatorForm
  • ValidatorActionForm
  • DynaValidatorActionForm

 

  1. Message Bundles:

Message Bundles are nothing but property files which contain key value pair. Mainly message bundles will be used to achieve i18n (internationalization).

II Controller Layer :-

The Controller portion of the application is focused on receiving requests from the client (typically a user running a web browser), deciding what business logic function is to be performed, and then delegating responsibility for producing the next phase of the user interface to an appropriate View component. Following are the various components which we use in struts controller layer.   

   i)   ActionServlet

   ii)  RequestProcessor

   iii)  Action class

 i) ActionServlet:

ActionServlet is the one and only servlet for the entire struts application which is implemented based on Front controller design pattern and this servlet is loaded during server startup time. ActionServlet is responsible for receiving all incoming request and delegating to RequestProcessor. It also initialize the struts-config.xml file.

 ii)  RequestProcessor

RequestProcessor is responsible for processing all incoming request. It is implemented on Application Controller Design pattern. RequestProcessor is responsible for identifying the incoming action class and its form bean, it is also responsible for managing life cycle of form bean. After getting response, it manages to return to corresponding jsp by using ActionForward.

iii)  Action class

Action Class is the beginning of application’s business logic.

III Model Layer : –

Since there are no predefined components for model layer in struts like presentation layer and controller layer, so struts will not deal with model layer. But struts allows us to make use of any of the following model layer.

i)    Simple JDBC component

ii)   DAO + Hibernate

iii)  DAO + JDBC

iv)  Spring

v)   Web services     etc…

STRUTS FLOW

Capture1flow diagram showing struts flow

Steps for the flow         

  • When we submit the request after entering value in jsp form, request will be submitted to ActionServlet which is configured in web.xml.
  • This ActionServlet is responsible for initializing struts-config.xml and this struts-config.xml file is     configured in web.xml.
  • ActionServlet delegates the request to RequestProcessor
  • RequestProcessor takes the incoming request uri and tries to find the matching <action> tag in struts-config.xml. If no matching action is found, then client will get “cannot retrieve mapping for action”.
  • Once matching action is found for the incoming request uri, then corresponding “name” will be taken and tries to find the corresponding <form-bean> under <form-beans> configuration. If no matching <form-bean> is found then client will get the error called “cannot retrieve definition for form beans”.
  •  Once matching is found, RequestProcessor is responsible for managing form-bean life cycle.
  •  RequestProcessor takes the <action> “type” which is Action java class and create the instance    for the first time. With that Action class instance, execute() method  will be called.
  •  Once execute() method is completed ActionForward will be returned to RequestProcessor with      the forward name associated with the ActionForward object and RequestProcessor will try to  identify corresponding <forward> inside struts-config.xml file.
  •  Once the forward is found then its path will be taken and will be forwarded to the client.
  •   RequestProcessor takes the <action> “type” which is Action java class and create the instance     for the first time. With that Action class instance, execute() method  will be called.
  •  Once execute() method is completed ActionForward will be returned to RequestProcessor with  the forward name associated with the ActionForward object and RequestProcessor will try to  identify corresponding <forward> inside struts-config.xml file.
  •  Once the forward is found then its path will be taken and will be forwarded to the client.