Topic: 关于command的模式中Invoker对象?

  Print this page

1.关于command的模式中Invoker对象? Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-01 15:40

如果把Invoker对象去掉,可不可以呢?

直接调用 command 的 execute方法,那位解释下Invoker对象的作用呢?

2.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: wes109
Posted on: 2004-12-01 18:30

在看《设计模式》?

Command是很简单的, 只要一个interface


public interface Command {

void execute();

}


然后是各个具体的实现,如:

public class ConsolePrintCommand {

public void execute() {
System.out.println("This is a ConsolePrintCommand!");
}

}


junit的Test和TestCase就是Command模式的应用
Test 是 Command interface
TestCase是ConcreatCommand

我觉得理解设计模式,首先要了解每个模式的意图
Command模式的意图是将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录日志,以及支持可撤销的操作.

画图软件的工具箱就是一个很好的例子:
用户选择不同的工具,就保存一个不同的Command对象
在画板上拖拽的时候,调用对象的execute方法
执行画圆或画方的操作

在《设计模式》中,client将command对象保存在invoker中,然后调用execute()

我觉得这只用一种应用而已,invoker和此模式没什么关系

3.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-01 20:17

看过几遍,这次又翻翻又有体会了,呵呵

我认为Command是抽象类里应该有些变量,对象什么的,不然和普通的

继承有什么区别?应该针对同样的环境具体对象会有自己的Execute 方法,另外就是这个Invoker对象的作用了......

4.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-12-02 00:01

invoker is the real client of commands, it invokes commands. The client box in the chart is the creator, similar to Spring object container.

接口里不应该有变量, to make maximal reuse. 变量 should be implementation specific, they have their setters. There are two logic flows here, one is the request flow, a request comes, a command response. Another flow is the creation of commands. So we have two dimensions.

2 cents.

5.Re:关于command的模式中Invoker对象? [Re: wes109] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-02 08:29

wes109 wrote:

画图软件的工具箱就是一个很好的例子:
用户选择不同的工具,就保存一个不同的Command对象
在画板上拖拽的时候,调用对象的execute方法
执行画圆或画方的操作

在《设计模式》中,client将command对象保存在invoker中,然后调用execute()

我觉得这只用一种应用而已,invoker和此模式没什么关系


在上面这个例子中我会把画板对象加到Command抽象类中,不知道各位怎么想?

6.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-12-02 23:14

画板 is in the Receiver/Action class, the Action class is in the Command class.

One more indirection here is to reuse the Action class, which could be a button or a MenuItem.

7.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: wes109
Posted on: 2004-12-03 09:10

具体的画图动作是由画板对象作出的
画板对象和Command对象之间存在关联关系
画板对象保存在具体的Command对象中
不同的Command对象会调用画板对象的不同方法
drawLine() or drawRect()
工具和Command对象存在对应关系

点击不同的工具条
保存对应的Command Object为Current Command Object
在面板上拖拽的时候
调用Current Command Object的 execute()方法
此方法调用画板对象的draw()方法

选的是Line Tool就是DrawLineCommand
在execute中执行画板.drawLine()

大体这样,具体情况可能要复杂的多

8.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-03 14:17

我的代码,各位给点意见:

public class ClientB
{
public ClientB()
{
}

public static void main(String args[])
{
DrawPane pane = new DrawPane();
LineShape line = new LineShape();
CircularShape circular = new CircularShape();
QuadrateShape quadrate = new QuadrateShape();

pane.drawObject(line);
pane.drawObject(circular);
pane.drawObject(quadrate);

}

}

interface ShapeObject
{
public String getShape();
public void draw();
}

class DrawPane
{
java.util.ArrayList shapeList = new java.util.ArrayList();

public void drawObject(ShapeObject so)
{
shapeList.add(so);
this.repaint();
System.out.println("--------------------------");
}

public void repaint()
{
for(int i=0;i<shapeList.size();i++)
{
System.out.print(i+":");
((ShapeObject)shapeList.get(i)).draw();
}

}

}

class LineShape implements ShapeObject
{
public String getShape()
{
return "画线";
}
public void draw()
{
System.out.println(this.getShape());
}
}

class CircularShape implements ShapeObject
{
public String getShape()
{
return "画圆";
}

public void draw()
{
System.out.println(this.getShape());
}

}

class QuadrateShape implements ShapeObject
{
public String getShape()
{
return "画方";
}

public void draw()
{
System.out.println(this.getShape());
}

9.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-03 14:28

就是用不上Invoker对象

10.Re:关于command的模式中Invoker对象? [Re: jameszhang] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-12-03 15:30

I think your DrawPane is the Invoker here. It is a command manager or command aggregator. Your implementation has one drawback: It is not a good idea to let command object implements shap object(the receiver or response object). this way the response object's interface is exposed to client (tight-coupled). it is better to embed the implementation details for command object, or even further maybe we can use bridge pattern to externalize the command receiver's implementation.

11.Re:关于command的模式中Invoker对象? [Re: WeiterWay] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-03 16:02

WeiterWay wrote:
I think your DrawPane is the Invoker here. It is a command manager or command aggregator. Your implementation has one drawback: It is not a good idea to let command object implements shap object(the receiver or response object). this way the response object's interface is exposed to client (tight-coupled). it is better to embed the implementation details for command object, or even further maybe we can use bridge pattern to externalize the command receiver's implementation.

改了,看看


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923