开发者

Why does the client have no reference to the invoker in the Command pattern?

开发者 https://www.devze.com 2022-12-17 12:50 出处:网络
Link to command pattern Why does the client have no reference to the invoker when it has references to receivers and concretecommands?

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(s开发者_StackOverflow中文版sc);
}


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消