Talk:Command pattern
From Wikipedia, the free encyclopedia
Contents |
[edit] Python examples
I'm not sure if Python is the best language to use for Design Pattern examples. Perhaps the main examples should be in a common language like Visual Basic or Java and there should be separate pages for examples in less popular languages. I'm not a Java programmer so I can't make this change. I would be happy to provide Delphi or C# examples.
- I agree. In the real world, Python would use closures for those examples. Particularly, statements like "Generic code that operates on command objects can be powerful and flexible while providing a simple API.", in the context of Python, sound absurd: how are they simpler or more powerful than plain callable objects?
- (Arguably, the "Command Pattern" would begin to make sense in Python once you start using it to handle undo functionality and such, as described on the C2 wiki page. However, none of the examples in the article do that kind of thing.)
- Tangentially, this article could probably be much improved by a comparison with closures --Piet Delport 10:06, 1 June 2006 (UTC)
-
- You know... removing this example was a mistake. The C++ example is unhelpful. The Python example was working code.
- If Python is less popular than Java, a good fix would be to rewrite the example in Java. (Duh.)
- As for the idea that Python programmers would use closures for this... well, it strikes me as kinda weird. I doubt many would. Python isn't Scheme. In Python, using an object is more flexible. You can add a priority and change the threadpool to understand priority. You can modify the object after it's created. You can examine the fields. You can find ways to merge similar objects. You can serialize it. You can populate it one field at a time (as in a wizard). And so on.
- So while your objections may be sound, deleting the example was not the right action. I'm putting it back and I challenge you to improve upon it. --Jorend 13:50, 2 April 2007 (UTC)
-
-
- Here is an example written in java, but I'm not 100% sure it is good enough. I'm posting it here. Requires jre 1.5 or later, working example ( but meaningless )
- with Callable<E>
-
package org.designPatterns.command;
import java.util.concurrent.Callable;
/**
Contains 2 numbers and returns their sum on call()
/
public class SumCommand implements Callable<Integer> {
private int param1; // both have default value of 0
private int param2;
public SumCommand() {
/* do nothing in this case */
}
/**
Fully initializes the inner state of the object
@param param1
@param param1
/
public SumCommand(int param1, int param2){
this.param1 = param1;
this.param2 = param2;
}
/**
@return param1+param2
/
public Integer call() throws Exception {
/* big delay due to some operations */
Thread.sleep(1000);
return param1+param2;
}
/* getters and setters omited ...*/
/* ... */
}
-
-
- with Runnable
-
package org.designPatterns.command;
/**
holds an integer and increments it with a given value every time the run method is called
/
public class IncrementCommand implements Runnable {
private int value;
private int incrementer;
public IncrementCommand(int value, int incrementer){
this.value = value;
this.incrementer = incrementer;
}
/**
The operation of this Command affects an object contained in it, it DOES NOT return a value
/
public void run() {
/* big delay due to some operations */
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
this.value+=incrementer;
}
}
public int getValue() {
return value;
}
}
-
-
- Main method for demonstrating both of them:
-
package org.designPatterns.command;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;
/**
This class demonstrates usage of the command classes with and without thread pool
/
public class Main {
public static void main(String[] args) throws Exception {
/* simple example with callable, expected 9:*/
System.out.println( "new SumCommand(4,5).call() : " + new SumCommand(4,5).call() );
-
/* and setting up the command object */
IncrementCommand ac = new IncrementCommand( 5,1 );
ac.run();
System.out.println( "new IncrementCommand( 5,1 ); after run() :"+ac.getValue() );
/*with Thread pool:*/
IncrementCommand aggregator = new IncrementCommand(3,2);
SumCommand summator = new SumCommand(3,6);
Future<Integer> res1;
Future<IncrementCommand> res2;
ExecutorService executor = new ThreadPoolExecutor(2,6,100l,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>() );
/*one call*/
res1 = executor.submit(summator); /*Callable*/
res2 = executor.submit(aggregator, aggregator); /*Runnable*/
/* DO SOMETHING ELSE TIMETAKING HERE */
System.out.println("expects (3+6) = 9: "+res1.get());
System.out.println("expects ( 3+ 2) = 5 :"+res2.get().getValue());
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
}
P.S. No idea how to make it appear correctly formated :( Oubless 09:48, 19 September 2007 (UTC)
[edit] Link to P of EAA : UnitOfWorks
The link to martin's fowler website, which redirect to a description of the UnitOfWork pattern does not seems appropriate. Are you sure that UnitOfWork is connected to the command pattern ? If so, some text should explain the link... —The preceding unsigned comment was added by 195.68.44.148 (talk) 14:31, 26 April 2007 (UTC).
[edit] Programming Code
Since the subject is a topic in computer science, the general explanations should not use a specific programming languages (Java, Python, or whatever) but should be written in pseudocode. Actual code examples (in many languages, preferably) can be shown at the end of the article. Inquam 07:56, 7 November 2007 (UTC)
[edit] C++ example
I added a simple c++ example. Let me know what you think.--Michael miceli (talk) 22:32, 7 April 2008 (UTC)
- I realise this is entirely the subject of the argument above, but I'm not sure C++ is optimal for this; there's a lot of stuff that can be difficult to understand in it. Seems as though Python, which would perhaps lead to a terser example, isn't well-loved either. Would re-writing in Java (it's OO and as ubiquitous as they come) make sense? Scala would make this look great but it's not used by anyone :( --zootm (talk) 11:31, 5 June 2008 (UTC)