Monday, July 21, 2014

REST vs SOAP

REST stands for REpresentational State Transfer (REST) which enforces a stateless client server design where web services are treated as resource and can be accessed and identified by there URL unlike SOAP web services which were defined by WSDL.
Web services written by applying REST Architectural concept are called RESTful web services which focus on System resources and how state of Resource should be transferred over http protocol.

SOAP
SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in XML form.SOAP is protocol which relies on XML, that defines what is in the message and how to process it,set of encoding rules for data types, representation for procedure calls and responses.


REST vs SOAP

Before going for differences between two lets first see the differences in request header :

Sample SOAP & REST Request message for Weather Web Service :


 


SOAP vs REST Key Differences
Here are some of Key differences between SOAP and REST web Service :

  •  REST uses HTTP/HTTPS, SOAP can use almost any transport to send the request(for example we can have SOAP Messages over SMTP),  SOAP is a XML based messaging protocol.
  • REST is totally stateless operations where as SOAP supports statefull calls too. 
  • SOAP has more strict contract in the form of WSDL between applications, no such contract exists in case of REST.
  • AS REST APIs can be consumed using simple GET requests, RESTresponse can be cached, this is not possible with SOAP.
  • REST is Lighter, SOAP requires an XML wrapper around every request and response(SOAP Headers, XML tags etc). Thats why REST is preferred choice in mobile devices and PDA's
  •  SOAP message format is restricted to XML only where as REST supports other formats too for example JSON.  
  • REST is simpler in terms of development as compared to SOAP.
  • Browsers can handle REST easily as compared to SOAP as REST is based on HTTP where SOAP is another wrapper over HTTP.

Why to use SOAP ? 
SOAP provides some features like Security, Reliable Messaging, Atomic transaction which are not available in REST API.

  • WS-Security  & WS-SecureConversation  Provide support for using security tokens like Kerberos, and X.509.
  • WS-ReliableMessaging   Reliable messages delivery between distributed applications in case of failures.
  • WS-AtomicTransaction, two-phase commit across distributed transactional resources
  • In case where exact specification  of exchange format need to agreed between applications SOAP is better suitable.

Sunday, July 13, 2014

Maintaining Application State

We usually can create two types of cookies,
1. Session cookies.
2. Persistent cookies.

A session cookies exists only in memory. If a user closes the Web browser,  the session cookie disappears forever.

A persistent cookie, on the other hand, can last for months or even years. When you create a persistent cookie, the  cookie is stored permanently by the user's browers on the user's computer. Internet Explorer,  for example,  stores cookies in a set of text files contained in the following  folder:
\Documents and Settings\[user]\Cookies

Thursday, July 10, 2014

What is the difference between static class and singleton pattern?

In the first overview; we see both singleton class and static class are look similar. But there are many difference and here are the major points.

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common IME), so you can pass around the singleton as if it were "just another" implementation.

A singleton allows access to a single created instance - that instance (or rather, a reference to that instance) can be passed as a parameter to other methods, and treated as a normal object.

A static class allows only static methods.

Singleton object stores in Heap but, static object stores in stack.

We can clone the object of Singleton but, we can not clone the static class object.

Singleton class follow the OOP(object oriented principles) but not static class
we can implement interface with Singleton class but not with Static class.

Friday, July 4, 2014

Command Design Pattern

 

Implementing the Command interface

First things first: all command objects implement the same interface, which consists of one method.

here's the Command interface:

 public interface Command{  
   public void execute();  
 }  

Implementing a Command to turn a light on

Now, let's say you want to implement a command for turning a light on.
Referring to our set of vendor classes, the Light class has two methods: on() and off(). Here's how you can implement this as a command:


public class LightOnCommand : Command{  
   Light light;  
   public LightOnCommand(Light light){  
     this.light=light;  
   }  
   public void execute(){  
     light.on();  
   }  
 }  

Using the command object

Okey, let's make things simple: say we've got a remote control with only one button and corresponding slot to hold a device to control:


public class SimpleRemoteControl{  
   //we have one slot to hold our command which will control one device.  
   Command slot;  
   public SimpleRemoteControl(){}  
   //we have a method for setting the command the slot is going to control.   
   //This could be called multiple times if the client of this code wanted   
   //to change the behavior of the remote button.  
   public void setCommand(Command command){  
     slot=command;  
   }  
   //This method is called when the button is pressed .   
   //All we do take the current command bound to the slot and  
   //call its execute() method.  
   public void buttonWasPressed(){  
     slot.execute();  
   }  
 }

Creating a simple test to use the Remote Control

Here's just a bit of code to test out the simple remote control. Let's take a look and we'll point out how the pieces match the Command Pattern diagram:


//This is our client in Command pattern-speak.  
 public class RemoteControlTest{  
   public static void main(String[] args){  
     //The remote is our invoker; it will be passed a command object that can be used to make  
     //requests.  
     SimpleRemoteControl remote = new SimpleRemoteControl();  
     //Now we create a Light object, this will be the Receiver of the request.  
     Light light= new Light();  
     //Here, creates a Command and pass it to the Receiver.  
     LightOnCommand lightOn=new LightOnCommand(light);  
     remote.setCommand(lightOn);//Here, pass the command to the invoker.  
     remote.buttonWasPressed();//And then we simulate the button being pressed.  
   }  
 }  

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code