CSC/ECE 517 Fall 2010/ch4 4f ls

From Expertiza_Wiki
Revision as of 01:04, 22 October 2010 by Paullei (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Topic: The Command pattern in static and dynamic languages

Fundamentals

What is Command Pattern ?

In object-oriented programming, the command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time[1]. This information includes the method name, the object that owns the method and values for the method parameters. Client, invoker and receiver are always associated with the command pattern. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code. Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.

What are static and dynamic languages?

Dynamic programming language is used to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages, which are usually called static language for convenience, might perform during compilation. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them. Most dynamic languages are dynamically typed, but not all.

Actually, the notion of dynamic language is ambiguous sometime because it attempts to make distinctions between code and data as well as between compilation and runtime which are not universal. Virtual machines, just-in-time compilation, and the ability of many programming languages on some systems to directly modify machine code make the distinction abstract. In general, the assertion that a language is dynamic is more an assertion about the ease of use of dynamic features than it is a clear statement of the capabilities of the language. Particularly, the following are generally considered dynamic languages:

  • Ruby
  • Javascript
  • Perl
  • PHP
  • Smalltalk

Uses of Command Pattern

Command pattern encapsulates a request as an object, thereby letting you parametrize clients with different requests, queue or log requests, and support undoable operations. It is useful for implementing.

Structure

Terminology

We first introduce some terminology terms used to describe command pattern implementations.

  • Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user.
  • Command: It is an object that encapsulates a request to the receiver.
  • Execute: It may refer to running the code identified by the command object's execute method.
  • Receiver: The actual work to be done by the command.
  • ConcreteCommand: It invokes the corresponding operations on the receiver.
  • Invoker: It decides when the method should be called. It takes in the request and calls the receiver by passing the command to it and asks it to carry out the request.
  • Concretecommand:

Illustration

We will use the following graph to illustrate the structure of Command Pattern.

Examples