CSC/ECE 517 Fall 2010/ch4 4f sv: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(142 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== '''Command Pattern in static and dynamic languages''' ==
= '''Command Pattern ''' =


== What is Command Pattern ? ==
== What is Command Pattern ? ==
The command pattern is a design pattern in which an object is 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. It encapsulates a request as an object and gives it a known public interface. It ensures that every object receives its own commands and provides a decoupling between sender and receiver. In this case a sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.
In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming], the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and [http://en.wikipedia.org/wiki/Encapsulation_(computer_science)#Encapsulation 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. It encapsulates a request as an object and gives it a known public interface. It ensures that every object receives its own commands and provides a [http://en.wikipedia.org/wiki/Decoupling#Software_Development decoupling] between sender and receiver. In this case a sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.


== Why and When do we use a Command Pattern? ==
== Why and When do we use a Command Pattern? ==
Command Pattern comes into picture when there is a need  to store objects behaviour as a command and its state. The Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.
Command Pattern comes into picture when there is a need  to store objects behaviour as a command and its state. The Command pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command pattern provides the options to queue commands, undo/redo actions and other manipulations.


There are several benefits of using the Command pattern:
There are several benefits[http://www.javaworld.com/javaworld/jw-06-2002/jw-0628-designpatterns.html] of using the Command pattern:


> It provides encapsulation of application logic so that it can be executed at a different point of time.
* It provides encapsulation of application logic so that it can be executed at a different point of time.


> It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.


> The separation between application logic and context allows to easier exchange the application logic.
* The separation between application logic and context allows to easier exchange the application logic.


== Components of a command pattern ==
== Components of a command pattern ==
<< to add diagram >>
The various components of a command pattern[http://www.oodesign.com/command-pattern.html]  are given as follows:
 
From the diagram above the major components for the command pattern can be listed out as follows.


'''Client:''' Invokes a particular module using a command and passes a request which gets propagated as a command.
'''Client:''' Invokes a particular module using a command and passes a request which gets propagated as a command.
Line 28: Line 26:
'''Invoker:''' It is the class that is invoked by the client. It takes in the request and calls the receiver by passing the command to it and asks it to carry out the request.
'''Invoker:''' It is the class that is invoked by the client. It takes in the request and calls the receiver by passing the command to it and asks it to carry out the request.


'''Concrete Command:''' <<to add matter>>
'''Concrete Command:''' It defines a link between the receiver and the action. It basically implements the execute method and invokes the corresponding operations on the receiver.
 
Throughout this page, we will discuss the example of an operation of converting a string into either upper case or lower case.
 
[[Image:WIKI.png]]
 
The components corresponding to the example are given in the figure above. From the figure we can identify the components as follows:
 
'''Client:''' Menu Class is used to set the ConcreteCommand and then sets the convert class
 
'''Command:''' Command Class in this case is declaring the interface which is used to execute the operation.
 
'''Receiver:''' Convert Class is the one which knows how to perform the operations.
 
'''Invoker:'''  TextOperation Class asks the command to carry out the action which in this case is to either convert toUpper or toLower.
 
'''ConcreteCommand:''' ConvertUpper, ConvertLower classes are the ones that implement the execute command.
 
== Example of Command pattern ==
 
Consider the case of converting a string to upper and lower case.The Client is the Menu where we see the options. The menu sends the request to the receiver(convert class) through the Invoker(TextOperations class). The textOperation encapsulates the command and then forwards it, creating the ConcreteCommand object which is the command itself. The Receiver will be the Convert class that, after completing all the commands that were sent to it before the command in question(to Upper case or to lower case ), starts to work on it.
 
= '''Implementation in static and dynamic languages''' =
 
== Implementation in Static languages ==
We initially define a Command interface with a method named execute() and then we create command classes that implement this interface. The execute() method of each object will contain the logic specific to the action you are encapsulating.
(For instance, you might create a convertUpper and a converLower command in a text editor application.)
 
In case of the static implementation, code snippets to implement the command pattern in languages like Java and C# are given as follows:
 
=== Java Example ===
 
//This is the command interface.
public interface Command
{
    void execute();
}
//A command to convert to lower case.
public class convertLower implements Command
{
private Convert convert;
    private convertLower(Convert conv)
    {
        this.convert = conv;
    }
    public void execute()
    {
        convert.toLower();
    }
}
//A command to convert to upper case
public class convertUpper implements Command
{
  private Convert convert;
    private convertUpper(Convert conv)
    {
        this.convert = conv;
    }
    public void execute()
    {
        convert.toUpper();
    }
}
//This is the reciever class.
public class Convert
{
    public Convert()
    {
   
    }
    public final void toUpper()
    {
        System.out.print("Turning all lower case elements to upper case");//The actual action that is being performed.
    }
    public final void toLower()
    {
        System.out.print("Converting all upper case elements to lower case");//The actual action that is being performed.
    }
}
// The invoker class
public class TextOperations
{
    private Command convertUp;
    private Command convertLow;
        private TextOperations(Command toup, Command todwn)
        {
            this.convertUp = toup;
            this.convertLow = todwn;
        }
        public void convToUp()
        {
            convertUp.execute();//The execute method that calls the execute method in the interface
        }
        public void convToLow()
        {
            convertLow.execute();//The execute method that calls the execute method in the interface
        }
   
}
// This is the client class.
public class Menu
{
    public static void main(String args[])
    {
        Convert c = new Convert();
        Command cou = new convertUpper(c);
        Command col = new convertLower(c);
        TextOperations converter = new TextOperations(cou,col); /*Client calls the invoker and gives the commands to execute*/
        converter.convToLow();
        converter.convToUp();
    }
}
 
=== C# Example ===
 
//This is the command interface.
public interface Command
{
    void execute(); //The execute method that is going to call the action
}
//A command to convert to lower case.
public class convertLower : Command
{
    private Convert convert;
    private convertLower(Convert conv)
    {
        this.convert = conv;
    }
    public void execute()
    {
        convert.toLower();
    }
}
//A command to convert to upper case
public class convertUpper : Command
{
    private Convert convert;
    private convertUpper(Convert conv)
    {
        this.convert = conv;
    }
    public void execute()
    {
        convert.toUpper();
    }
 
}
//This is the reciever class.
public class Convert
{
    public Convert()
    {
   
    }
    public final void toUpper()
    {
        Console.WriteLine("Turning all lower case elements to upper case");// These are the actual actions that are performed
    }
    public final void toLower()
    {
        Console.WriteLine("Converting all upper case elements to lower case"); //These is the actual action that is performed
    }
}
// The invoker class
public class TextOperations
{
    private Command convertUp;
    private Command convertLow;
        private TextOperations(Command toup, Command todwn)
        {
            this.convertUp = toup;
            this.convertLow = todwn;
        }
        public virtual void convToUp()
        {
            convertUp.execute();
        }
        public virtual void convToLow()
        {
            convertLow.execute();
        }
}
// This is the client class.
public class Menu
{
    Static void Main(string[] args)
    {
        Convert c = new Convert();
        Command cou = new convertUpper(c);
        Command col = new convertLower(c);
        TextOperations converter = new TextOperations(cou,col); /*Client calls the invoker and gives the commands to execute*/
        converter.convToLow();
        converter.convToUp();
    }
}
 
== Implementation in Dynamic Languages ==
 
In [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamic languages] we will have a controller class that constructs each of the Command objects.Each of these command objects is passed to an invoker class. The invoker class calls the execute() method of a Command object at the appropriate time.
(For instance this may happen when a user selects a menu item or presses a button)
 
In case of the dynamic languages, the code snippets to implement command pattern for ruby and python are given as follows.
 
=== Ruby Example ===
 
#This is the command interface.
class Command
def execute()
end
end
#A command to convert to lower case.
class convertLower < Command
def initialize(conv)
self.@convert = conv
end
def execute()
@convert.toLower()
end
end
#A command to convert to upper case
class convertUpper < Command
def initialize(conv)
self.@convert = conv
end
def execute()
@convert.toUpper()
end
end
#This is the reciever class.
class Convert
def initialize()
end
def toUpper()
Console.WriteLine("Turning all lower case elements to upper case")
end
def toLower()
Console.WriteLine("Converting all upper case elements to lower case")
end
end
# The invoker class
class TextOperations
def initialize(toup, todwn)
self.@convertUp = toup
self.@convertLow = todwn
end
def convToUp()
@convertUp.execute()
end
def convToLow()
@convertLow.execute()
end
end
# This is the client class.
class Menu
def Main(args)
c = Convert.new()
cou = convertUpper.new(c)
col = convertLower.new(c)
converter = TextOperations.new(cou, col) # Client calls the invoker and gives the commands to execute
converter.convToLow()
converter.convToUp()
end
end
 
=== Python Example ===
 
#This is the command interface.
class Command(object):
def execute(self):
pass
#A command to convert to lower case.
class convertLower(Command):
def __init__(self, conv):
self._convert = conv
def execute(self):
self._convert.toLower()
#A command to convert to upper case
class convertUpper(Command):
def __init__(self, conv):
self._convert = conv
def execute(self):
self._convert.toUpper()
#This is the reciever class.
class Convert(object):
def __init__(self):
pass
def toUpper(self):
Console.WriteLine("Turning all lower case elements to upper case")
def toLower(self):
Console.WriteLine("Converting all upper case elements to lower case")
# The invoker class
class TextOperations(object):
def __init__(self, toup, todwn):
self._convertUp = toup
self._convertLow = todwn
def convToUp(self):
self._convertUp.execute()
def convToLow(self):
self._convertLow.execute()
# This is the client class.
class Menu(object):
def Main(self, args):
c = Convert()
cou = convertUpper(c)
col = convertLower(c)
converter = TextOperations(cou, col) # Client calls the invoker and gives the commands to execute
converter.convToLow()
converter.convToUp()
 
== Static vs Dynamic implementations ==
In case of the implementations ruby has better implementation of the command patterns than the languages like Java. Some of the advantages of Ruby over Java can be given as follows.
* To implement the command pattern in static languages like Java, we have to define a common interface which has to be extended by all the other classes. In case of ruby we can use procs which makes it easier to implement.
* Ruby implements something called mixins in order to implement the command pattern which is efficient compared to Java.
* The lines of code for static language like Java is more than that taken by the dynamic language like Ruby. This is more because of the proc objects that are present in ruby which are concisely defined than that present in Java.
* Since Ruby is a dynamic language, one can add the commands to the command array during runtime. Java is a static language and hence does not support this feature.
 
== Conclusion ==
Hence it can be summarized that the dynamically typed languages keep the code straight and narrow as it does not need to depend on static type-checking. Also due to the flexibility of the dynamic languages, writing code is significantly easier.
 
= '''References''' =
 
[[#References|[1]]] Freeman,E., and Robson,E.,and Bates,B.,and Sierra,K. 2004. Head First Design Patterns
 
[[#References|[2]]] Wikipedia. (2010, October) Wikipedia - Command_Pattern. [Online].
http://en.wikipedia.org/wiki/Command_pattern
 
[[#References|[3]]] Oodesign. (2005-2006)- command-pattern. [Online]. http://www.oodesign.com/command-pattern.html
 
[[#References|[4]]] Erich,G., and Richard,H.,and Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software
 
[[#References|[5]]] James,C., 1998. The Design Patterns-Java Companion. [Online]. http://www.patterndepot.com/put/8/command.pdf

Latest revision as of 00:11, 4 October 2012

Command Pattern

What is 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 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. It encapsulates a request as an object and gives it a known public interface. It ensures that every object receives its own commands and provides a decoupling between sender and receiver. In this case a sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.

Why and When do we use a Command Pattern?

Command Pattern comes into picture when there is a need to store objects behaviour as a command and its state. The Command pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command pattern provides the options to queue commands, undo/redo actions and other manipulations.

There are several benefits[1] of using the Command pattern:

  • It provides encapsulation of application logic so that it can be executed at a different point of time.
  • It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.
  • The separation between application logic and context allows to easier exchange the application logic.

Components of a command pattern

The various components of a command pattern[2] are given as follows:

Client: Invokes a particular module using a command and passes a request which gets propagated as a command.

Command: It is an object that encapsulates a request to the receiver. Command request maps to particular modules. According to the command, a module is invoked.

Receiver: It is the component that is acted upon by each request. It knows how to perform the operations associated with carrying out a certain request.

Invoker: It is the class that is invoked by the client. It takes in the request and calls the receiver by passing the command to it and asks it to carry out the request.

Concrete Command: It defines a link between the receiver and the action. It basically implements the execute method and invokes the corresponding operations on the receiver.

Throughout this page, we will discuss the example of an operation of converting a string into either upper case or lower case.

The components corresponding to the example are given in the figure above. From the figure we can identify the components as follows:

Client: Menu Class is used to set the ConcreteCommand and then sets the convert class

Command: Command Class in this case is declaring the interface which is used to execute the operation.

Receiver: Convert Class is the one which knows how to perform the operations.

Invoker: TextOperation Class asks the command to carry out the action which in this case is to either convert toUpper or toLower.

ConcreteCommand: ConvertUpper, ConvertLower classes are the ones that implement the execute command.

Example of Command pattern

Consider the case of converting a string to upper and lower case.The Client is the Menu where we see the options. The menu sends the request to the receiver(convert class) through the Invoker(TextOperations class). The textOperation encapsulates the command and then forwards it, creating the ConcreteCommand object which is the command itself. The Receiver will be the Convert class that, after completing all the commands that were sent to it before the command in question(to Upper case or to lower case ), starts to work on it.

Implementation in static and dynamic languages

Implementation in Static languages

We initially define a Command interface with a method named execute() and then we create command classes that implement this interface. The execute() method of each object will contain the logic specific to the action you are encapsulating. (For instance, you might create a convertUpper and a converLower command in a text editor application.)

In case of the static implementation, code snippets to implement the command pattern in languages like Java and C# are given as follows:

Java Example

//This is the command interface.
public interface Command
{
    void execute();
}

//A command to convert to lower case.
public class convertLower implements Command
{
	 private Convert convert;
   private convertLower(Convert conv)
   {
       this.convert = conv;
   }
   public void execute()
   {
       convert.toLower();
   }
}

//A command to convert to upper case
public class convertUpper implements Command
{
 private Convert convert;
   private convertUpper(Convert conv)
   {
       this.convert = conv;
   }
   public void execute()
   {
       convert.toUpper();
   }
}

//This is the reciever class.
public class Convert
{
   public Convert()
   {
   
   }

   public final void toUpper()
   {
       System.out.print("Turning all lower case elements to upper case");//The actual action that is being performed.
   }

   public final void toLower()
   {
       System.out.print("Converting all upper case elements to lower case");//The actual action that is being performed.
   }
}

// The invoker class
public class TextOperations
{
   private Command convertUp;
   private Command convertLow;
       private TextOperations(Command toup, Command todwn)
       {
           this.convertUp = toup;
           this.convertLow = todwn;
       }
       public void convToUp()
       {
           convertUp.execute();//The execute method that calls the execute method in the interface
       }
       public void convToLow()
       {
           convertLow.execute();//The execute method that calls the execute method in the interface
       }
   
}

// This is the client class.
public class Menu
{
   public static void main(String args[])
   {
       Convert c = new Convert();
       Command cou = new convertUpper(c);
       Command col = new convertLower(c);

       TextOperations converter = new TextOperations(cou,col); /*Client calls the invoker and gives the commands to execute*/
       converter.convToLow();
       converter.convToUp();
   }
}

C# Example

//This is the command interface.
public interface Command
{
    void execute(); //The execute method that is going to call the action
}

//A command to convert to lower case.
public class convertLower : Command
{
   private Convert convert;
   private convertLower(Convert conv)
   {
       this.convert = conv;
   }
   public void execute()
   {
       convert.toLower();
   }
}

//A command to convert to upper case
public class convertUpper : Command
{
   private Convert convert;
   private convertUpper(Convert conv)
   {
       this.convert = conv;
   }
   public void execute()
   {
       convert.toUpper();
   }
  
}

//This is the reciever class.
public class Convert
{
   public Convert()
   {
   
   }

   public final void toUpper()
   {
       Console.WriteLine("Turning all lower case elements to upper case");// These are the actual actions that are performed
   }

   public final void toLower()
   {
       Console.WriteLine("Converting all upper case elements to lower case"); //These is the actual action that is performed
   }
}

// The invoker class
public class TextOperations
{
   private Command convertUp;
   private Command convertLow;
       private TextOperations(Command toup, Command todwn)
       {
           this.convertUp = toup;
           this.convertLow = todwn;
       }
       public virtual void convToUp()
       {
           convertUp.execute();
       }
       public virtual void convToLow()
       {
           convertLow.execute();
       }
}

// This is the client class.
public class Menu
{
   Static void Main(string[] args)
   {
       Convert c = new Convert();
       Command cou = new convertUpper(c);
       Command col = new convertLower(c);

       TextOperations converter = new TextOperations(cou,col); /*Client calls the invoker and gives the commands to execute*/
       converter.convToLow();
       converter.convToUp();
   }
}

Implementation in Dynamic Languages

In dynamic languages we will have a controller class that constructs each of the Command objects.Each of these command objects is passed to an invoker class. The invoker class calls the execute() method of a Command object at the appropriate time. (For instance this may happen when a user selects a menu item or presses a button)

In case of the dynamic languages, the code snippets to implement command pattern for ruby and python are given as follows.

Ruby Example

#This is the command interface.
class Command
	def execute()
	end
end
#A command to convert to lower case.
class convertLower < Command
	def initialize(conv)
		self.@convert = conv
	end

	def execute()
		@convert.toLower()
	end
end
#A command to convert to upper case
class convertUpper < Command
	def initialize(conv)
		self.@convert = conv
	end

	def execute()
		@convert.toUpper()
	end
end
#This is the reciever class.
class Convert
	def initialize()
	end

	def toUpper()
		Console.WriteLine("Turning all lower case elements to upper case")
	end

	def toLower()
		Console.WriteLine("Converting all upper case elements to lower case")
	end
end
# The invoker class
class TextOperations
	def initialize(toup, todwn)
		self.@convertUp = toup
		self.@convertLow = todwn
	end

	def convToUp()
		@convertUp.execute()
	end

	def convToLow()
		@convertLow.execute()
	end
end
# This is the client class.
class Menu
	def Main(args)
		c = Convert.new()
		cou = convertUpper.new(c)
		col = convertLower.new(c)
		converter = TextOperations.new(cou, col) # Client calls the invoker and gives the commands to execute
		converter.convToLow()
		converter.convToUp()
	end
end

Python Example

#This is the command interface.
class Command(object):
	def execute(self):
		pass

#A command to convert to lower case.
class convertLower(Command):
	def __init__(self, conv):
		self._convert = conv

	def execute(self):
		self._convert.toLower()

#A command to convert to upper case
class convertUpper(Command):
	def __init__(self, conv):
		self._convert = conv

	def execute(self):
		self._convert.toUpper()

#This is the reciever class.
class Convert(object):
	def __init__(self):
		pass
	def toUpper(self):
		Console.WriteLine("Turning all lower case elements to upper case")

	def toLower(self):
		Console.WriteLine("Converting all upper case elements to lower case")

# The invoker class
class TextOperations(object):
	def __init__(self, toup, todwn):
		self._convertUp = toup
		self._convertLow = todwn

	def convToUp(self):
		self._convertUp.execute()

	def convToLow(self):
		self._convertLow.execute()

# This is the client class.
class Menu(object):
	def Main(self, args):
		c = Convert()
		cou = convertUpper(c)
		col = convertLower(c)
		converter = TextOperations(cou, col) # Client calls the invoker and gives the commands to execute
		converter.convToLow()
		converter.convToUp()

Static vs Dynamic implementations

In case of the implementations ruby has better implementation of the command patterns than the languages like Java. Some of the advantages of Ruby over Java can be given as follows.

  • To implement the command pattern in static languages like Java, we have to define a common interface which has to be extended by all the other classes. In case of ruby we can use procs which makes it easier to implement.
  • Ruby implements something called mixins in order to implement the command pattern which is efficient compared to Java.
  • The lines of code for static language like Java is more than that taken by the dynamic language like Ruby. This is more because of the proc objects that are present in ruby which are concisely defined than that present in Java.
  • Since Ruby is a dynamic language, one can add the commands to the command array during runtime. Java is a static language and hence does not support this feature.

Conclusion

Hence it can be summarized that the dynamically typed languages keep the code straight and narrow as it does not need to depend on static type-checking. Also due to the flexibility of the dynamic languages, writing code is significantly easier.

References

[1] Freeman,E., and Robson,E.,and Bates,B.,and Sierra,K. 2004. Head First Design Patterns

[2] Wikipedia. (2010, October) Wikipedia - Command_Pattern. [Online]. http://en.wikipedia.org/wiki/Command_pattern

[3] Oodesign. (2005-2006)- command-pattern. [Online]. http://www.oodesign.com/command-pattern.html

[4] Erich,G., and Richard,H.,and Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software

[5] James,C., 1998. The Design Patterns-Java Companion. [Online]. http://www.patterndepot.com/put/8/command.pdf