<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Paullei</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Paullei"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Paullei"/>
	<updated>2026-09-12T06:02:39Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39640</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39640"/>
		<updated>2010-10-31T17:02:36Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
&lt;br /&gt;
The Command pattern solves this problem: A program needs to issue requests to objects.  The code that is doing the requesting doesn’t know what the receiver will be, or what operation will be requested.&lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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&lt;br /&gt;
[http://en.wikipedia.or/wiki/Dynamic_programming_language]. 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. &lt;br /&gt;
&lt;br /&gt;
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[http://en.wikipedia.org/wiki/Dynamic_programming_language]:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations[http://www.patterndepot.com/put/8/command.pdf].&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
In C++ and C#, they use the &amp;quot;pointer to methods&amp;quot; to implement command pattern. This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
Similar to C#, C++ uses &amp;quot;pointers to methods&amp;quot; to implement command patter.&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
 };&lt;br /&gt;
 class Person&lt;br /&gt;
 {&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
 class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln(''); &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN'); &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: '' &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.! &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.! &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! ! &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! ! &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method&lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff. &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown. &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ] &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
=Comparison: static vs dynamic=&lt;br /&gt;
Based on the above-mentioned examples, we present a short summary on comparison of command pattern in static language (such as Java) and dynamic language (such as ruby). &lt;br /&gt;
*Dynamic languages, such as ruby, can change their behavior conveniently at run time, such as reflection and metaprogramming. Therefore, the implementation of command pattern in dynamic languages is much easier and more convenient than that in static language.&lt;br /&gt;
*Command pattern is to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Although most actions of static language have to be performed at compile time, static languages have their own solutions to implement command pattern. For example, C and C# use &amp;quot;pointers to methods&amp;quot;. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39639</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39639"/>
		<updated>2010-10-31T16:55:01Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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&lt;br /&gt;
[http://en.wikipedia.or/wiki/Dynamic_programming_language]. 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. &lt;br /&gt;
&lt;br /&gt;
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[http://en.wikipedia.org/wiki/Dynamic_programming_language]:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations[http://www.patterndepot.com/put/8/command.pdf].&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
In C++ and C#, they use the &amp;quot;pointer to methods&amp;quot; to implement command pattern. This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
Similar to C#, C++ uses &amp;quot;pointers to methods&amp;quot; to implement command patter.&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
 };&lt;br /&gt;
 class Person&lt;br /&gt;
 {&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
 class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln(''); &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN'); &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: '' &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.! &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.! &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! ! &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! ! &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method&lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff. &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown. &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ] &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
=Comparison: static vs dynamic=&lt;br /&gt;
Based on the above-mentioned examples, we present a short summary on comparison of command pattern in static language (such as Java) and dynamic language (such as ruby). &lt;br /&gt;
*Dynamic languages, such as ruby, can change their behavior conveniently at run time, such as reflection and metaprogramming. Therefore, the implementation of command pattern in dynamic languages is much easier and more convenient than that in static language.&lt;br /&gt;
*Command pattern is to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Although most actions of static language have to be performed at compile time, static languages have their own solutions to implement command pattern. For example, C and C# use &amp;quot;pointers to methods&amp;quot;. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39638</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39638"/>
		<updated>2010-10-31T16:52:37Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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[http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
]. 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. &lt;br /&gt;
&lt;br /&gt;
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[http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
]:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations[http://www.patterndepot.com/put/8/command.pdf].&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
In C++ and C#, they use the &amp;quot;pointer to methods&amp;quot; to implement command pattern. This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
Similar to C#, C++ uses &amp;quot;pointers to methods&amp;quot; to implement command patter.&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
 };&lt;br /&gt;
 class Person&lt;br /&gt;
 {&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
 class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln(''); &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN'); &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: '' &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.! &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.! &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! ! &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! ! &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method&lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff. &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown. &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ] &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
=Comparison: static vs dynamic=&lt;br /&gt;
Based on the above-mentioned examples, we present a short summary on comparison of command pattern in static language (such as Java) and dynamic language (such as ruby). &lt;br /&gt;
*Dynamic languages, such as ruby, can change their behavior conveniently at run time, such as reflection and metaprogramming. Therefore, the implementation of command pattern in dynamic languages is much easier and more convenient than that in static language.&lt;br /&gt;
*Command pattern is to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Although most actions of static language have to be performed at compile time, static languages have their own solutions to implement command pattern. For example, C and C# use &amp;quot;pointers to methods&amp;quot;. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39637</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39637"/>
		<updated>2010-10-31T16:46:40Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations.&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
In C++ and C#, they use the &amp;quot;pointer to methods&amp;quot; to implement command pattern. This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
Similar to C#, C++ uses &amp;quot;pointers to methods&amp;quot; to implement command patter.&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
 };&lt;br /&gt;
 class Person&lt;br /&gt;
 {&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
 class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln(''); &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN'); &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: '' &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.! &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.! &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! ! &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! ! &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method&lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff. &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown. &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ] &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
=Comparison: static vs dynamic=&lt;br /&gt;
Based on the above-mentioned examples, we present a short summary on comparison of command pattern in static language (such as Java) and dynamic language (such as ruby). &lt;br /&gt;
*Dynamic languages, such as ruby, can change their behavior conveniently at run time, such as reflection and metaprogramming. Therefore, the implementation of command pattern in dynamic languages is much easier and more convenient than that in static language.&lt;br /&gt;
*Command pattern is to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Although most actions of static language have to be performed at compile time, static languages have their own solutions to implement command pattern. For example, C and C# use &amp;quot;pointers to methods&amp;quot;. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39636</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39636"/>
		<updated>2010-10-31T16:38:41Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations.&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
{&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
 class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN');&lt;br /&gt;
 &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
 &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
 &lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.!&lt;br /&gt;
 &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.!&lt;br /&gt;
 &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! !&lt;br /&gt;
 &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
 &lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! !&lt;br /&gt;
 &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff.&lt;br /&gt;
 &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown.&lt;br /&gt;
 &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ].&lt;br /&gt;
 &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
=Comparison: static vs dynamic=&lt;br /&gt;
Based on the above-mentioned examples, we present a short summary on comparison of command pattern in static language (such as Java) and dynamic language (such as ruby). &lt;br /&gt;
*Dynamic languages, such as ruby, can change their behavior conveniently at run time, such as reflection and metaprogramming. Therefore, the implementation of command pattern in dynamic languages is much easier and more convenient than that in static language.&lt;br /&gt;
*Command pattern is to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Although most actions of static language have to be performed at compile time, static languages have their own solutions to implement command pattern. For example, C and C# use &amp;quot;pointers to methods&amp;quot;. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39634</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39634"/>
		<updated>2010-10-31T16:25:14Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations.&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
{&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
 class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN');&lt;br /&gt;
 &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
 &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
 &lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.!&lt;br /&gt;
 &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.!&lt;br /&gt;
 &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! !&lt;br /&gt;
 &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
 &lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! !&lt;br /&gt;
 &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff.&lt;br /&gt;
 &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown.&lt;br /&gt;
 &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ].&lt;br /&gt;
 &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
=Comparison: static vs dynamic=&lt;br /&gt;
Based on the above-mentioned examples, we present a short summary on comparison of command pattern in static language (such as Java) and dynamic language (such as ruby). &lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Command.png&amp;diff=39348</id>
		<title>File:Command.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Command.png&amp;diff=39348"/>
		<updated>2010-10-22T02:29:38Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39347</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39347"/>
		<updated>2010-10-22T02:29:14Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations.&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
{&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
 class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN');&lt;br /&gt;
 &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
 &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
 &lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.!&lt;br /&gt;
 &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.!&lt;br /&gt;
 &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! !&lt;br /&gt;
 &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
 &lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! !&lt;br /&gt;
 &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff.&lt;br /&gt;
 &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown.&lt;br /&gt;
 &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ].&lt;br /&gt;
 &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39346</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39346"/>
		<updated>2010-10-22T02:27:36Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations.&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
  using System;&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
 {&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class MainApp&lt;br /&gt;
  {&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    static void Main()&lt;br /&gt;
    {&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
      Invoker invoker = new Invoker()&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
      // Wait for user&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  abstract class Command&lt;br /&gt;
  {&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
    {&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
    }&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
  {&lt;br /&gt;
    // Constructor&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
      base(receiver)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
    {&lt;br /&gt;
     receiver.Action();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Receiver&lt;br /&gt;
  {&lt;br /&gt;
    public void Action()&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  } &lt;br /&gt;
 /// &amp;lt;summary&amp;gt;&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  class Invoker&lt;br /&gt;
  {&lt;br /&gt;
    private Command _command&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
    {&lt;br /&gt;
      this._command = command;&lt;br /&gt;
    }&lt;br /&gt;
     public void ExecuteCommand()&lt;br /&gt;
    {&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
{&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN');&lt;br /&gt;
 &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
 &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
 &lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.!&lt;br /&gt;
 &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.!&lt;br /&gt;
 &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! !&lt;br /&gt;
 &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
 &lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! !&lt;br /&gt;
 &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff.&lt;br /&gt;
 &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown.&lt;br /&gt;
 &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ].&lt;br /&gt;
 &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39345</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39345"/>
		<updated>2010-10-22T02:23:46Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations.&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: declares an interface for executing an operation;&lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: extends the Command interface, implementing the Execute method by invoking the corresponding operations on Receiver. It defines a link between the Receiver and the action.&lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
== Why Command Pattern?==&lt;br /&gt;
“An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes”. This is the definition of a macro, one that should be familiar to any computer user. From this idea the Command design pattern was given birth.&lt;br /&gt;
The Macro represents, at some extent, a command that is built from the reunion of a set of other commands, in a given order. Just as a macro, the Command design pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.&lt;br /&gt;
== Advantages and Disadvantages of Using Command Pattern ==&lt;br /&gt;
Now that we have understood how the pattern works, it's time to take a look at its advantages and disadvantages.&lt;br /&gt;
=== The intelligence of a command ===&lt;br /&gt;
* The command is just a link between the receiver and the actions that carry out the request.&lt;br /&gt;
* The command implements everything itself, without sending anything to the receiver.&lt;br /&gt;
We must always keep in mind the fact that the receiver is the one who knows how to perform the operations needed, the purpose of the command being to help the client to delegate its request quickly and to make sure the command ends up where it should.&lt;br /&gt;
=== Advantages of Command Pattern ===&lt;br /&gt;
The main advantage of the command design pattern is that it decouples the object that invokes the operation from the one that know how to perform it.&lt;br /&gt;
* It provides encapsulation of application logic so that it can be executed at a different point of time.&lt;br /&gt;
&lt;br /&gt;
* It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.&lt;br /&gt;
&lt;br /&gt;
* The separation between application logic and context allows to easier exchange the application logic.&lt;br /&gt;
=== Disadvantages of Command Pattern ===&lt;br /&gt;
The main benefits of the Command Pattern are discussed above. The major disadvantage of the pattern is that it results in lots of little Command classes that can clutter up a design. However, the routing information that Command objects encapsulate has to go somewhere. If this information is not contained in Command objects, then it will have to go into the Controller. The resulting bloat may necessitate partitioning the Controller into a subsystem, and it will certainly make the Controller harder to understand and maintain.&lt;br /&gt;
&lt;br /&gt;
= Command pattern in static and dynamic languages =&lt;br /&gt;
In this section, we demonstrate the implementation of command pattern in both dynamic and static languages by some examples. &lt;br /&gt;
== Command pattern in static languages ==&lt;br /&gt;
We consider in this article some typical static languages C#, java and C++.&lt;br /&gt;
===C#===&lt;br /&gt;
This structural code demonstrates the Command pattern which stores requests as objects allowing clients to execute or playback the requests. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 // Command pattern -- Structural example&lt;br /&gt;
&lt;br /&gt;
  using System;&lt;br /&gt;
&lt;br /&gt;
  namespace DoFactory.GangOfFour.Command.Structural&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// MainApp startup class for Structural&lt;br /&gt;
&lt;br /&gt;
  /// Command Design Pattern.&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  class MainApp&lt;br /&gt;
&lt;br /&gt;
  {&lt;br /&gt;
&lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    /// Entry point into console application.&lt;br /&gt;
&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    static void Main()&lt;br /&gt;
&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
      // Create receiver, command, and invoker&lt;br /&gt;
&lt;br /&gt;
      Receiver receiver = new Receiver();&lt;br /&gt;
&lt;br /&gt;
      Command command = new ConcreteCommand(receiver);&lt;br /&gt;
&lt;br /&gt;
      Invoker invoker = new Invoker();&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
      // Set and execute command&lt;br /&gt;
&lt;br /&gt;
      invoker.SetCommand(command);&lt;br /&gt;
&lt;br /&gt;
      invoker.ExecuteCommand();&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
      // Wait for user&lt;br /&gt;
&lt;br /&gt;
      Console.ReadKey();&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// The 'Command' abstract class&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  abstract class Command&lt;br /&gt;
&lt;br /&gt;
  {&lt;br /&gt;
&lt;br /&gt;
    protected Receiver receiver;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    // Constructor&lt;br /&gt;
&lt;br /&gt;
    public Command(Receiver receiver)&lt;br /&gt;
&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
      this.receiver = receiver;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    public abstract void Execute();&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// The 'ConcreteCommand' class&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  class ConcreteCommand : Command&lt;br /&gt;
&lt;br /&gt;
  {&lt;br /&gt;
&lt;br /&gt;
    // Constructor&lt;br /&gt;
&lt;br /&gt;
    public ConcreteCommand(Receiver receiver) :&lt;br /&gt;
&lt;br /&gt;
      base(receiver)&lt;br /&gt;
&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    public override void Execute()&lt;br /&gt;
&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
      receiver.Action();&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// The 'Receiver' class&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  class Receiver&lt;br /&gt;
&lt;br /&gt;
  {&lt;br /&gt;
&lt;br /&gt;
    public void Action()&lt;br /&gt;
&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
      Console.WriteLine(&amp;quot;Called Receiver.Action()&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  /// The 'Invoker' class&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  class Invoker&lt;br /&gt;
&lt;br /&gt;
  {&lt;br /&gt;
&lt;br /&gt;
    private Command _command;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    public void SetCommand(Command command)&lt;br /&gt;
&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
      this._command = command;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    public void ExecuteCommand()&lt;br /&gt;
&lt;br /&gt;
    {&lt;br /&gt;
&lt;br /&gt;
      _command.Execute();&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.” The Command design pattern suggests encapsulating (“wrapping”) in an object all (or some) of the following: an object, a method name, and some arguments. Java does not support “pointers to methods”, but its reflection capability will do nicely. The “command” is a black box to the “client”. All the client does is call “execute()” on the opaque object. &lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
  public class CommandReflect {&lt;br /&gt;
   private int state;&lt;br /&gt;
   public CommandReflect( int in ) {&lt;br /&gt;
      state = in;&lt;br /&gt;
   }&lt;br /&gt;
   public int addOne( Integer one ) {&lt;br /&gt;
      return state + one.intValue();&lt;br /&gt;
   }&lt;br /&gt;
   public int addTwo( Integer one, Integer two ) {&lt;br /&gt;
      return state + one.intValue() + two.intValue();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   static public class Command {&lt;br /&gt;
      private Object   receiver;               // the &amp;quot;encapsulated&amp;quot; object&lt;br /&gt;
      private Method   action;                 // the &amp;quot;pre-registered&amp;quot; request&lt;br /&gt;
      private Object[] args;                   // the &amp;quot;pre-registered&amp;quot; arg list&lt;br /&gt;
      public Command( Object obj, String methodName, Object[] arguments ) {&lt;br /&gt;
         receiver = obj;&lt;br /&gt;
         args = arguments;&lt;br /&gt;
         Class cls = obj.getClass();           // get the object's &amp;quot;Class&amp;quot;&lt;br /&gt;
         Class[] argTypes = new Class[args.length];&lt;br /&gt;
         for (int i=0; i &amp;lt; args.length; i++)   // get the &amp;quot;Class&amp;quot; for each&lt;br /&gt;
            argTypes[i] = args[i].getClass();  //    supplied argument&lt;br /&gt;
         // get the &amp;quot;Method&amp;quot; data structure with the correct name and signature&lt;br /&gt;
         try {      action = cls.getMethod( methodName, argTypes );      }&lt;br /&gt;
         catch( NoSuchMethodException e ) { System.out.println( e ); }&lt;br /&gt;
      }&lt;br /&gt;
      public Object execute() {&lt;br /&gt;
         // in C++, you do something like --- return receiver-&amp;gt;action( args ); &lt;br /&gt;
         try {     return action.invoke( receiver, args );     }&lt;br /&gt;
         catch( IllegalAccessException e    ) { System.out.println( e ); }&lt;br /&gt;
         catch( InvocationTargetException e ) { System.out.println( e ); }&lt;br /&gt;
         return null;&lt;br /&gt;
   }  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
      CommandReflect[] objs = { new CommandReflect(1), new CommandReflect(2) };&lt;br /&gt;
      System.out.print( &amp;quot;Normal call results: &amp;quot; );&lt;br /&gt;
      System.out.print( objs[0].addOne( new Integer(3) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.print( objs[1].addTwo( new Integer(4),&lt;br /&gt;
                                        new Integer(5) ) + &amp;quot; &amp;quot; );&lt;br /&gt;
      Command[] cmds = {&lt;br /&gt;
         new Command( objs[0], &amp;quot;addOne&amp;quot;, new Integer[] { new Integer(3) } ),&lt;br /&gt;
         new Command( objs[1], &amp;quot;addTwo&amp;quot;, new Integer[] { new Integer(4),&lt;br /&gt;
                                                         new Integer(5) } ) };&lt;br /&gt;
      System.out.print( &amp;quot;\nReflection results:  &amp;quot; );&lt;br /&gt;
      for (int i=0; i &amp;lt; cmds.length; i++)&lt;br /&gt;
          System.out.print( cmds[i].execute() + &amp;quot; &amp;quot; );&lt;br /&gt;
      System.out.println();&lt;br /&gt;
 }  }&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    #include &amp;lt;iostream&amp;gt;  #include &amp;lt;string&amp;gt;  using namespace std;&lt;br /&gt;
    class Person;&lt;br /&gt;
&lt;br /&gt;
    class Command&lt;br /&gt;
  {&lt;br /&gt;
    // 1. Create a class that encapsulates an object and a member function&lt;br /&gt;
    // a pointer to a member function (the attribute's name is &amp;quot;method&amp;quot;)&lt;br /&gt;
    Person *object; //    &lt;br /&gt;
    void(Person:: *method)();&lt;br /&gt;
  public:&lt;br /&gt;
    Command(Person *obj = 0, void(Person:: *meth)() = 0)&lt;br /&gt;
    {&lt;br /&gt;
        object = obj; // the argument's name is &amp;quot;meth&amp;quot;&lt;br /&gt;
        method = meth;&lt;br /&gt;
    }&lt;br /&gt;
    void execute()&lt;br /&gt;
    {&lt;br /&gt;
        (object-&amp;gt; *method)(); // invoke the method on the object&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
{&lt;br /&gt;
    string name;&lt;br /&gt;
&lt;br /&gt;
    // cmd is a &amp;quot;black box&amp;quot;, it is a method invocation&lt;br /&gt;
    // promoted to &amp;quot;full object status&amp;quot;&lt;br /&gt;
    Command cmd; &lt;br /&gt;
  public:&lt;br /&gt;
    Person(string n, Command c): cmd(c)&lt;br /&gt;
    {&lt;br /&gt;
        name = n;&lt;br /&gt;
    }&lt;br /&gt;
    void talk()&lt;br /&gt;
    {&lt;br /&gt;
        // &amp;quot;this&amp;quot; is the sender, cmd has the receiver&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is talking&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute(); // ask the &amp;quot;black box&amp;quot; to callback the receiver&lt;br /&gt;
    }&lt;br /&gt;
    void passOn()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is passing on&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        &lt;br /&gt;
        // 4. When the sender is ready to callback to the receiver,&lt;br /&gt;
        // it calls execute()&lt;br /&gt;
        cmd.execute(); &lt;br /&gt;
    }&lt;br /&gt;
    void gossip()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is gossiping&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        cmd.execute();&lt;br /&gt;
    }&lt;br /&gt;
    void listen()&lt;br /&gt;
    {&lt;br /&gt;
        cout &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &amp;quot; is listening&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
  // Fred will &amp;quot;execute&amp;quot; Barney which will result in a call to passOn()&lt;br /&gt;
  // Barney will &amp;quot;execute&amp;quot; Betty which will result in a call to gossip()&lt;br /&gt;
  // Betty will &amp;quot;execute&amp;quot; Wilma which will result in a call to listen()&lt;br /&gt;
  Person wilma(&amp;quot;Wilma&amp;quot;, Command());&lt;br /&gt;
  // 2. Instantiate an object for each &amp;quot;callback&amp;quot;&lt;br /&gt;
  // 3. Pass each object to its future &amp;quot;sender&amp;quot;&lt;br /&gt;
  Person betty(&amp;quot;Betty&amp;quot;, Command(&amp;amp;wilma, &amp;amp;Person::listen));&lt;br /&gt;
  Person barney(&amp;quot;Barney&amp;quot;, Command(&amp;amp;betty, &amp;amp;Person::gossip));&lt;br /&gt;
  Person fred(&amp;quot;Fred&amp;quot;, Command(&amp;amp;barney, &amp;amp;Person::passOn));&lt;br /&gt;
  fred.talk();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Command pattern in dynamic languages == &lt;br /&gt;
In terms of dynamic languages, we consider PHP and Smalltalk.&lt;br /&gt;
&lt;br /&gt;
=== PHP ===&lt;br /&gt;
In this example, a BookStarsOnCommand object is instantiated with an instance of the BookComandee class. The BookStarsOnCommand object will call that BookComandee object’s bookStarsOn() function when it’s execute() function is called.&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
class BookCommandee {&lt;br /&gt;
    private $author;&lt;br /&gt;
    private $title;&lt;br /&gt;
    function __construct($title_in, $author_in) {&lt;br /&gt;
        $this-&amp;gt;setAuthor($author_in);&lt;br /&gt;
        $this-&amp;gt;setTitle($title_in);&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthor() {&lt;br /&gt;
        return $this-&amp;gt;author;&lt;br /&gt;
    }&lt;br /&gt;
    function setAuthor($author_in) {&lt;br /&gt;
        $this-&amp;gt;author = $author_in;&lt;br /&gt;
    }&lt;br /&gt;
    function getTitle() {&lt;br /&gt;
        return $this-&amp;gt;title;&lt;br /&gt;
    }&lt;br /&gt;
    function setTitle($title_in) {&lt;br /&gt;
        $this-&amp;gt;title = $title_in;&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOn() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace(' ','*',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace(' ','*',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function setStarsOff() {&lt;br /&gt;
        $this-&amp;gt;setAuthor(Str_replace('*',' ',$this-&amp;gt;getAuthor()));&lt;br /&gt;
        $this-&amp;gt;setTitle(Str_replace('*',' ',$this-&amp;gt;getTitle()));&lt;br /&gt;
    }&lt;br /&gt;
    function getAuthorAndTitle() {&lt;br /&gt;
        return $this-&amp;gt;getTitle().' by '.$this-&amp;gt;getAuthor();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
abstract class BookCommand {&lt;br /&gt;
    protected $bookCommandee;&lt;br /&gt;
    function __construct($bookCommandee_in) {&lt;br /&gt;
        $this-&amp;gt;bookCommandee = $bookCommandee_in;&lt;br /&gt;
    }&lt;br /&gt;
    abstract function execute();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class BookStarsOnCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOn();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class BookStarsOffCommand extends BookCommand {&lt;br /&gt;
    function execute() {&lt;br /&gt;
        $this-&amp;gt;bookCommandee-&amp;gt;setStarsOff();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
  writeln('BEGIN TESTING COMMAND PATTERN');&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');&lt;br /&gt;
  writeln('book after creation: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOn = new BookStarsOnCommand($book);&lt;br /&gt;
  callCommand($starsOn);&lt;br /&gt;
  writeln('book after stars on: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
 &lt;br /&gt;
  $starsOff = new BookStarsOffCommand($book);&lt;br /&gt;
  callCommand($starsOff);&lt;br /&gt;
  writeln('book after stars off: ');&lt;br /&gt;
  writeln($book-&amp;gt;getAuthorAndTitle());&lt;br /&gt;
  writeln('');&lt;br /&gt;
&lt;br /&gt;
  writeln('END TESTING COMMAND PATTERN');&lt;br /&gt;
 &lt;br /&gt;
  // the callCommand function demonstrates that a specified&lt;br /&gt;
  // function in BookCommandee can be executed with only &lt;br /&gt;
  // an instance of BookCommand.&lt;br /&gt;
  function callCommand(BookCommand $bookCommand_in) {&lt;br /&gt;
    $bookCommand_in-&amp;gt;execute();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  function writeln($line_in) {&lt;br /&gt;
    echo $line_in.&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
 ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Smalltalk ==&lt;br /&gt;
In this example we configure the Switch with 2 commands: to turn the light on and to turn the light off.&lt;br /&gt;
&lt;br /&gt;
  Object subclass: #Switch&lt;br /&gt;
  instanceVariableNames: &lt;br /&gt;
    ' flipUpCommand flipDownCommand '&lt;br /&gt;
  classVariableNames: ''&lt;br /&gt;
  poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #Light&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  Object subclass: #PressSwitch&lt;br /&gt;
    instanceVariableNames: ''&lt;br /&gt;
    classVariableNames: ''&lt;br /&gt;
    poolDictionaries: ''&lt;br /&gt;
 &lt;br /&gt;
  !Switch class methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
 &lt;br /&gt;
	^self new upMessage: flipUpMessage downMessge: flipDownMessage; yourself.! !&lt;br /&gt;
 &lt;br /&gt;
  !Switch methods !&lt;br /&gt;
  upMessage: flipUpMessage downMessge: flipDownMessage&lt;br /&gt;
	flipUpCommand := flipUpMessage.&lt;br /&gt;
	flipDownCommand := flipDownMessage.!&lt;br /&gt;
 &lt;br /&gt;
  flipDown	&lt;br /&gt;
	flipDownCommand perform.!&lt;br /&gt;
 &lt;br /&gt;
  flipUp&lt;br /&gt;
	flipUpCommand perform.! !&lt;br /&gt;
 &lt;br /&gt;
  !Light methods !&lt;br /&gt;
  turnOff&lt;br /&gt;
	Transcript show: 'The light is off'; cr.!&lt;br /&gt;
 &lt;br /&gt;
  turnOn&lt;br /&gt;
	Transcript show: 'The light is on'; cr.! !&lt;br /&gt;
 &lt;br /&gt;
  !PressSwitch class methods !&lt;br /&gt;
  switch: state&lt;br /&gt;
	&amp;quot; This is the test method &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
	| lamp switchUp switchDown switch |&lt;br /&gt;
	lamp := Light new.&lt;br /&gt;
	switchUp := Message receiver: lamp selector: #turnOn.&lt;br /&gt;
	switchDown := Message receiver: lamp selector: #turnOff.&lt;br /&gt;
 &lt;br /&gt;
	switch := Switch upMessage: switchUp downMessge: switchDown.&lt;br /&gt;
 &lt;br /&gt;
	state = #on ifTrue: [ ^switch flipUp ].&lt;br /&gt;
	state = #off ifTrue: [ ^switch flipDown ].&lt;br /&gt;
 &lt;br /&gt;
	Transcript show: 'Argument #on or #off is required.'.&lt;br /&gt;
&lt;br /&gt;
= Summary =&lt;br /&gt;
In this article we took a quick look at the Command Pattern in static and dynamic languages. Command pattern is a great pattern for disconnecting the command originator and the system receiving the commands. This should be one of the first patterns you consider when thinking about creating a distributed type of system. &lt;br /&gt;
&lt;br /&gt;
Furthermore, the comparison between command pattern in static languages and dynamic languages is actually the comparison between the static languages and dynamic languages. That is, in dynamic language, we can implement command pattern at runtime. &lt;br /&gt;
= References =&lt;br /&gt;
[[#References|[1]]] Wikipedia - Command_Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
[[#References|[2]]] Command Design Pattern. [Online]. &lt;br /&gt;
http://sourcemaking.com/design_patterns/command&lt;br /&gt;
&lt;br /&gt;
[[#References|[3]]] Wikipedia - Dynamic programming language. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Dynamic_programming_language&lt;br /&gt;
&lt;br /&gt;
[[#References|[4]]] Wikipedia - Design Pattern. [Online]. &lt;br /&gt;
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)&lt;br /&gt;
&lt;br /&gt;
[[#References|[5]]] http://www.patterndepot.com/put/8/command.pdf&lt;br /&gt;
&lt;br /&gt;
[[#References|[6]]] Erich,G., Richard,H., Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software&lt;br /&gt;
&lt;br /&gt;
[[#References|[7]]] Freeman,E., Robson,E., Bates,B.,and Sierra,K. 2004. Head First Design Patterns&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39344</id>
		<title>CSC/ECE 517 Fall 2010/ch4 4f ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch4_4f_ls&amp;diff=39344"/>
		<updated>2010-10-22T01:04:10Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Topic: The Command pattern in static and dynamic languages&amp;lt;/p&amp;gt;&lt;br /&gt;
= Fundamentals =&lt;br /&gt;
== What is Command Pattern ?==&lt;br /&gt;
In object-oriented programming, the command pattern is a [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] in which an object is used to represent and encapsulate all the information needed to call a method at a later time[http://en.wikipedia.org/wiki/Command_pattern]. &lt;br /&gt;
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.&lt;br /&gt;
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.  &lt;br /&gt;
== What are static and dynamic languages? ==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
* Ruby&lt;br /&gt;
* Javascript&lt;br /&gt;
* Perl&lt;br /&gt;
* PHP&lt;br /&gt;
* Smalltalk&lt;br /&gt;
= Uses of Command Pattern =&lt;br /&gt;
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.&lt;br /&gt;
== Structure ==&lt;br /&gt;
===Terminology ===&lt;br /&gt;
We first introduce some terminology terms used to describe command pattern implementations.&lt;br /&gt;
* Client: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. &lt;br /&gt;
* Command: It is an object that encapsulates a request to the receiver. &lt;br /&gt;
* Execute: It may refer to running the code identified by the command object's execute method.&lt;br /&gt;
* Receiver: The actual work to be done by the command.&lt;br /&gt;
* ConcreteCommand: It invokes the corresponding operations on the receiver. &lt;br /&gt;
* 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. &lt;br /&gt;
* Concretecommand: &lt;br /&gt;
=== Illustration ===&lt;br /&gt;
We will use the following graph to illustrate the structure of Command Pattern.&lt;br /&gt;
[[Image:Command.png]]&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37962</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37962"/>
		<updated>2010-10-14T02:43:28Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
=== What is The Type System? ===&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
=== Classification of Type Checking ===&lt;br /&gt;
The process of verifying and enforcing the constraints of types – type checking – may occur either at compile-time (a static check) or run-time (a dynamic check). In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
===Mixing Static and Dynamic Typing ===&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Fundamentals ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Static Typing and Dynamic Typing versus Strong Typing and Weak Typing ===&lt;br /&gt;
Static and dynamic typing, and strong and weak typing, are two totally different concepts, which, unfortunately, are very often confused. It is erroneous to say that a language that is static or dynamic typed cannot be strong or weak typed. Static and dynamic typing, and strong and weak typing, are different forms of classification of programming languages, and one of each class necessarily characterizes a given language. It is thus imperative to discuss strong and weak typing vis-a-vis static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
Programming languages that exhibit &amp;quot;strong typing&amp;quot; are &amp;quot;strong typed,&amp;quot; and those that exhibit &amp;quot;weak typing&amp;quot; are &amp;quot;weak typed&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]].&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
=== Is Dynamic Typing Good? ===&lt;br /&gt;
In a dynamic typed language, you don't have to initialize variables, which is a big bonus for many developers. Programmers like the fact that you can use a variable at will when required (without having to initialize it). Dynamic typing is characteristic of many of the scripting languages: Perl, PHP, Python, etc. Dynamic typing, in fact, does save you from writing a few &amp;quot;extra&amp;quot; lines of code, which, in turn, means less time spent writing code.&lt;br /&gt;
&lt;br /&gt;
The very characteristic of dynamic typed languages that appeals to many developers is also a pitfall, and a major one at that. Consider the following simple example:&lt;br /&gt;
&lt;br /&gt;
/* Python code */&lt;br /&gt;
my_variable = 10&lt;br /&gt;
while my_variable &amp;gt; 0:&lt;br /&gt;
       i = foo(my_variable)&lt;br /&gt;
       if i &amp;lt; 100:&lt;br /&gt;
               my_variable++&lt;br /&gt;
       else&lt;br /&gt;
               my_varaible = (my_variable + i) / 10 // spelling error intentional&lt;br /&gt;
&lt;br /&gt;
As you can see in the above code, my_varaible is a spelling mistake that the programmer could have very well made. The problem here is that, since Python is dynamically typed, it will not return an error, but instead will create a new variable called my_varaible. So, now we have two variables: my_variable and my_varaible. This obviously is a serious problem; some would suggest that forced variable declaration is an important requirement in any programming language.&lt;br /&gt;
&lt;br /&gt;
=== Static Typed Behavior in Dynamic Typed Languages ===&lt;br /&gt;
Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. Consider the following Perl example:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
      $sum = 10;&lt;br /&gt;
      print $sum;&lt;br /&gt;
&lt;br /&gt;
The above code will run without any problem, and will print 10 to the console. Note that here, we have not initialized the variable sum; this exemplifies the dynamic typing characteristic of Perl. To enforce variable declaration, we make use of the strict pragma as follows:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
&lt;br /&gt;
     use strict;&lt;br /&gt;
     $sum = 10;&lt;br /&gt;
     print $sum;&lt;br /&gt;
&lt;br /&gt;
The above code fragment will return the following error when you try to run it:&lt;br /&gt;
&lt;br /&gt;
Global symbol &amp;quot;$num&amp;quot; requires explicit package name at perl.pl line 2.&lt;br /&gt;
Execution of perl.pl aborted due to compilation errors.&lt;br /&gt;
&lt;br /&gt;
To rectify the above error, we are forced to declare the variable num as follows:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
&lt;br /&gt;
   use strict;&lt;br /&gt;
   my $num; // forced declaration&lt;br /&gt;
   $sum = 10;&lt;br /&gt;
   print $sum;&lt;br /&gt;
&lt;br /&gt;
The above codes are specific to Perl; not all programming languages have a way to enforce variable declaration: Python, for example doesn't have a way to enforce variable declaration. However, there is a tool, called &amp;quot;pychecker&amp;quot; (available here), that can be used to &amp;quot;detect&amp;quot; stray variables; this is, of course, far from a desirable solution.&lt;br /&gt;
&lt;br /&gt;
== Polymorphism and types ==&lt;br /&gt;
The term &amp;quot;polymorphism&amp;quot; refers to the ability of code to act on values of multiple types, or to the ability of different instances of the same data-structure to contain elements of different types. Type systems that allow polymorphism generally do so in order to improve the potential for code re-use: in a language with polymorphism, programmers need only implement a data structure such as a list or an associative array once, rather than once for each type of element with which they plan to use it. For this reason computer scientists sometimes call the use of certain forms of polymorphism generic programming. The type-theoretic foundations of polymorphism are closely related to those of abstraction, modularity and (in some cases) subtyping.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing ===&lt;br /&gt;
In &amp;quot;duck typing&amp;quot;, a statement calling a method m on an object does not rely on the declared type of the object; only that the object, of whatever type, must implement the method called. One way of looking at this is that in duck typing systems the type of an object is intrinsic to the object and is determined by what methods it implements, and hence that a duck typing system is by definition type-safe since one can only invoke operations an object actually implements. Another way of looking at this is that the object is a member of several types, including a type that describes the fact that it &amp;quot;has a method m.&amp;quot; Type checking however occurs only on demand at runtime, every time the method m needs to be executed, not at compile-time or load-time.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from structural typing in that, if the part (of the whole module structure) needed for a given local computation is present at runtime, the duck type system is satisfied in its type identity analysis. On the other hand, a structural type system would require the analysis of the whole module structure at compile-time to determine type identity or type dependence.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from a nominative type system in a number of aspects. The most prominent ones are that, for duck typing, type information is determined at runtime (as contrasted to compile-time) and the name of the type is irrelevant to determine type identity or type dependence; only partial structure information is required for that, for a given point in the program execution.&lt;br /&gt;
&lt;br /&gt;
Initially coined by Alex Martelli in the Python community, duck typing uses the premise that (referring to a value) &amp;quot;if it walks like a duck, and quacks like a duck, then it is a duck&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
There are a lot of discussions all over the internet about static vs dynamic languages. Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. Static typing and dynamic typing, are topics of programming language design that are not always clearly defined and, as a result, are not very well understood, especially for languages with mixing static and dynamic typing. For example, Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. This article has given you an insight into the concepts of static and dynamic typing. &lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=9&amp;amp;ved=0CD0QFjAI&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.69.5966%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=static%20and%20dynamic%20typing&amp;amp;ei=qx2yTPfgFcWclgfGs5CAAg&amp;amp;usg=AFQjCNFl0eYeI_vgs6nJD4V0TNRsBlMTEg] Static Typing Where Possible, Dynamic Typing When Needed:&lt;br /&gt;
The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Template:Type_system_cross_reference_list] Template:Type system cross reference list, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/#hl=en&amp;amp;source=hp&amp;amp;biw=1280&amp;amp;bih=760&amp;amp;q=static+and+dynamic+typing&amp;amp;aq=0&amp;amp;aqi=g2&amp;amp;aql=&amp;amp;oq=static+and+dynamic+typ&amp;amp;gs_rfai=C4_IOqB2yTOLZMYOIyAT7kYmCCgAAAKoEBU_QIQIR&amp;amp;fp=dc2ab5d7430ebd84] Dynamic vs. Static Typing — A Pattern-Based Analysis, Pascal Costanza, March, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5523694&amp;amp;tag=1] Including both static and dynamic typing in the same programming language,  Ortin, F.;    Zapico, D.;    Perez-Schofield, J.B.G.;    Garcia, M.;   Aug, 2010.&lt;br /&gt;
&lt;br /&gt;
[http://www.artima.com/weblogs/viewpost.jsp?thread=7590] Typing: Strong vs. Weak, Static vs. Dynamic, by Aahz, July, 2003.&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages] Dynamic type languages versus static type languages, Sep, 2009.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37961</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37961"/>
		<updated>2010-10-14T02:42:00Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
=== What is The Type System? ===&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
=== Classification of Type Checking ===&lt;br /&gt;
The process of verifying and enforcing the constraints of types – type checking – may occur either at compile-time (a static check) or run-time (a dynamic check). In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
===Mixing Static and Dynamic Typing ===&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Fundamentals ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Static Typing and Dynamic Typing versus Strong Typing and Weak Typing ===&lt;br /&gt;
Static and dynamic typing, and strong and weak typing, are two totally different concepts, which, unfortunately, are very often confused. It is erroneous to say that a language that is static or dynamic typed cannot be strong or weak typed. Static and dynamic typing, and strong and weak typing, are different forms of classification of programming languages, and one of each class necessarily characterizes a given language. It is thus imperative to discuss strong and weak typing vis-a-vis static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
Programming languages that exhibit &amp;quot;strong typing&amp;quot; are &amp;quot;strong typed,&amp;quot; and those that exhibit &amp;quot;weak typing&amp;quot; are &amp;quot;weak typed&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]].&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
=== Is Dynamic Typing Good? ===&lt;br /&gt;
In a dynamic typed language, you don't have to initialize variables, which is a big bonus for many developers. Programmers like the fact that you can use a variable at will when required (without having to initialize it). Dynamic typing is characteristic of many of the scripting languages: Perl, PHP, Python, etc. Dynamic typing, in fact, does save you from writing a few &amp;quot;extra&amp;quot; lines of code, which, in turn, means less time spent writing code.&lt;br /&gt;
&lt;br /&gt;
The very characteristic of dynamic typed languages that appeals to many developers is also a pitfall, and a major one at that. Consider the following simple example:&lt;br /&gt;
&lt;br /&gt;
/* Python code */&lt;br /&gt;
my_variable = 10&lt;br /&gt;
while my_variable &amp;gt; 0:&lt;br /&gt;
       i = foo(my_variable)&lt;br /&gt;
       if i &amp;lt; 100:&lt;br /&gt;
               my_variable++&lt;br /&gt;
       else&lt;br /&gt;
               my_varaible = (my_variable + i) / 10 // spelling error intentional&lt;br /&gt;
&lt;br /&gt;
As you can see in the above code, my_varaible is a spelling mistake that the programmer could have very well made. The problem here is that, since Python is dynamically typed, it will not return an error, but instead will create a new variable called my_varaible. So, now we have two variables: my_variable and my_varaible. This obviously is a serious problem; some would suggest that forced variable declaration is an important requirement in any programming language.&lt;br /&gt;
&lt;br /&gt;
=== Static Typed Behavior in Dynamic Typed Languages ===&lt;br /&gt;
Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. Consider the following Perl example:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
&lt;br /&gt;
                                    $sum = 10;&lt;br /&gt;
                                    print $sum;&lt;br /&gt;
&lt;br /&gt;
The above code will run without any problem, and will print 10 to the console. Note that here, we have not initialized the variable sum; this exemplifies the dynamic typing characteristic of Perl. To enforce variable declaration, we make use of the strict pragma as follows:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
&lt;br /&gt;
use strict;&lt;br /&gt;
$sum = 10;&lt;br /&gt;
print $sum;&lt;br /&gt;
&lt;br /&gt;
The above code fragment will return the following error when you try to run it:&lt;br /&gt;
&lt;br /&gt;
Global symbol &amp;quot;$num&amp;quot; requires explicit package name at perl.pl line 2.&lt;br /&gt;
Execution of perl.pl aborted due to compilation errors.&lt;br /&gt;
&lt;br /&gt;
To rectify the above error, we are forced to declare the variable num as follows:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
&lt;br /&gt;
use strict;&lt;br /&gt;
my $num; // forced declaration&lt;br /&gt;
$sum = 10;&lt;br /&gt;
print $sum;&lt;br /&gt;
&lt;br /&gt;
The above codes are specific to Perl; not all programming languages have a way to enforce variable declaration: Python, for example doesn't have a way to enforce variable declaration. However, there is a tool, called &amp;quot;pychecker&amp;quot; (available here), that can be used to &amp;quot;detect&amp;quot; stray variables; this is, of course, far from a desirable solution.&lt;br /&gt;
&lt;br /&gt;
== Polymorphism and types ==&lt;br /&gt;
The term &amp;quot;polymorphism&amp;quot; refers to the ability of code to act on values of multiple types, or to the ability of different instances of the same data-structure to contain elements of different types. Type systems that allow polymorphism generally do so in order to improve the potential for code re-use: in a language with polymorphism, programmers need only implement a data structure such as a list or an associative array once, rather than once for each type of element with which they plan to use it. For this reason computer scientists sometimes call the use of certain forms of polymorphism generic programming. The type-theoretic foundations of polymorphism are closely related to those of abstraction, modularity and (in some cases) subtyping.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing ===&lt;br /&gt;
In &amp;quot;duck typing&amp;quot;, a statement calling a method m on an object does not rely on the declared type of the object; only that the object, of whatever type, must implement the method called. One way of looking at this is that in duck typing systems the type of an object is intrinsic to the object and is determined by what methods it implements, and hence that a duck typing system is by definition type-safe since one can only invoke operations an object actually implements. Another way of looking at this is that the object is a member of several types, including a type that describes the fact that it &amp;quot;has a method m.&amp;quot; Type checking however occurs only on demand at runtime, every time the method m needs to be executed, not at compile-time or load-time.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from structural typing in that, if the part (of the whole module structure) needed for a given local computation is present at runtime, the duck type system is satisfied in its type identity analysis. On the other hand, a structural type system would require the analysis of the whole module structure at compile-time to determine type identity or type dependence.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from a nominative type system in a number of aspects. The most prominent ones are that, for duck typing, type information is determined at runtime (as contrasted to compile-time) and the name of the type is irrelevant to determine type identity or type dependence; only partial structure information is required for that, for a given point in the program execution.&lt;br /&gt;
&lt;br /&gt;
Initially coined by Alex Martelli in the Python community, duck typing uses the premise that (referring to a value) &amp;quot;if it walks like a duck, and quacks like a duck, then it is a duck&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
There are a lot of discussions all over the internet about static vs dynamic languages. Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. Static typing and dynamic typing, are topics of programming language design that are not always clearly defined and, as a result, are not very well understood, especially for languages with mixing static and dynamic typing. For example, Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. This article has given you an insight into the concepts of static and dynamic typing. &lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=9&amp;amp;ved=0CD0QFjAI&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.69.5966%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=static%20and%20dynamic%20typing&amp;amp;ei=qx2yTPfgFcWclgfGs5CAAg&amp;amp;usg=AFQjCNFl0eYeI_vgs6nJD4V0TNRsBlMTEg] Static Typing Where Possible, Dynamic Typing When Needed:&lt;br /&gt;
The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Template:Type_system_cross_reference_list] Template:Type system cross reference list, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/#hl=en&amp;amp;source=hp&amp;amp;biw=1280&amp;amp;bih=760&amp;amp;q=static+and+dynamic+typing&amp;amp;aq=0&amp;amp;aqi=g2&amp;amp;aql=&amp;amp;oq=static+and+dynamic+typ&amp;amp;gs_rfai=C4_IOqB2yTOLZMYOIyAT7kYmCCgAAAKoEBU_QIQIR&amp;amp;fp=dc2ab5d7430ebd84] Dynamic vs. Static Typing — A Pattern-Based Analysis, Pascal Costanza, March, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5523694&amp;amp;tag=1] Including both static and dynamic typing in the same programming language,  Ortin, F.;    Zapico, D.;    Perez-Schofield, J.B.G.;    Garcia, M.;   Aug, 2010.&lt;br /&gt;
&lt;br /&gt;
[http://www.artima.com/weblogs/viewpost.jsp?thread=7590] Typing: Strong vs. Weak, Static vs. Dynamic, by Aahz, July, 2003.&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages] Dynamic type languages versus static type languages, Sep, 2009.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37960</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37960"/>
		<updated>2010-10-14T02:40:10Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
=== What is The Type System? ===&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
=== Classification of Type Checking ===&lt;br /&gt;
The process of verifying and enforcing the constraints of types – type checking – may occur either at compile-time (a static check) or run-time (a dynamic check). In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
===Mixing Static and Dynamic Typing ===&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Fundamentals ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Static Typing and Dynamic Typing versus Strong Typing and Weak Typing ===&lt;br /&gt;
Static and dynamic typing, and strong and weak typing, are two totally different concepts, which, unfortunately, are very often confused. It is erroneous to say that a language that is static or dynamic typed cannot be strong or weak typed. Static and dynamic typing, and strong and weak typing, are different forms of classification of programming languages, and one of each class necessarily characterizes a given language. It is thus imperative to discuss strong and weak typing vis-a-vis static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
Programming languages that exhibit &amp;quot;strong typing&amp;quot; are &amp;quot;strong typed,&amp;quot; and those that exhibit &amp;quot;weak typing&amp;quot; are &amp;quot;weak typed&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]].&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
=== Is Dynamic Typing Good? ===&lt;br /&gt;
In a dynamic typed language, you don't have to initialize variables, which is a big bonus for many developers. Programmers like the fact that you can use a variable at will when required (without having to initialize it). Dynamic typing is characteristic of many of the scripting languages: Perl, PHP, Python, etc. Dynamic typing, in fact, does save you from writing a few &amp;quot;extra&amp;quot; lines of code, which, in turn, means less time spent writing code.&lt;br /&gt;
&lt;br /&gt;
The very characteristic of dynamic typed languages that appeals to many developers is also a pitfall, and a major one at that. Consider the following simple example:&lt;br /&gt;
&lt;br /&gt;
/* Python code */&lt;br /&gt;
my_variable = 10&lt;br /&gt;
while my_variable &amp;gt; 0:&lt;br /&gt;
       i = foo(my_variable)&lt;br /&gt;
       if i &amp;lt; 100:&lt;br /&gt;
               my_variable++&lt;br /&gt;
       else&lt;br /&gt;
               my_varaible = (my_variable + i) / 10 // spelling error intentional&lt;br /&gt;
&lt;br /&gt;
As you can see in the above code, my_varaible is a spelling mistake that the programmer could have very well made. The problem here is that, since Python is dynamically typed, it will not return an error, but instead will create a new variable called my_varaible. So, now we have two variables: my_variable and my_varaible. This obviously is a serious problem; some would suggest that forced variable declaration is an important requirement in any programming language.&lt;br /&gt;
&lt;br /&gt;
=== Static Typed Behavior in Dynamic Typed Languages ===&lt;br /&gt;
Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. Consider the following Perl example:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
$sum = 10;&lt;br /&gt;
print $sum;&lt;br /&gt;
&lt;br /&gt;
The above code will run without any problem, and will print 10 to the console. Note that here, we have not initialized the variable sum; this exemplifies the dynamic typing characteristic of Perl. To enforce variable declaration, we make use of the strict pragma as follows:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
use strict;&lt;br /&gt;
$sum = 10;&lt;br /&gt;
print $sum;&lt;br /&gt;
&lt;br /&gt;
The above code fragment will return the following error when you try to run it:&lt;br /&gt;
&lt;br /&gt;
Global symbol &amp;quot;$num&amp;quot; requires explicit package name at perl.pl line 2.&lt;br /&gt;
Execution of perl.pl aborted due to compilation errors.&lt;br /&gt;
&lt;br /&gt;
To rectify the above error, we are forced to declare the variable num as follows:&lt;br /&gt;
&lt;br /&gt;
/* Perl code */&lt;br /&gt;
use strict;&lt;br /&gt;
my $num; // forced declaration&lt;br /&gt;
$sum = 10;&lt;br /&gt;
print $sum;&lt;br /&gt;
&lt;br /&gt;
The above codes are specific to Perl; not all programming languages have a way to enforce variable declaration: Python, for example doesn't have a way to enforce variable declaration. However, there is a tool, called &amp;quot;pychecker&amp;quot; (available here), that can be used to &amp;quot;detect&amp;quot; stray variables; this is, of course, far from a desirable solution.&lt;br /&gt;
&lt;br /&gt;
== Polymorphism and types ==&lt;br /&gt;
The term &amp;quot;polymorphism&amp;quot; refers to the ability of code to act on values of multiple types, or to the ability of different instances of the same data-structure to contain elements of different types. Type systems that allow polymorphism generally do so in order to improve the potential for code re-use: in a language with polymorphism, programmers need only implement a data structure such as a list or an associative array once, rather than once for each type of element with which they plan to use it. For this reason computer scientists sometimes call the use of certain forms of polymorphism generic programming. The type-theoretic foundations of polymorphism are closely related to those of abstraction, modularity and (in some cases) subtyping.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing ===&lt;br /&gt;
In &amp;quot;duck typing&amp;quot;, a statement calling a method m on an object does not rely on the declared type of the object; only that the object, of whatever type, must implement the method called. One way of looking at this is that in duck typing systems the type of an object is intrinsic to the object and is determined by what methods it implements, and hence that a duck typing system is by definition type-safe since one can only invoke operations an object actually implements. Another way of looking at this is that the object is a member of several types, including a type that describes the fact that it &amp;quot;has a method m.&amp;quot; Type checking however occurs only on demand at runtime, every time the method m needs to be executed, not at compile-time or load-time.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from structural typing in that, if the part (of the whole module structure) needed for a given local computation is present at runtime, the duck type system is satisfied in its type identity analysis. On the other hand, a structural type system would require the analysis of the whole module structure at compile-time to determine type identity or type dependence.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from a nominative type system in a number of aspects. The most prominent ones are that, for duck typing, type information is determined at runtime (as contrasted to compile-time) and the name of the type is irrelevant to determine type identity or type dependence; only partial structure information is required for that, for a given point in the program execution.&lt;br /&gt;
&lt;br /&gt;
Initially coined by Alex Martelli in the Python community, duck typing uses the premise that (referring to a value) &amp;quot;if it walks like a duck, and quacks like a duck, then it is a duck&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
There are a lot of discussions all over the internet about static vs dynamic languages. Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. Static typing and dynamic typing, are topics of programming language design that are not always clearly defined and, as a result, are not very well understood, especially for languages with mixing static and dynamic typing. For example, Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. This article has given you an insight into the concepts of static and dynamic typing. &lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=9&amp;amp;ved=0CD0QFjAI&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.69.5966%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=static%20and%20dynamic%20typing&amp;amp;ei=qx2yTPfgFcWclgfGs5CAAg&amp;amp;usg=AFQjCNFl0eYeI_vgs6nJD4V0TNRsBlMTEg] Static Typing Where Possible, Dynamic Typing When Needed:&lt;br /&gt;
The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Template:Type_system_cross_reference_list] Template:Type system cross reference list, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/#hl=en&amp;amp;source=hp&amp;amp;biw=1280&amp;amp;bih=760&amp;amp;q=static+and+dynamic+typing&amp;amp;aq=0&amp;amp;aqi=g2&amp;amp;aql=&amp;amp;oq=static+and+dynamic+typ&amp;amp;gs_rfai=C4_IOqB2yTOLZMYOIyAT7kYmCCgAAAKoEBU_QIQIR&amp;amp;fp=dc2ab5d7430ebd84] Dynamic vs. Static Typing — A Pattern-Based Analysis, Pascal Costanza, March, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5523694&amp;amp;tag=1] Including both static and dynamic typing in the same programming language,  Ortin, F.;    Zapico, D.;    Perez-Schofield, J.B.G.;    Garcia, M.;   Aug, 2010.&lt;br /&gt;
&lt;br /&gt;
[http://www.artima.com/weblogs/viewpost.jsp?thread=7590] Typing: Strong vs. Weak, Static vs. Dynamic, by Aahz, July, 2003.&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages] Dynamic type languages versus static type languages, Sep, 2009.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37959</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37959"/>
		<updated>2010-10-14T02:30:39Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
=== What is The Type System? ===&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
=== Classification of Type Checking ===&lt;br /&gt;
The process of verifying and enforcing the constraints of types – type checking – may occur either at compile-time (a static check) or run-time (a dynamic check). In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
===Mixing Static and Dynamic Typing ===&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Fundamentals ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]].&lt;br /&gt;
&lt;br /&gt;
== Polymorphism and types ==&lt;br /&gt;
The term &amp;quot;polymorphism&amp;quot; refers to the ability of code to act on values of multiple types, or to the ability of different instances of the same data-structure to contain elements of different types. Type systems that allow polymorphism generally do so in order to improve the potential for code re-use: in a language with polymorphism, programmers need only implement a data structure such as a list or an associative array once, rather than once for each type of element with which they plan to use it. For this reason computer scientists sometimes call the use of certain forms of polymorphism generic programming. The type-theoretic foundations of polymorphism are closely related to those of abstraction, modularity and (in some cases) subtyping.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing ===&lt;br /&gt;
In &amp;quot;duck typing&amp;quot;, a statement calling a method m on an object does not rely on the declared type of the object; only that the object, of whatever type, must implement the method called. One way of looking at this is that in duck typing systems the type of an object is intrinsic to the object and is determined by what methods it implements, and hence that a duck typing system is by definition type-safe since one can only invoke operations an object actually implements. Another way of looking at this is that the object is a member of several types, including a type that describes the fact that it &amp;quot;has a method m.&amp;quot; Type checking however occurs only on demand at runtime, every time the method m needs to be executed, not at compile-time or load-time.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from structural typing in that, if the part (of the whole module structure) needed for a given local computation is present at runtime, the duck type system is satisfied in its type identity analysis. On the other hand, a structural type system would require the analysis of the whole module structure at compile-time to determine type identity or type dependence.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from a nominative type system in a number of aspects. The most prominent ones are that, for duck typing, type information is determined at runtime (as contrasted to compile-time) and the name of the type is irrelevant to determine type identity or type dependence; only partial structure information is required for that, for a given point in the program execution.&lt;br /&gt;
&lt;br /&gt;
Initially coined by Alex Martelli in the Python community, duck typing uses the premise that (referring to a value) &amp;quot;if it walks like a duck, and quacks like a duck, then it is a duck&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
There are a lot of discussions all over the internet about static vs dynamic languages. Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. Static typing and dynamic typing, are topics of programming language design that are not always clearly defined and, as a result, are not very well understood, especially for languages with mixing static and dynamic typing. For example, Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. This article has given you an insight into the concepts of static and dynamic typing. &lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=9&amp;amp;ved=0CD0QFjAI&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.69.5966%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=static%20and%20dynamic%20typing&amp;amp;ei=qx2yTPfgFcWclgfGs5CAAg&amp;amp;usg=AFQjCNFl0eYeI_vgs6nJD4V0TNRsBlMTEg] Static Typing Where Possible, Dynamic Typing When Needed:&lt;br /&gt;
The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Template:Type_system_cross_reference_list] Template:Type system cross reference list, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/#hl=en&amp;amp;source=hp&amp;amp;biw=1280&amp;amp;bih=760&amp;amp;q=static+and+dynamic+typing&amp;amp;aq=0&amp;amp;aqi=g2&amp;amp;aql=&amp;amp;oq=static+and+dynamic+typ&amp;amp;gs_rfai=C4_IOqB2yTOLZMYOIyAT7kYmCCgAAAKoEBU_QIQIR&amp;amp;fp=dc2ab5d7430ebd84] Dynamic vs. Static Typing — A Pattern-Based Analysis, Pascal Costanza, March, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5523694&amp;amp;tag=1] Including both static and dynamic typing in the same programming language,  Ortin, F.;    Zapico, D.;    Perez-Schofield, J.B.G.;    Garcia, M.;   Aug, 2010.&lt;br /&gt;
&lt;br /&gt;
[http://www.artima.com/weblogs/viewpost.jsp?thread=7590] Typing: Strong vs. Weak, Static vs. Dynamic, by Aahz, July, 2003.&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages] Dynamic type languages versus static type languages, Sep, 2009.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37955</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37955"/>
		<updated>2010-10-14T02:29:41Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
=== What is The Type System? ===&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
=== Classification of Type Checking ===&lt;br /&gt;
The process of verifying and enforcing the constraints of types – type checking – may occur either at compile-time (a static check) or run-time (a dynamic check). In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
===Mixing Static and Dynamic Typing ===&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]].&lt;br /&gt;
&lt;br /&gt;
== Polymorphism and types ==&lt;br /&gt;
The term &amp;quot;polymorphism&amp;quot; refers to the ability of code to act on values of multiple types, or to the ability of different instances of the same data-structure to contain elements of different types. Type systems that allow polymorphism generally do so in order to improve the potential for code re-use: in a language with polymorphism, programmers need only implement a data structure such as a list or an associative array once, rather than once for each type of element with which they plan to use it. For this reason computer scientists sometimes call the use of certain forms of polymorphism generic programming. The type-theoretic foundations of polymorphism are closely related to those of abstraction, modularity and (in some cases) subtyping.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing ===&lt;br /&gt;
In &amp;quot;duck typing&amp;quot;, a statement calling a method m on an object does not rely on the declared type of the object; only that the object, of whatever type, must implement the method called. One way of looking at this is that in duck typing systems the type of an object is intrinsic to the object and is determined by what methods it implements, and hence that a duck typing system is by definition type-safe since one can only invoke operations an object actually implements. Another way of looking at this is that the object is a member of several types, including a type that describes the fact that it &amp;quot;has a method m.&amp;quot; Type checking however occurs only on demand at runtime, every time the method m needs to be executed, not at compile-time or load-time.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from structural typing in that, if the part (of the whole module structure) needed for a given local computation is present at runtime, the duck type system is satisfied in its type identity analysis. On the other hand, a structural type system would require the analysis of the whole module structure at compile-time to determine type identity or type dependence.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from a nominative type system in a number of aspects. The most prominent ones are that, for duck typing, type information is determined at runtime (as contrasted to compile-time) and the name of the type is irrelevant to determine type identity or type dependence; only partial structure information is required for that, for a given point in the program execution.&lt;br /&gt;
&lt;br /&gt;
Initially coined by Alex Martelli in the Python community, duck typing uses the premise that (referring to a value) &amp;quot;if it walks like a duck, and quacks like a duck, then it is a duck&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
There are a lot of discussions all over the internet about static vs dynamic languages. Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. Static typing and dynamic typing, are topics of programming language design that are not always clearly defined and, as a result, are not very well understood, especially for languages with mixing static and dynamic typing. For example, Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. This article has given you an insight into the concepts of static and dynamic typing. &lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=9&amp;amp;ved=0CD0QFjAI&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.69.5966%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=static%20and%20dynamic%20typing&amp;amp;ei=qx2yTPfgFcWclgfGs5CAAg&amp;amp;usg=AFQjCNFl0eYeI_vgs6nJD4V0TNRsBlMTEg] Static Typing Where Possible, Dynamic Typing When Needed:&lt;br /&gt;
The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Template:Type_system_cross_reference_list] Template:Type system cross reference list, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/#hl=en&amp;amp;source=hp&amp;amp;biw=1280&amp;amp;bih=760&amp;amp;q=static+and+dynamic+typing&amp;amp;aq=0&amp;amp;aqi=g2&amp;amp;aql=&amp;amp;oq=static+and+dynamic+typ&amp;amp;gs_rfai=C4_IOqB2yTOLZMYOIyAT7kYmCCgAAAKoEBU_QIQIR&amp;amp;fp=dc2ab5d7430ebd84] Dynamic vs. Static Typing — A Pattern-Based Analysis, Pascal Costanza, March, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5523694&amp;amp;tag=1] Including both static and dynamic typing in the same programming language,  Ortin, F.;    Zapico, D.;    Perez-Schofield, J.B.G.;    Garcia, M.;   Aug, 2010.&lt;br /&gt;
&lt;br /&gt;
[http://www.artima.com/weblogs/viewpost.jsp?thread=7590] Typing: Strong vs. Weak, Static vs. Dynamic, by Aahz, July, 2003.&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages] Dynamic type languages versus static type languages, Sep, 2009.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37809</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37809"/>
		<updated>2010-10-10T21:25:49Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
&lt;br /&gt;
In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]]&lt;br /&gt;
.&lt;br /&gt;
&lt;br /&gt;
== Polymorphism and types ==&lt;br /&gt;
The term &amp;quot;polymorphism&amp;quot; refers to the ability of code to act on values of multiple types, or to the ability of different instances of the same data-structure to contain elements of different types. Type systems that allow polymorphism generally do so in order to improve the potential for code re-use: in a language with polymorphism, programmers need only implement a data structure such as a list or an associative array once, rather than once for each type of element with which they plan to use it. For this reason computer scientists sometimes call the use of certain forms of polymorphism generic programming. The type-theoretic foundations of polymorphism are closely related to those of abstraction, modularity and (in some cases) subtyping.&lt;br /&gt;
&lt;br /&gt;
=== Duck Typing ===&lt;br /&gt;
In &amp;quot;duck typing&amp;quot;, [4] a statement calling a method m on an object does not rely on the declared type of the object; only that the object, of whatever type, must implement the method called. One way of looking at this is that in duck typing systems the type of an object is intrinsic to the object and is determined by what methods it implements, and hence that a duck typing system is by definition type-safe since one can only invoke operations an object actually implements. Another way of looking at this is that the object is a member of several types, including a type that describes the fact that it &amp;quot;has a method m.&amp;quot; Type checking however occurs only on demand at runtime, every time the method m needs to be executed, not at compile-time or load-time.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from structural typing in that, if the part (of the whole module structure) needed for a given local computation is present at runtime, the duck type system is satisfied in its type identity analysis. On the other hand, a structural type system would require the analysis of the whole module structure at compile-time to determine type identity or type dependence.&lt;br /&gt;
&lt;br /&gt;
Duck typing differs from a nominative type system in a number of aspects. The most prominent ones are that, for duck typing, type information is determined at runtime (as contrasted to compile-time) and the name of the type is irrelevant to determine type identity or type dependence; only partial structure information is required for that, for a given point in the program execution.&lt;br /&gt;
&lt;br /&gt;
Initially coined by Alex Martelli in the Python community, duck typing uses the premise that (referring to a value) &amp;quot;if it walks like a duck, and quacks like a duck, then it is a duck&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
There are a lot of discussions all over the internet about static vs dynamic languages. Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. Static typing and dynamic typing, are topics of programming language design that are not always clearly defined and, as a result, are not very well understood, especially for languages with mixing static and dynamic typing. For example, Perl is a dynamic typed programming language. However, it provides a means to &amp;quot;simulate&amp;quot; static typing by means of a pragma called strict. This article has given you an insight into the concepts of static and dynamic typing. &lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=9&amp;amp;ved=0CD0QFjAI&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.69.5966%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=static%20and%20dynamic%20typing&amp;amp;ei=qx2yTPfgFcWclgfGs5CAAg&amp;amp;usg=AFQjCNFl0eYeI_vgs6nJD4V0TNRsBlMTEg] Static Typing Where Possible, Dynamic Typing When Needed:&lt;br /&gt;
The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Template:Type_system_cross_reference_list] Template:Type system cross reference list, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/#hl=en&amp;amp;source=hp&amp;amp;biw=1280&amp;amp;bih=760&amp;amp;q=static+and+dynamic+typing&amp;amp;aq=0&amp;amp;aqi=g2&amp;amp;aql=&amp;amp;oq=static+and+dynamic+typ&amp;amp;gs_rfai=C4_IOqB2yTOLZMYOIyAT7kYmCCgAAAKoEBU_QIQIR&amp;amp;fp=dc2ab5d7430ebd84] Dynamic vs. Static Typing — A Pattern-Based Analysis, Pascal Costanza, March, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5523694&amp;amp;tag=1] Including both static and dynamic typing in the same programming language,  Ortin, F.;    Zapico, D.;    Perez-Schofield, J.B.G.;    Garcia, M.;   Aug, 2010.&lt;br /&gt;
&lt;br /&gt;
[http://www.artima.com/weblogs/viewpost.jsp?thread=7590] Typing: Strong vs. Weak, Static vs. Dynamic, by Aahz, July, 2003.&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages] Dynamic type languages versus static type languages, Sep, 2009.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37808</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37808"/>
		<updated>2010-10-10T20:38:53Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
&lt;br /&gt;
In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]]&lt;br /&gt;
.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=9&amp;amp;ved=0CD0QFjAI&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.69.5966%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=static%20and%20dynamic%20typing&amp;amp;ei=qx2yTPfgFcWclgfGs5CAAg&amp;amp;usg=AFQjCNFl0eYeI_vgs6nJD4V0TNRsBlMTEg] Static Typing Where Possible, Dynamic Typing When Needed:&lt;br /&gt;
The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Template:Type_system_cross_reference_list] Template:Type system cross reference list, from Wikipedia.&lt;br /&gt;
&lt;br /&gt;
[http://www.google.com/#hl=en&amp;amp;source=hp&amp;amp;biw=1280&amp;amp;bih=760&amp;amp;q=static+and+dynamic+typing&amp;amp;aq=0&amp;amp;aqi=g2&amp;amp;aql=&amp;amp;oq=static+and+dynamic+typ&amp;amp;gs_rfai=C4_IOqB2yTOLZMYOIyAT7kYmCCgAAAKoEBU_QIQIR&amp;amp;fp=dc2ab5d7430ebd84] Dynamic vs. Static Typing — A Pattern-Based Analysis, Pascal Costanza, March, 2004.&lt;br /&gt;
&lt;br /&gt;
[http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5523694&amp;amp;tag=1] Including both static and dynamic typing in the same programming language,  Ortin, F.;    Zapico, D.;    Perez-Schofield, J.B.G.;    Garcia, M.;   Aug, 2010.&lt;br /&gt;
&lt;br /&gt;
[http://www.artima.com/weblogs/viewpost.jsp?thread=7590] Typing: Strong vs. Weak, Static vs. Dynamic, by Aahz, July, 2003.&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages] Dynamic type languages versus static type languages, Sep, 2009.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37807</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37807"/>
		<updated>2010-10-10T20:22:12Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
&lt;br /&gt;
In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                   /* C code */&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
&lt;br /&gt;
The above code fragment is an example of how variable declaration in static typed languages generally appears. Note that in the above code, static has nothing to do with static typing; it has been used along with int only to initialize num and sum to zero.&lt;br /&gt;
&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they're used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                /* Python code */&lt;br /&gt;
              num = 10 // directly using the variable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]]&lt;br /&gt;
.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
[http://articles.sitepoint.com/article/typing-versus-dynamic-typing] Introduction to Static and Dynamic Typing, Premshree Pillai, June, 2004.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Type_system] Type system, from Wikipedia.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37806</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37806"/>
		<updated>2010-10-10T20:15:59Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
&lt;br /&gt;
In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
                      /* C code */&lt;br /&gt;
&lt;br /&gt;
              static int num, sum; // explicit declaration&lt;br /&gt;
              num = 5; // now use the variables&lt;br /&gt;
              sum = 10;&lt;br /&gt;
              sum = sum + num;&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]]&lt;br /&gt;
.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37805</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37805"/>
		<updated>2010-10-10T20:14:43Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
&lt;br /&gt;
In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
Static typed programming languages are those in which variables need not be defined before they're used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they're employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don't get converted; you just read them assuming they are another type.&lt;br /&gt;
&lt;br /&gt;
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:&lt;br /&gt;
&lt;br /&gt;
/* C code */&lt;br /&gt;
static int num, sum; // explicit declaration&lt;br /&gt;
num = 5; // now use the variables&lt;br /&gt;
sum = 10;&lt;br /&gt;
sum = sum + num;&lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]]&lt;br /&gt;
.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37804</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37804"/>
		<updated>2010-10-10T20:02:16Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
&lt;br /&gt;
In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Statically typed languages include Ada, AS3, C, C++, C#, Eiffel, F#, Go, JADE, Java, Fortran, Haskell, ML, Objective-C, Pascal, Perl. They will reject some programs that may be well-behaved at run-time, but that cannot be statically determined to be well-typed. For example, even if an expression &amp;lt;complex test&amp;gt; always evaluates to true at run-time, a program containing the code&lt;br /&gt;
&lt;br /&gt;
    if &amp;lt;complex test&amp;gt; then 42 else &amp;lt;type error&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be rejected as ill-typed, because a static analysis cannot determine that the else branch won't be taken. &lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing, values have types but variables do not; that is, a variable can refer to a value of any type. Dynamically typed languages include Erlang, Groovy, JavaScript, Lisp, Lua, Objective-C, Perl (with respect to user-defined types but not built-in types), PHP, Prolog, Python, Ruby, Smalltalk and Tcl. Compared to static typing, dynamic typing can be more flexible (e.g. by allowing programs to generate types and functionality based on run-time data), though at the expense of fewer a priori guarantees. This is because a dynamically typed language accepts and attempts to execute some programs which may be ruled as invalid by a static type checker. The term &amp;quot;dynamic language&amp;quot; means something different (&amp;quot;runtime dynamism&amp;quot;) and a dynamic language is not necessarily dynamically typed.&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. &lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]]&lt;br /&gt;
.&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Typereference.jpg&amp;diff=37803</id>
		<title>File:Typereference.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Typereference.jpg&amp;diff=37803"/>
		<updated>2010-10-10T19:47:44Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37802</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3i ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3i_ls&amp;diff=37802"/>
		<updated>2010-10-10T19:40:54Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p&amp;gt;Mixing static and dynamic code other than Ruby and Java&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A programming language is a notation for writing programs, which are specifications of a computation or algorithm. Some, but not all, authors restrict the term &amp;quot;programming language&amp;quot; to those languages that can express all possible algorithms. A type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. The goal of a type system is to verify and usually enforce a certain level of correctness in programs written in that language by detecting certain incorrect operations. Any decidable type system involves a trade-off: while it rejects many incorrect programs, it can also prohibit some correct, albeit unusual programs. In order to bypass this downside, a number of languages have type loopholes, usually unchecked casts that may be used by the programmer to explicitly allow a normally disallowed operation between different types. In most typed languages, the type system is used only to type check programs, but a number of languages, usually functional ones, perform type inference, which relieves the programmer from writing type annotations. The formal design and study of type systems is known as type theory. &lt;br /&gt;
&lt;br /&gt;
In static typing all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. Statically typed languages can be either manifestly typed or type-inferred. In the first case, the programmer must explicitly write types at certain textual positions (for example, at variable declarations). In the second case, the compiler infers the types of expressions and declarations based on context. Most mainstream statically typed languages, such as C++, C# and Java, are manifestly typed. Complete type inference has traditionally been associated with less mainstream languages, such as Haskell and ML. However, many manifestly typed languages support partial type inference; for example, Java and C# both infer types in certain limited cases.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.As with type-inferred languages, dynamically typed languages do not require the programmer to write explicit type annotations on expressions. Among other things, this may permit a single variable to refer to values of different types at different points in the program execution. However, type errors cannot be automatically detected until a piece of code is actually executed, making debugging more difficult. Ruby, Lisp, JavaScript, and Python are dynamically typed.&lt;br /&gt;
&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations, the support of runtime type tests, a form of dynamic typing. &lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question determines what constitutes a type error, but a type system generally seeks to guarantee that operations expecting a certain kind of value are not used with values for which that operation makes no sense.&lt;br /&gt;
&lt;br /&gt;
Assigning data types (typing) gives meaning to sequences of bits. Types usually have associations either with values in memory or with objects such as variables. Because any value simply consists of a sequence of bits in a computer, hardware makes no intrinsic distinction even between memory addresses, instruction code, characters, integers and floating-point numbers, being unable to discriminate between them based on bit pattern alone. Associating a sequence of bits and a type informs programs and programmers how that sequence of bits should be understood.&lt;br /&gt;
&lt;br /&gt;
Major functions provided by type systems include:&lt;br /&gt;
*Safety: Use of types may allow a compiler to detect meaningless or probably invalid code.&lt;br /&gt;
*Optimization – Static type-checking may provide useful compile-time information.Optimization – Static type-checking may provide useful compile-time information.&lt;br /&gt;
*Abstraction (or modularity) – Types allow programmers to think about programs at a higher level than the bit or byte, not bothering with low-level implementation.&lt;br /&gt;
&lt;br /&gt;
Type safety contributes to program correctness, but cannot guarantee it unless the type checking itself becomes an undecidable problem. Depending on the specific type system, a program may give the wrong result and be safely typed, producing no compiler errors. For instance, division by zero is not caught by the type checker in most programming languages; instead it is a runtime error. To prove the absence of more general defects, other kinds of formal methods, collectively known as program analysis, are in common use, as well as software testing—a widely used empirical method for finding errors that the type checker cannot detect.&lt;br /&gt;
&lt;br /&gt;
== Static Typing vs. Dynamic Typing ==&lt;br /&gt;
=== Static Typing ===&lt;br /&gt;
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Statically typed languages include Ada, AS3, C, C++, C#, Eiffel, F#, Go, JADE, Java, Fortran, Haskell, ML, Objective-C, Pascal, Perl. They will reject some programs that may be well-behaved at run-time, but that cannot be statically determined to be well-typed. For example, even if an expression &amp;lt;complex test&amp;gt; always evaluates to true at run-time, a program containing the code&lt;br /&gt;
&lt;br /&gt;
    if &amp;lt;complex test&amp;gt; then 42 else &amp;lt;type error&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be rejected as ill-typed, because a static analysis cannot determine that the else branch won't be taken. &lt;br /&gt;
=== Dynamic Typing ===&lt;br /&gt;
A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing, values have types but variables do not; that is, a variable can refer to a value of any type. Dynamically typed languages include Erlang, Groovy, JavaScript, Lisp, Lua, Objective-C, Perl (with respect to user-defined types but not built-in types), PHP, Prolog, Python, Ruby, Smalltalk and Tcl. Compared to static typing, dynamic typing can be more flexible (e.g. by allowing programs to generate types and functionality based on run-time data), though at the expense of fewer a priori guarantees. This is because a dynamically typed language accepts and attempts to execute some programs which may be ruled as invalid by a static type checker. The term &amp;quot;dynamic language&amp;quot; means something different (&amp;quot;runtime dynamism&amp;quot;) and a dynamic language is not necessarily dynamically typed.&lt;br /&gt;
&lt;br /&gt;
== Mixing Static and Dynamic Typing ==&lt;br /&gt;
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.&lt;br /&gt;
&lt;br /&gt;
As of the 4.0 Release, the .NET Framework supports a variant of dynamic typing via the System.Dynamic namespace whereby a static object of type 'dynamic' is a placeholder for the .NET runtime to interrogate its dynamic facilities to resolve the object reference.&lt;br /&gt;
&lt;br /&gt;
The choice between static and dynamic typing requires trade-offs.&lt;br /&gt;
&lt;br /&gt;
Static typing can find type errors reliably at compile time. This should increase the reliability of the delivered program. However, programmers disagree over how commonly type errors occur, and thus what proportion of those bugs which are written would be caught by static typing. Static typing advocates believe programs are more reliable when they have been well type-checked, while dynamic typing advocates point to distributed code that has proven reliable and to small bug databases. The value of static typing, then, presumably increases as the strength of the type system is increased. Advocates of dependently typed languages such as Dependent ML and Epigram have suggested that almost all bugs can be considered type errors, if the types used in a program are properly declared by the programmer or correctly inferred by the compiler. [3]&lt;br /&gt;
&lt;br /&gt;
Static typing usually results in compiled code that executes more quickly. When the compiler knows the exact data types that are in use, it can produce optimized machine code. Further, compilers for statically typed languages can find assembler shortcuts more easily. Some dynamically typed languages such as Common Lisp allow optional type declarations for optimization for this very reason. Static typing makes this pervasive. See optimization.&lt;br /&gt;
&lt;br /&gt;
By contrast, dynamic typing may allow compilers to run more quickly and allow interpreters to dynamically load new code, since changes to source code in dynamically typed languages may result in less checking to perform and less code to revisit. This too may reduce the edit-compile-test-debug cycle.&lt;br /&gt;
&lt;br /&gt;
Statically typed languages which lack type inference (such as Java and C) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization. However, a language can be statically typed without requiring type declarations (examples include Haskell, Scala and to a lesser extent C#), so this is not a necessary consequence of static typing.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing allows constructs that some static type checking would reject as illegal. For example, eval functions, which execute arbitrary data as code, become possible. Furthermore, dynamic typing better accommodates transitional code and prototyping, such as allowing a placeholder data structure (mock object) to be transparently used in place of a full-fledged data structure (usually for the purposes of experimentation and testing).&lt;br /&gt;
&lt;br /&gt;
Dynamic typing is used in Duck typing which can support easier code reuse.&lt;br /&gt;
&lt;br /&gt;
Dynamic typing typically makes metaprogramming more effective and easier to use. For example, C++ templates are typically more cumbersome to write than the equivalent Ruby or Python code.[citation needed] More advanced run-time constructs such as metaclasses and introspection are often more difficult to use in statically typed languages.&lt;br /&gt;
&lt;br /&gt;
The following table shows the type system cross reference list.&lt;br /&gt;
[[Image:typereference.jpg|center|type system cross reference list.]]&lt;br /&gt;
&lt;br /&gt;
== Programming Style ==&lt;br /&gt;
Some programmers prefer statically typed languages; others prefer dynamically typed languages. Statically typed languages alert programmers to type errors during compilation, and they may perform better at runtime. Advocates of dynamically typed languages claim they better support rapid prototyping and that type errors are only a small subset of errors in a program. Likewise, there is often no need to manually declare all types in statically typed languages with type inference; thus, the need for the programmer to explicitly specify types of variables is automatically lowered for such languages; and some dynamic languages have run-time optimisers that can generate fast code approaching the speed of static language compilers, often by using partial type inference.&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2c_ls&amp;diff=35956</id>
		<title>CSC/ECE 517 Fall 2010/ch2 2c ls</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch2_2c_ls&amp;diff=35956"/>
		<updated>2010-09-22T04:35:07Z</updated>

		<summary type="html">&lt;p&gt;Paullei: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
In object-oriented programming (oop), inheritance is a way to reuse code by creating collections of attributes and beheaviors called objects, which can be classified as class-based inheritance and prototype-based inheritance. In class-based inheritance, objects are defined by classes which can inherit other classes; and in contrast, in prototype-based inheritance, objects can be defined directly from other objects without the need to define any classes. Particularly, prototype-based inheritance is a code reuse behavior which is performed via a process of cloning existing objects that serve as prototypes.&lt;br /&gt;
= &amp;quot;Class-based&amp;quot; Vs. &amp;quot;Prototype-Based&amp;quot; =&lt;br /&gt;
We first compare the whole &amp;quot;class&amp;quot; with &amp;quot;prototype&amp;quot;. The idea originally began in Simula, where with a class-based method each class represented a set of objects that share the same state space and the same operations, thereby forming an equivalence class. Later object-oriented languages wanted to be able to use static type checking, so we got the notion of a fixed class set at compile time. &lt;br /&gt;
In  &amp;quot;class-based&amp;quot; inheritance, copying happens at compile time. In prototype-based inheritance, the operations are stored in the prototype data structure, which is copied and modified at run time. Abstractly, though, a class is still the equivalence class of all objects that share the same state space and methods. When you add a method to the prototype, you're effectively making an element of a new equivalence class.&lt;br /&gt;
&lt;br /&gt;
In class-based languages a new instance is constructed through the class's constructor and an optional set of constructor arguments. The resulting instance is modeled on the layout and behavior dictated by the chosen class.&lt;br /&gt;
&lt;br /&gt;
In prototype-based systems there are two methods of constructing new objects, through cloning of an existing object, and through ex nihilo (&amp;quot;from nothing&amp;quot;) object creation. While most systems support a variety of cloning, ex nihilo object creation is not as prominent.&lt;br /&gt;
&lt;br /&gt;
= Pros for Prototype-base Inheritance =&lt;br /&gt;
Class-based languages encourage a model of development that focuses first on the taxonomy and relationships between classes. In contrast, prototype-based programming is seen as encouraging the programmer to focus on the behavior of some set of examples and only later worry about classifying these objects into archetypal objects that are later used in a fashion similar to classes. As such, many prototype-based systems encourage the alteration of prototypes during runtime, whereas only very few class-based object-oriented systems (such as the dynamic object-oriented system, Smalltalk, Python, Perl, or Ruby) allow classes to be altered during the execution of a program. Advocates argue the following pros for prototype-based inheritance:&lt;br /&gt;
1, Suitable in loosely typed environments, no need to define explicit types.&lt;br /&gt;
2, Makes it incredibly easy to implement singleton pattern ( compare javascript and java in this regard.)&lt;br /&gt;
3, Provides ways of applying a method of an object in the context of a different object, adding and replacing methods dynamically from an object etc.&lt;br /&gt;
= Cons for Prototype-base Inheritance =&lt;br /&gt;
Advocates of class-based inheritance criticize prototype-based inheritance often have concerns that could be seen as similar to those concerns that proponents of static type systems for programming languages have of dynamic type systems (see Datatype). Usually, such concerns involve: correctness, safety, predictability, and efficiency.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
* (1) Günther Blaschek, Omega: Statically Typed Prototypes.&lt;br /&gt;
* (2) John C. Mitchell, Concepts in programming languages, Cambridge University Press, 2003, ISBN 0521780985, chapter 10 &amp;quot;Concepts in object-oriented languages&amp;quot;&lt;br /&gt;
* (3) Antero Taivalsaari, Classes vs. Prototypes: Some Philosophical and Historical Observations&lt;br /&gt;
* [http://publications.ai.mit.edu/ai-publications/pdf/AIM-602.pdf (4)]&lt;/div&gt;</summary>
		<author><name>Paullei</name></author>
	</entry>
</feed>