Click here to Skip to main content
15,886,693 members
Articles / Web Development
Article

Command Pattern

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 5.9K   1  
Command Pattern"Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Command Pattern

"Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. "

A VB example of the Command Pattern

A lot of the new multimedia keyboards have extra buttons on them for opening up a web browser, an mp3 player etc. we can use the Command pattern to program them so that the keybaord need not know what application it is executing.

' This is the Invoker
Public Class MultiMediaKeyBoard

     
' This array holds commands for the buttons
    
' of our keyboard
    
Private _ButtonSlots As New ArrayList(2)     

     Public
Sub Main()
        
' Set up the commands to corresponding keyboard
        
' buttons.
        
Dim aWebBrowser As New WebBrowser
         _ButtonSlots(0) =
New WebBrowserCommand(aWebBrowser)

         Dim aWordProcessor As New WordProcessor
         _ButtonSlots(1) =
New WordProcessorCommand(aWordProcessor)

     
End Sub

      Public Sub MediaButtonPushed(ByVal buttonNumber As Integer)

          
' When a button is clicked on the keyboard its API can send us the
         
' buttons ID. We can then call the execute method of the class in the
         
' corresponding
         
CType(_ButtonSlots(buttonNumber), ICommand).execute()

     
End Sub

End
Class

Public Interface ICommand
  
Sub execute()
End Interface

' The receiver
Public Class WebBrowser

      Public Sub OpenWebBrowser()
         
' Code to open a web browser
     
End Sub
End Class

' The concrete command class
Public Class WebBrowserCommand
     
Implements ICommand

     
Private _WebBrowser As WebBrowser
   
     
Public Sub New(ByVal WebBrowser As WebBrowser)
            _WebBrowser = WebBrowser
      
End Sub      

      Public
Sub execute() Implements ICommand.execute
           _WebBrowser.OpenWebBrowser()
     
End Sub

End Class

' The receiver
Public Class WordProcessor

     
Public Sub OpenWordProcessor()
          
' Code to open a word processor
     
End Sub
End Class

' The concrete command class
Public Class WordProcessorCommand
     
Implements ICommand

      Private _WordProcessor As WordProcessor

     
Public Sub New(ByVal WordProcessor As WordProcessor)
           _WordProcessor = WordProcessor
     
End Sub

      Public Sub execute() Implements ICommand.execute
           _WordProcessor.OpenWordProcessor()
     
End Sub

End Class


 

This article was originally posted at http://wiki.asp.net/page.aspx/437/command-pattern

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --