.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> Architecture/Pattern >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Command Pattern explained using a Football game

Posted By:Jean Paul       Posted Date: September 29, 2010    Points: 5    Category: Architecture/Pattern    URL: http://www.dotnetspark.com  

A graphical demonstration of the famous Command pattern

Command Pattern

"Encapsulate a request as an object, thereby letting you parameterize clients with different

requests, queue or log requests, and support undoable operations."

Advantage of Command Pattern

We can support Undo operation using this pattern.


In a football simulation we can track of each moves done by the player.

This is done by converting each action into an object and queuing the object in a list.

Later we can undo the operation by traversing the list.


Code Explained

There is an ICommand interface having Do() and Undo() methods

There is a KickCommand class implementing the above methods


From the main form, whenever the user clicks the football image,

a new KickCommand object is created with the mouse X, Y positions and the football as the argument to the

constructor.  These information are required by the KickCommand class to perform Do() and Undo() operations.

After calling the Do() operation the football gets moved on the screen. 


Along with that the KickCommand instance is stored in a listbox for logging purpose.  On clicking the Undo button the

last command from the listbox is fetched and the Undo() operation is called.


Here the KickCommand encapsulates Do() and Undo() behavior.



 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend