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
Line 223: Line 223:
=== Ruby Example ===
=== Ruby Example ===


#This is the command interface.
#This is the command interface.
class Command
class Command
def execute()
def execute()
end
end
end
end
#A command to convert to lower case.
#A command to convert to lower case.
class convertLower < Command
class convertLower < Command
def initialize(conv)
def initialize(conv)
self.@convert = conv
self.@convert = conv
end
end
 
def execute()
def execute()
@convert.toLower()
@convert.toLower()
end
end
end
end
#A command to convert to upper case
#A command to convert to upper case
class convertUpper < Command
class convertUpper < Command
def initialize(conv)
def initialize(conv)
self.@convert = conv
self.@convert = conv
end
end
 
def execute()
def execute()
@convert.toUpper()
@convert.toUpper()
end
end
end
end
#This is the reciever class.
#This is the reciever class.
class Convert
class Convert
def initialize()
def initialize()
end
end
 
def toUpper()
def toUpper()
Console.WriteLine("Turning all lower case elements to upper case")
Console.WriteLine("Turning all lower case elements to upper case")
end
end
 
def toLower()
def toLower()
Console.WriteLine("Converting all upper case elements to lower case")
Console.WriteLine("Converting all upper case elements to lower case")
end
end
end
end
# The invoker class
# The invoker class
class Converter
class Converter
def initialize(toup, todwn)
def initialize(toup, todwn)
self.@convertUp = toup
self.@convertUp = toup
self.@convertLow = todwn
self.@convertLow = todwn
end
end
 
def convToUp()
def convToUp()
@convertUp.execute()
@convertUp.execute()
end
end
 
def convToLow()
def convToLow()
@convertLow.execute()
@convertLow.execute()
end
end
end
end
# This is the client class.
# This is the client class.
class Menu
class Menu
def Main(args)
def Main(args)
c = Convert.new()
c = Convert.new()
cou = convertUpper.new(c)
cou = convertUpper.new(c)
col = convertLower.new(c)
col = convertLower.new(c)
converter = Converter.new(cou, col) # Client calls the invoker and gives the commands to execute
converter = Converter.new(cou, col) # Client calls the invoker and gives the commands to execute
converter.convToLow()
converter.convToLow()
converter.convToUp()
converter.convToUp()
end
end
end
end


=== Python Example ===
=== Python Example ===

Revision as of 01:29, 16 October 2010

Command Pattern in static and dynamic languages

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.

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.

There are several benefits 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

<< to add diagram >>

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.

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: <<to add matter>>

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(Converter class). The converter 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 languages

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");
   }

   public final void toLower()
   {
       System.out.print("Converting all upper case elements to lower case");
   }
}

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

// 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);

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

C# code

//This is the command interface.

public interface Command
{
    void execute();
}

//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");
   }

   public final void toLower()
   {
       Console.WriteLine("Converting all upper case elements to lower case");
   }
}

// The invoker class
public class Converter
{
   private Command convertUp;
   private Command convertLow;
       private Converter(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);

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

Implementation in Dynamic Languages

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 Converter
	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 = Converter.new(cou, col) # Client calls the invoker and gives the commands to execute
		converter.convToLow()
		converter.convToUp()
	end
end

Python Example

  1. This is the command interface.

class Command(object): def execute(self): pass

  1. A command to convert to lower case.

class convertLower(Command): def __init__(self, conv): self._convert = conv

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

  1. A command to convert to upper case

class convertUpper(Command): def __init__(self, conv): self._convert = conv

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

  1. 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")

  1. The invoker class

class Converter(object): def __init__(self, toup, todwn): self._convertUp = toup self._convertLow = todwn

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

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

  1. This is the client class.

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

Static vs Dynamic implementations

<< to add matter >>