Sunday, December 30, 2012

Factory Method Design Pattern or Virtual Constructor

Factory method design pattern is one of the extensively used design patter in software development using java. Concept of factory method design pattern is blend from factory which deals with the problem of creating products (in case of programming object).This is one of the basic creational design pattern.

Intent of factory method :

Define an interface for creating an object, but let subclasses decide which class
to instantiate. Factory Method lets a class defer instantiation to subclasses.

Assume an online banking system using two different sms gateway to deliver mobile banking notification to it's valued customers.System need to resolve which gateway will use by cell number assume hear gateway type. Based on category or need system need to create or return object/instance of a gateway. Hear is the practical example :
Gateway type enumeration :
package com.thinktank.rnd.se.pattern.factorymethod;


public enum GatewayType {
    ONE(1), TWO(2);

    private int value;

    private GatewayType(int value) {
        this.value = value;
    }

};
Gateway interface :
package com.thinktank.rnd.se.pattern.factorymethod;

public interface GateWay {
    public int connectToGateWay();
}

Implementation of gateway interface :
package com.thinktank.rnd.se.pattern.factorymethod;

public class SmsGatewayOne implements GateWay {
    public int connectToGateWay() {

        System.out.println("Connection Established With Gateway One");
        return 1;
    }
}

Another implementation of gateway interface :
package com.thinktank.rnd.se.pattern.factorymethod;


public class SmsGatewayTwo implements GateWay {
    public int connectToGateWay() {
        System.out.println("Connection Established to Gateway Two");
        return 1;
    }
}

Gateway factory class :
package com.thinktank.rnd.se.pattern.factorymethod;

public class GatewayFactory {

    public GateWay getGateway(GatewayType gatewayType) {

        switch (gatewayType) {
            case ONE:
                return new SmsGatewayOne();
            case TWO:
                return new SmsGatewayTwo();
        }
        return null;
    }
}

Main class for our factory method demo :
package com.thinktank.rnd.se.pattern.factorymethod;

public class DemoFactoryMethod {
    public static void main(String args[]) {

        GatewayFactory gatewayFactory = new GatewayFactory();
        GateWay gateWay = gatewayFactory.getGateway(GatewayType.TWO);
        gateWay.connectToGateWay();
    }
}


Use the Factory Method pattern when
·  a class can't anticipate the class of objects it must create.
·  a class wants its subclasses to specify the objects it creates.
·  classes delegate responsibility to one of several helper subclasses, and
you want to localize the knowledge of which helper subclass is the delegate.





Reference :
Design-patterns-elements-reusable-object-oriented
http://en.wikipedia.org/wiki/Design_Patterns
http://java.dzone.com/articles/design-patterns-factory
http://javapapers.com/design-patterns/factory-method-pattern/
http://en.wikipedia.org/wiki/Factory_method_pattern

Friday, December 28, 2012

Configure Tomcat for Remote Debug Your Web Application With Eclipse

Debugging is a vital part of developing any software system. Every developer relay on debug to find bugs and solve those quickly.In case of large code base it is too important.

To do remote debugging  with from eclipse we need to follow steps described bellow .

Before remote debug tomcat from eclipse you need to make sure :
  • Make sure that Tomcat is started in remote debugging mode
  • Make sure that you have the sources for the code that you are trying to debug in your IDE. For the libraries and for Tomcat itself you can "attach" the sources to the jar files: open a class file and then click "Attach Source..." button.
  • If you have a servlet or something, set a breakpoint where it is sure to hit on the next request.
  • Go to "Run->Debug Configurations...". Click on "Remote Java Applications", then click "New". Type in the title. Note that port 8000 from the Tomcat instructions. Save and run.
  • Eclipse will connect to the JVM that Tomcat is running under. Wow, that was easy!

Assume you are not starting tomcat from ide rathe using startup.sh/startup.bat file under /tomcat/bin.

Just add two lines in startup.bat at the end of file but before : 
call "%EXECUTABLE%" start %CMD_LINE_ARGS% 

set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket
And change :
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
to 
call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS% startup.bat
Now save the file as startup_debug.bat You are ready to start your tomcat in debug mode .Start your tomcat with newly saved *.bat file startup_debug.bat. This will run the tomcat in debug mode.

Setup debug configuration in eclipse ide :
Go to debug configuration this will appear like bellow and replace project name with your one.


You are ready to start debugging . Now hit URL in browser set break point and have fun with debugging .














If you are interested about JPDA then take a look : http://docs.oracle.com/javase/1.5.0/docs/guide/jpda/



Wednesday, December 12, 2012

REST Service Client Tool : Beginner's Way

REST means Representational state transfer one of the mechanism of writing web service . After writing web service we need to write a client for it for testing purpose .But by using simple tool we can save our time of writing a client to test our server side service even it is 10 min. There is a chrome browser named Simple REST Client which we can use to serve our purpose.
We can download it from :Chrome Extension (Simple REST Client). We will find an icon in our chrome as indicated in picture after installing it in our chrome browser.



If we click the icon previously mentioned we will get a UI (user interface) like :


Instruction to use it :
URL : put your service url hear , example : https://www.example.com/restService/payment
Method : depends on your endpoint . in case of us POST
Header : Content-Type: text/html;charset=utf-8 or Content-Type: text/xml;charset=utf-8 this define the request message type expected by service end.In case of us (Content-Type: text/html;charset=utf-8).
Data : If you put service url in URL then this Data section will appear.In this section we will put actual client request message such as :


  CC
  John
  Doe
  1234 Street
  WA
  123
  US
  v
  4100111111111111
  0115
  123
  usd
 
     
  123
  Fall 2013
  123
  BBA
 
 
  example description
           12/12/2012
 
 
 
      
  TYPE
  tuition
  2
  2
  1

 
 
  TYPE
  Late Fee
  1
  2
  1
 


Previous request message (xml) taken from a real world scenario so i changed the real information to dummy information.

Now , we just need to hit send button , it will send the request to service end and show us response as desired by client. Response example : :

1
1
This transaction has been approved.
9DJEXW
Y
2180716417
4.00
CC
billing first name
billing last name
dhaka dhaka 1229 bangladesh 2 XXXX1111 Visa

This extension will work as a REST client . That's all. Apart from it we can use SOAP UI but this is much more easier for beginner and later on SOPA UI can be used . Wish to write on SOAP UI how to later.