CSC/ECE 517 Fall 2007/wiki1b 7 as: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
m (Rev 3)
Line 19: Line 19:
Both Java and Ruby are object oriented programming languages in which command pattern can be implemented by turning the request itself into an object. This object can be stored and passed around like other objects. The Receiver has the knowledge required to carry out the request. In Java the implementation is through ''interfaces'' and in Ruby the implementation is through ''mixins''. The difference in implementation of command patterns in Java and Ruby are brought out using a simple example that undos the concatination operation on strings. Because of concepts such as meta-programming, modules, mixins many of the design patterns have much simpler, cleaner implementations in Ruby.  
Both Java and Ruby are object oriented programming languages in which command pattern can be implemented by turning the request itself into an object. This object can be stored and passed around like other objects. The Receiver has the knowledge required to carry out the request. In Java the implementation is through ''interfaces'' and in Ruby the implementation is through ''mixins''. The difference in implementation of command patterns in Java and Ruby are brought out using a simple example that undos the concatination operation on strings. Because of concepts such as meta-programming, modules, mixins many of the design patterns have much simpler, cleaner implementations in Ruby.  


To illustrate the concept of command pattern we have attempted to implement ''do-undo concept'' in both Java and Ruby as follows.  This concept is very useful in real life, not just in text editing but also for very complicated classes with appropriate definitions of do and undo.  We have have brought out the concept using string concatenation as an example.  Every time the object calls the concatenation function, we push the current state of the object into a stack and then allow it to be modified.  On the invocation of undo method, the stack is popped and the previous state is restored.
To illustrate the concept of command pattern we have attempted to implement ''do-undo concept'' in both Java and Ruby as follows.  This concept is very useful in real life, not just in text editing but also for very complicated classes in which the implementation needs to be decoupled from the invocation.  We have have brought out the concept of command patterns using string concatenation.  Every time the object calls the concatenation function, we push the current state of the object into a stack and then allow it to be modified.  On the invocation of undo method, the stack is popped and the previous state is restored.


====Implementation in Java====
====Implementation in Java====
Line 43: Line 43:
  */
  */
class NewString implements CommandPattern {
class NewString implements CommandPattern {
// Stores the states of the objects
private ArrayList<String> states = new ArrayList();
private ArrayList<String> states = new ArrayList();
private String s;
// The string on which the concatenation operation is done
        private String s;
public NewString (String args) {
public NewString (String args) {
s = args;
s = args;
}
}
// returns the previous state of the object
public ArrayList<String> getStates() {
public ArrayList<String> getStates() {
return states;
return states;
}
}
 
        // pushes the current state on to the ArrayList
public void setStates(ArrayList<String> states) {
public void setStates(ArrayList<String> states) {
this.states = states;
this.states = states;
Line 81: Line 82:
System.out.println("no command done to undo");
System.out.println("no command done to undo");
}
}
 
