Tuesday, November 11, 2008

Facad Pattern

What is Façade Pattern?

Façade provides a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Have a look into following diagram







Consequences of façade-:

  • Façade shields the clients from complex subsystem components , and prevents a simpler programming interface for general users.
  • However it doesn’t prevent advance users from going deeper to more complex classes when needed.
  • Façade pattern provides us the flexibility to change under laying subsystem without effecting the client code.

Examples of façade pattern

  • Now let’s suppose we have a client who may want to register, update, login, logout a user into our system.
  • All these processes need complex operations to be excuted e.g
  • In registration process we may need to do following things before registering a user

Ø User’s country is in our allowed countries list.

Ø User name is valid.

Ø Password is valid.

Ø User with same name doesn’t already exist.

Ø Credit card validation, if user is supposed to pay for the registration.

Ø Required amount available in the card.

e.t.c.

  • Now without using façade pattern our client needs to first call all those complex subsystem methods , he/she has to remember about all the parameters in those methods , values they return what the returned values mean etc. This will actually create a messy and complex code at client end. Flow chart client has to follow will look like this


















  • You can clearly see how the code will become complex in this case and with the addition of any new thing in the registration process we will also need to update the client code.
  • It become even worst in case of client/server application as you can clearly see how many numbers of network calls will be there before the actual operation i.e. user registration ( assuming that all the validation and other complex operations are to be performed on server end) is performed.
  • Let’s implement façade pattern in this scenario , according to façade pattern we should provide simple programming interface to the user and shield other complexities from him/her, so on server end we can create a single method and pass it all the required parameters as

registerUser(String username, String userPassword, String creditCardNo,String countryCode){

if user doesn’t belong to an allowed country

return error code;

if user name not valid

return error code;

if user password not valid

return error code;

if user already exists

return error code;

if credit card not allowed

return error code;

if required Amount is not available

return error code;

//if the above validations are ok then

· Amount deduction from credit card.

· Register user into the system (database).

· Return a success code to the client.

}

Note: Body of the above method is in pseudo code, we will look into a practical example soon.

  • Similarly we can implement other complex operations such as login. Updating, logout, buy something, sell something, logout.
  • And in the end we may have a full fledge class of all those methods. All those methods will be shielding complex business operations and provide a simple interface to client to call.
  • Lets call the class having those methods as MyFacadeWithCommandPattern.

Monday, November 10, 2008

Command Pattern

Command Pattern:

Definition:

Command pattern is an object behavioral pattern that allows us to achieve complete decoupling between sender and receiver. With this decoupling sender has no knowledge of the receiver’s interface.

Command pattern enables a client to send command without knowing about actions to be performed, those actions can later on be changed without effecting client.

The key to command pattern is the Command interface which in it’s simple form contains the execute method. Each Command class must define it’s own execute method.

Command pattern turns the request into an object, this object can be stored and passed around like other objects.

Another advantage of command pattern is to implement undo methods which actually enables us to undo the action of a command, this

Uses for the Command pattern

Command objects are useful for implementing:

Multi-level undo

If all user actions in a program are implemented as command objects, the program can keep a stack of the most recently executed commands. When the user wants to undo a command, the program simply pops the most recent command object and executes its undo() method. Note that it is not possible in all cases to undo a command.

Transactional behavior

Undo is perhaps even more essential when it's called rollback and happens automatically when an operation fails partway through. Installers need this. So do databases. Command objects can also be used to implement two-phase commit.

Networking

It is possible to send whole command objects across the network to be executed on the other machines, for example player actions in computer games.

Example to understand the concept:

Let’s go through an example of implementing command pattern to understand things quickly

  1. Define The Command Interface

package blog.sajjadparacha.commandpattern;

public interface Command {

public void execute();

}

Defines the execute method to be implemented in each command.

  1. Define The receivers

LIGHT

package blog.sajjadparacha.commandpattern;

public class Light {

public void turnLightOn(){

System.out.println("Light turned on");

}

public void turnLightOff(){

System.out.println("Light turned off");

}

}

FAN

package blog.sajjadparacha.commandpattern;

public class Fan {

public void startRoatate(){

System.out.println("Fan turned on");

}

public void stopRotate(){

System.out.println("Fan turned off");

}

}

Fan and Light here are receiver objects which actually receive a command.

  1. Define The invoker

package blog.sajjadparacha.commandpattern;

public class Switch {

Command upCommand,downCommand;

public Switch(Command up,Command down){

upCommand=up;downCommand=down;

}

public void flipUp(){

upCommand.execute();

}

public void flipDown(){

downCommand.execute();

}

}

Switch stores a command and executes it in its functions flipUp/flipDown.

  1. Define Commands

LightOnCommand

package blog.sajjadparacha.commandpatteren.commands;

import blog.sajjadparacha.commandpattern.Command;

import blog.sajjadparacha.commandpattern.Light;

public class LightOnCommand implements Command{

private Light myLight;

public LightOnCommand(Light light){

myLight=light;

}

public void execute() {

myLight.turnLightOn();

}

}

LightOffCommand

package blog.sajjadparacha.commandpatteren.commands;

import blog.sajjadparacha.commandpattern.Command;

import blog.sajjadparacha.commandpattern.Light;

public class LightOffCommand implements Command {

public void execute() {

mylight.turnLightOff();

}

private Light mylight;

public LightOffCommand(Light light){

mylight=light;

}

}

LightOnCommand and LightOffCommand are implementation of Command, They store The receiver as an instance inside them and then later on in execute method appropriate method of receiver is called.

  1. Test Command Pattern

package blog.sajjadparacha.commandpattern;

import blog.sajjadparacha.commandpatteren.commands.FanOffCommand;

import blog.sajjadparacha.commandpatteren.commands.FanOnCommand;

import blog.sajjadparacha.commandpatteren.commands.LightOffCommand;

import blog.sajjadparacha.commandpatteren.commands.LightOnCommand;

public class SwitchTest {

/**

* @param args

*/

public static void main(String[] args) {

//**Turn light on/off

Light light = new Light();

LightOnCommand lightOnCommand=new LightOnCommand(light);

LightOffCommand lightOffCommand=new LightOffCommand(light);

Switch commandSwitch = new Switch(lightOnCommand,lightOffCommand);

commandSwitch.flipUp();

commandSwitch.flipDown();

//**Start and stop fan

Fan fan = new Fan();

FanOnCommand fanOnCommand=new FanOnCommand(fan);

FanOffCommand fanOffCommand=new FanOffCommand(fan);

commandSwitch = new Switch(fanOnCommand,fanOffCommand);

commandSwitch.flipUp();

commandSwitch.flipDown();

}

}

Conclusion:

Command pattern completely decouples the object that invokes the operation (Switch) from the ones having the knowledge to perform it (Light/Fan).

The object (Client) issuing the request must only know how to issue it; it doesn’t need to know how the request will be performed.