Command Pattern

Command pattern encapsulates a request under an object as command, and passed to invoker object. It allows the parameterization of clients with different requests, queue or log requests. Normally, command pattern can be applied when we need to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.

Command patter decouples Invoker (invokes operation) from Receiver (know how to perform the operation). The following diagram demonstrates how it works.

Command

In the diagram, an Invoker treats a Command object as a black box. If it executes operation() method, it will call the command object execute() method that will call the receiver’s one action method.

Step 1 implement a receiver class

Step 2 define Command interface

Step 3 Create concrete command class

Step 4 create a Invoker class

Step 5 write a demo client

Output

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponEmail this to someoneShare on TumblrDigg this

One thought on “Command Pattern

  1. Pingback: Software Design Patterns

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">