        // the getter and setter methods for the string
public String getS() {
public String getS() {
return s;
return s;
}
}
public void setS(String s) {
public void setS(String s) {
this.s = s;
this.s = s;
Line 94: Line 94:
NewString s = new NewString("sample ");
NewString s = new NewString("sample ");
System.out.println(s.getS());
System.out.println(s.getS());
s.concat("123");
s.concat("123");//concatenating the string
System.out.println(s.getS());
System.out.println(s.getS());
s.undoCommand();
s.undoCommand();//removes the last modification of the NewString object
System.out.println(s.getS());
System.out.println(s.getS());
s.undoCommand();
s.undoCommand();
Line 124: Line 124:
# note: there is no mention of type of object before pushing/popping in  
# note: there is no mention of type of object before pushing/popping in  
# 'states' (stack) variable
# 'states' (stack) variable
module CommandPattern
module CommandPattern
  //pops the previous state of the stack
   def undoCommand()     
   def undoCommand()     
     if(@states.length > 0)
     if(@states.length > 0)
Line 132: Line 133:
     end
     end
   end
   end
    
   //pushes the current state of the stack
   def doCommand(object)
   def doCommand(object)
    
    
Line 146: Line 147:
# here we can extend any class to whatever functionality we want, while
# here we can extend any class to whatever functionality we want, while
# java String class cannot be.
# java String class cannot be.
class NewString < String  
class NewString < String  
   include CommandPattern
   include CommandPattern
Line 191: Line 191:
*Wikipedia page, [http://en.wikipedia.org/wiki/Command_pattern]
*Wikipedia page, [http://en.wikipedia.org/wiki/Command_pattern]
*Javaworld, a pretty detailed discussion on pros and cons of using java and ruby for various purposes. [http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html]
*Javaworld, a pretty detailed discussion on pros and cons of using java and ruby for various purposes. [http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html]
*Example of Undo Implementation [http://c2.com/cgi/wiki?CommandPattern]
==Further Reading==
*Design Patterns: A good starting point [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)]
*How to implement Command Pattern in java [http://www.javaworld.com/javaworld/javatips/jw-javatip68.html]
*How to implement Command Pattern in java [http://www.javaworld.com/javaworld/javatips/jw-javatip68.html]
*Example of Undo Implementation [http://c2.com/cgi/wiki?CommandPattern]
*Advanced topics on Ruby[http://ruby.about.com/od/advancedruby/Advanced_Ruby.htm]
*A good explanation of Ruby functionalities [http://peter.michaux.ca/article/1174]
*An elementary tutorial on Design Pattern [http://www.csc.calpoly.edu/~dbutler/tutorials/winter96/patterns/]
*For further reading [http://ruby.about.com/od/advancedruby/Advanced_Ruby.htm]
*A Good Books. Design patterns in java [http://www.amazon.com/Design-Patterns-Java-Workbook-Software/dp/0201743973]


==Contributors==
==External Links==
*Ashwin Veeravalli [http://ashwin.gopalakrishnan.googlepages.com/]
*http://www.vincehuston.org/dp/command.html
*Satya N Viswanathan [http://vsn314.googlepages.com/test1]
*http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/
*http://www.patterndepot.com/put/8/JavaPatterns.htm
*http://www.stardeveloper.com/articles/display.html?article=2004022804&page=1
*http://www.mactech.com/articles/mactech/Vol.13/13.04/DesignPatternsTutorial/index.html

Revision as of 00:44, 9 October 2007

Command Patterns

Definition : Command Pattern is a Design Pattern that encapsulates invocation and allows code that causes actions to be initiated to be separated from code that performs those actions.

Why use Command Patterns?

A Command pattern is an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. (A sender is an object that invokes an operation, and a receiver is an object that receives the request to execute a certain operation. With decoupling, the sender has no knowledge of the Receiver's request.) The term request here refers to the command that is to be executed. The Command pattern also allows us to vary when and how a request is fulfilled. Therefore, a Command pattern provides us flexibility as well as extensibility.


Use of Command Patterns

  • Decouple the object that invokes the operation from the one that performs the action described earlier.
  • Assemble commands into a composite command.
  • Add new Commands, without having to change existing classes.


Implementation of Command Patterns in Java Vs Ruby

Both Java and Ruby are object oriented programming languages in which command pattern can be implemented by turning the request itself into an object. This object can be stored and passed around like other objects. The Receiver has the knowledge required to carry out the request. In Java the implementation is through interfaces and in Ruby the implementation is through mixins. The difference in implementation of command patterns in Java and Ruby are brought out using a simple example that undos the concatination operation on strings. Because of concepts such as meta-programming, modules, mixins many of the design patterns have much simpler, cleaner implementations in Ruby.

To illustrate the concept of command pattern we have attempted to implement do-undo concept in both Java and Ruby as follows. This concept is very useful in real life, not just in text editing but also for very complicated classes in which the implementation needs to be decoupled from the invocation. We have have brought out the concept of command patterns using string concatenation. Every time the object calls the concatenation function, we push the current state of the object into a stack and then allow it to be modified. On the invocation of undo method, the stack is popped and the previous state is restored.

Implementation in Java

The key to implementing command patterns in Java are interfaces, which declare the executing operations. In its simplest form, this interface includes an abstract execute operation. Each concrete Command class specifies a receiver-action pair by storing the Receiver as an instance variable. It provides different implementations of the execute() method to invoke the request.

Example Code

import java.util.ArrayList;

/*
 * Basic interface for do-undo command pattern. 
 */
interface CommandPattern {
	void doCommand();
	void undoCommand();
}

/*
 * this NewString implements CommandPattern, and note: this cannot extend String class,
 * because String-class in java is 'final'
 */
class NewString implements CommandPattern {
	// Stores the states of the objects
	private ArrayList<String> states = new ArrayList();
	// The string on which the concatenation operation is done
        private String s;
	
	public NewString (String args) {
		s = args;
	}
	// returns the previous state of the object
	public ArrayList<String> getStates() {
		return states;
	}
        // pushes the current state on to the ArrayList
	public void setStates(ArrayList<String> states) {
		this.states = states;
	}
	
	public void concat(String args) {
		doCommand();
		s = s.concat(args);
	}
	
        //this function is called when concat method is called. (the previous function)
        //we append current state to an ArrayList
	public void doCommand() {
		states.add(this.s);
	}
	
        // this method does the undo operation.
        // the storage of states is done in an ArrayList, and hence we implemented our own
        // way of 'popping' for the ArrayList
	public void undoCommand() {
		if(states.size() > 0) {
			s = states.get(states.size()-1);
			states.remove(states.size()-1);
		}
		else
			System.out.println("no command done to undo");
	}
        // the getter and setter methods for the string
	public String getS() {
		return s;
	}
	public void setS(String s) {
		this.s = s;
	}
	
	public static void main(String[] args) {
	
		NewString s = new NewString("sample ");
		System.out.println(s.getS());
		s.concat("123");//concatenating the string
		System.out.println(s.getS());
		s.undoCommand();//removes the last modification of the NewString object
		System.out.println(s.getS());
		s.undoCommand();
		System.out.println(s.getS());
	}
}

Sample Output:

$ sample
$ sample123
$ sample
$ no command done to undo

Implementation in Ruby

The key to implementing command patterns in Java are mixins, which define the executing operations. In its simplest form, this mixin include the implementation of the execute operation. Each Command class that specifies a receiver-action pair by storing the Receiver as an instance includes the mixin and calls the execute operation of the mixin.

Example Code

$ irb
# this is command pattern module which implements do undo operation 
# for any class/object.  
# note: there is no mention of type of object before pushing/popping in 
# 'states' (stack) variable
module CommandPattern
  //pops the previous state of the stack
  def undoCommand()    
    if(@states.length > 0)
        initialize(@states.pop())
    else
        puts "no command done to undo"
    end
  end
  //pushes the current state of the stack
  def doCommand(object)
  
    if(@states == nil)
      @states = []
    end
    
    @states.push(object)
    puts "docommand #{object}" 
  end
end

# here we can extend any class to whatever functionality we want, while
# java String class cannot be.
class NewString < String 
  include CommandPattern
  attr_accessor :states
  def initialize(arg)
    super(arg)
  end
  def concat(arg)
    doCommand(to_s())
    super(arg)
    
  end
end

Sample Output:

>a = NewString.new("sample")
>puts a
>>sample
>a.concat("123")
>puts a
>>sample123
>a.undoCommand()
>puts a
>>sample
>a.undoCommand()
>>no command done to undo

Advantages of Ruby over Java

The following explanation from the webpage [1] captures the advantages of Ruby implementation of command patterns over Java in a succinct and clear manner.

"Execute() methods in the Command design pattern must return Object rather than a specific type in pre-Java 5 code, resulting in ClassCastExceptions. Likewise, when signatures change between compile-time and runtime, runtime Errors ensue. In practice, whether in Java or Ruby, such errors rarely cause severe field bugs. A strong unit test suite—which you need anyway!—generally catches them in time.

Ruby's dynamic typing does not mean weak typing—Ruby always requires you to pass objects of the correct type. Java, in fact, enforces types more weakly than Ruby. For example, Java evaluates "4" + 2 as "42", coercing the integer to a string, while Ruby throws a TypeError, telling you it "can't convert Fixnum into String." Likewise, Java, sacrificing correctness for speed, can silently overflow an integer operation, producing weirdness such as Integer.MAX_VALUE + 1, which equals Integer.MIN_VALUE, while Ruby simply expands integers as needed."

Conclusion

In our Ruby implementation, the command pattern for undo is coded as a module, which can be included for any class or an object. While in Java, its been implemented as an interface forcing it be implemented by every class that needs this functionality. Also, implementing it as a separate module makes 'do' and 'undo' code to be written only once and any object/class can be extended easily. While in Java, each class that implements this interface must have a separate definition for the code. This has several disadvantages like increases code complexity, size and is generally confusing and tedious to trace or understand the code.

References

  • Wikipedia page, [2]
  • Javaworld, a pretty detailed discussion on pros and cons of using java and ruby for various purposes. [3]
  • Example of Undo Implementation [4]

Further Reading

  • Design Patterns: A good starting point [5]
  • How to implement Command Pattern in java [6]
  • Advanced topics on Ruby[7]
  • An elementary tutorial on Design Pattern [8]

External Links