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 :

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

    private int value;

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

Gateway interface :

public interface GateWay {
    public int connectToGateWay();

Implementation of gateway interface :

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

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

Another implementation of gateway interface :

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

Gateway factory class :

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 :

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

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

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 :

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 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_TRANSPORT=dt_socket
And change :
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 :

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 :
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 :

  1234 Street
  Fall 2013
  example description

  Late Fee

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 : :

This transaction has been approved.
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.