JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Command pattern
  • In object-oriented programming, the command pattern is a design pattern in which an object is used to represent and encapsulate all the information need to call a method at a later time.
  • This information includes the method name, the object that owns the method and values for the
    method parameters.
  • Three terms always associated with the command pattern are client, invoker and receiver.
  • The client instantiates the command object and provides the information required to call the method at a later time.
Purpose
  • Encapsulate a request as an object, thereby letting you parameterize clients with different
    requests, queue or log requests, and support undoable operations.

Applications
  • Parameterize objects by an action to perform, as MenuItem objects did above. 
  • You can express such parameterization in a procedural language with a
    callback function, that is, a function that's registered somewhere to
    be called at a later point.
  •  Commands are an object-oriented replacement for callbacks. 
  • Specify, queue, and execute requests at different times. 
  • A Command object can have a lifetime independent of the
    original request.
  •  If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.
Interaction


  • The client creates a ConcreteCommand object and specifies its receiver.
  • An Invoker object stores the ConcreteCommand object.
  • The invoker issues a request by calling Execute on the command. When commands
    are undoable, ConcreteCommand stores state for undoing the command prior to
    invoking Execute.
  • The ConcreteCommand object invokes operations on its receiver to carry out the
    request.
Examples of Coomand Pattern

  • The Command pattern allows requests to be encapsulated as objects, thereby
    allowing clients to be parameterized with different requests.
  •  The "check" at a diner is an example of a Command pattern.
  •  The waiter or waitress takes an order or command from a customer and encapsulates that order by writing it on the check.
  •  The order is then queued for a short order cook.


Example:

CommandQueue.java
import java.util.*;
public class CommandQueue {

interface Command void execute()}

static class DomesticEngineer implements Command {
   public void execute() {
      System.out.println"take out the trash" );
}  }

static class Politician implements Command {
   public void execute() {
      System.out.println"take money from the
rich, take votes from the poor" 
);
}  }

static class Programmer implements Command {
   public void execute() {
      System.out.println"sell the bugs, charge
 extra for the fixes" 
);
}  }

public static List produceRequests() {
   List queue = new ArrayList();
   queue.addnew DomesticEngineer() );
   queue.addnew Politician() );
   queue.addnew Programmer() );
   return queue;
}

public static void workOffRequestsList queue ) {
   for (Iterator it = queue.iterator(); it.hasNext())
      ((Command)it.next()).execute();
}

public static void mainString[] args ) {
   List queue = produceRequests();
   workOffRequestsqueue );
}}

Output:-


Download Zip code

Example:-

Cook.java
package simplefoodorder;

public class Cook {
  public Food prepareOrder(Order order,Waiter waiter)
  {
    Food food=getCookedFood(order);
    return food;
    
  }
  public Food getCookedFood(Order order) {
    Food food = new Food(order);
    return food;
    }

}

Customer.java
package simplefoodorder;

public class Customer {

  private String name;
  public Customer(String str)
  {
    name=str;
  }
  

Food.java
package simplefoodorder;

public class Food {
  Order order;
  public Food()
  {
    
  }
  public Food(Order order)
  {
    this.order=order;
  }
  public String toString() {
    return order.getCommand() +" has completed";
  }
  
  public void setOrder(Order order)
  {
    this.order=order;
  }
  public Order getOrder()
  {
    return order;
  }
}

Order.java
package simplefoodorder;

public class Order {

    String command;
  public Order(String command)
  {
    this.command=command;
  }
  
  public void setCommand(String command)
  {
    this.command=command;
  }
  
  public String getCommand()
  {
    return command;
  }
}

Waiter.java
package simplefoodorder;

public class Waiter {

  public Food takeOrder(Customer cust,Order order)
  {
    Cook cook=new Cook();
    Food food=cook.prepareOrder(order,this);
    return food;
  }
}

Test.java
package simplefoodorder;

public class Test {

  public static void main(String[] args) {
    Waiter waiter=new Waiter();
    Customer cust=new Customer("Samant");
    Order order= new Order("Cold drink");
    Food  f=waiter.takeOrder(cust,order);
    
    System.out.println(f);
    
  }
}

Output:-



Download Zip code