Friday, 7 February 2014

How To....

The Command Pattern:
"Encapsulates a request as an object, thereby letting you parameterise other objects with different requests queue or log requests and support undoable operations."
It supports the decoupling of the invoker of a request and receiver of the request.
-Head First Design Patterns.
ex: Home automation systems,  ... or where ever you have to press the bottons then think of you as a client and the webpage which holds the button as innocent invoker(he is not responsible for all unexpected actions :))


 This Picture is from:
http://www.oodesign.com/command-pattern.html
 I remembered my dissertation in MSc. Jobs come to the server in a queue, and these jobs are discarded once they are done with it. 
Let us think about Briton got talent.  Let us say "ITV"  is our client. Ant&Deck are (is) our invoker/s. All vendors have different kinds of act to show. Our commands are performing acts.  This is the audition taking place some where in Birmingham  and it is a all day process and people can come anytime between 9am - 5pm and put thier name in the list carried by our invoker (Ant&Deck).  They have nothing to do with who is doing the act. They just have to announce thier name  according to the list and all acts are come as a surprise. Simon Cowell , Alesha Dixon ... undo button invokers.
public interface PerformingAct{
public void perform();
}
public class SingingAct implements PerformingAct{
Team team;
public SingingAct(Team team){
this.team =team;
}
public void perform(){
team.sing();
}
public class StopSingingAct impements PerformingAct{
Team team;
public StopSingingAct(Team team){
this.team =team;
}
public void perform(){
team.retreat();
}
}
Same way we can implement DancingAct, MimingAct,ScreamingAct....and stoping all those acts in a class.
This is our invoker.
public class Invoker{
ArrayList acts;//you can also use a LinkedList or a Queue.
   static PerformingAct stopact;
static int count ==0 ;
public Invoker(){
acts =  new ArrayList();// all invoker may have a list for marking purposes.
}

public void addAct(PerformingAct act){
acts.add(act);
}
public void announce(PerformingAct act){

setStopAct(act);
//check the size of the arraylist here
  PerformingAct act1 = (PerformingAct)acts.remove(0);
act1.perform();
}
public void xbottunPushed(){
count = count+1;
if( count >=2){
stopact.perform();
count ==0;
}
}
public void setStopAct(PerformingAct act){
stopact = act;
}
}
On the day of audition,
public class LoadPerformingActs{
public static void main(String[] args){
Invoker antdeck = new Invoker();
Invoker simon = new Invoker();
Invoker alesha = new Invoker();

Team teamA = new Team();
Team teamB = new Team();
PerformingAct diversity = new DancingAct(teamA);
PerformingAct stopDiversity = new StopDancingAct(teamA);
PerformingAct mindBlowing = new SingingAct(teamB);
PerformingAct stopMindBlowing = new StopSingingAct(teamB);
antdeck.add(diversity);
antdeck.add(mindBlowing);
antdeck.announce(stopDiversity);
simon.xbottonPushed();//this needs thread synchronization
alesha.xbottonPushed();/**this needs thread synchronization in real life. Here we assume that alesha and simon does not push the button at the same time.
.......
continues until 5 pm. **/
}
}
Just have to remember if  you make a class for commands they can be set in a queue and invokers only remember to execute the commands and  all actions are vendors' responsibility. 
  In this way commands helps  Invokers to detach themselves from responsibilities.

How to detach yourself from responsibilities. :>)


No comments:

Post a Comment