0

Link to command pattern

Why does the client have no reference to the invoker when it has references to receivers and concretecommands?

public static void main(String[] args) 
{
    StockTrade stock = new StockTrade();
    BuyStockOrder bsc = new BuyStockOrder (stock);
    SellStockOrder ssc = new SellStockOrder (stock);
    Agent agent = new Agent(); //<-- The invoker is right there yet it is not
                               // in the diagram?

    agent.placeOrder(bsc);
    agent.placeOrder(ssc);
}
Tad Donaghe
  • 6,625
  • 1
  • 29
  • 64
Mads Andersen
  • 3,123
  • 5
  • 38
  • 44

2 Answers2

2

The wiki article explains this as:

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.

toolkit
  • 49,809
  • 17
  • 109
  • 135
  • Dances around the root of the question, the point is that Client and Invoker can be decoupled, the "main" just happens create and call them both. – Ted Johnson Jan 19 '10 at 01:48
1

If I understand you question correctly, the answer is this.

There is a reference in that diagram that can be traced from the Client to Invoker. If you look you can see there is a dotted line from Client to ConcreteCommand called "instantiate" and then a line from ConcreteCommand to Command and then a line from Command to Invoker.

As I understand it -- this dotted line represents creating the objects for later use (as you can see from the code -- they are created in main.)

Hogan
  • 69,564
  • 10
  • 76
  • 117