CSC/ECE 517 Fall 2012/ch2b 2w40 sn: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 138: Line 138:


= Chain of Responsibility =
= Chain of Responsibility =
Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it.
This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern].


== Comparison with Command Pattern ==
== Comparison with Command Pattern ==

Revision as of 08:55, 17 November 2012

Introduction to Command Pattern

The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed 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.

The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.

The intent of the Command pattern can be listed as:

  1. encapsulate a request in an object
  2. allows the parametrization of clients with different requests
  3. allows saving the requests in a queue

A Java Example

As the figure above suggests the integral parts of the command pattern are the client,invoker and the receiver.The command part is split into two parts-the interface and the concrete command. The examples are explored more in the below example. This is the command interface which contains the skeleton code of the command pattern containing exactly one method called execute.

//Command
public interface Command
{
    public void execute();
}
//Concrete Command
public class LightOnCommand implements Command
{
    //reference to the light
    Light light;
    
    public LightOnCommand(Light light)
    {
        this.light = light;
    }
    
    public void execute()
    {
        light.switchOn();
    }
    
}
 
 //Concrete Command
public class LightOffCommand implementsCommand
{
    //reference to the light
    Light light;
    
    public LightOffCommand(Light light)
    {
        this.light = light;
    }
    
    public void execute()
    {
        light.switchOff();
    }
    
}

LightOnCommand and LightOffCommand represents the concrete command classes that the client shall use.

Light is the receiver class which contains the commands to be executed.

//Receiver
public class Light
{
   private boolean on;
  
   public void switchOn()
   {
      on = true;
   }
  
   public void switchOff()
   {
      on = false;
   }
  
}

The invoker is the one which actually which calls the execute method of the command class. This also has a accessor method which sets the current command to be executed.

//Invoker
public class RemoteControl
{
    private Command command;

    public void setCommand(Command command)
    {
        this.command = command;
    }
    
   
    public void pressButton()
    {
        command.execute();
    }
    
}

And finally there is the Client class which will use the commands to switch the lights on and off.

//Client
public class Client
{
    public static void main(String[] args)
    {
        RemoteControl control = new RemoteControl();
        
        Light light = new Light();
        
        Command lightsOn = new LightsOnCommand(light);
        Command lightsOff = new LightsOffCommand(light);
        
        //switch on
        control.setCommand(lightsOn);
        control.pressButton();
        
        //switch off
        control.setCommand(lightsOff);
        control.pressButton();
    
    }

}

Real life applications

The command pattern can be used when the following things are desired:

  1. Specify,queue and execute requests at different times.
  2. To support operations like Undo,Redo.
  3. Support Logging changes so that they can be reapplied in case of a system crash.If the command interface is extended to include the load and store operations, a persistent history of changes can be kept.
  4. These can also be applied to transactions. They have a common interface so all the transactions can be invoked in the same way. It is also helpful to rollback transaction if something goes wrong.
  5. It is also used for implementing GUI objects. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on.

For an additional list of applications users can read this [1].

Chain of Responsibility

Chain-of-responsibility pattern is another behavioral design pattern consisting of a source of command objects and a series of processing objects. Processing objects contain logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. The basic intent of this pattern is to chain the receiving objects and pass the request along the chain until an object handles it. This pattern promotes decoupling between senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it. More information can be handled from here [2].

Comparison with Command Pattern

Real life applications

Memento

Comparison with Command Pattern

Real life applications

Strategy

Comparison with Command Pattern

Real life applications

Conclusion

Advantages and Disadvantages