简体   繁体   English

命令模式混乱

[英]Command Pattern confusion

Command Pattern Says: 命令模式说:

Command Pattern allows you to decouple the requester of an action from the object that actually performs the action. 命令模式使您可以将操作的请求者与实际执行操作的对象分离。

Command decouples the object that invokes the operation from the one that knows how to perform it . 命令将调用操作的对象与知道如何执行该操作的对象分离。

But in all code examples which I found there is coupling between Client(Requester,Inovker,RemoteControl,Waitress,Sender) and the actual object (Reciver,TV,Garage,Coffe) 但是在我发现的所有代码示例中,Client(请求者,Inovker,RemoteControl,Waitress,发件人)与实际对象(Reciver,TV,Garage,Coffe)之间存在耦合

If you this question in the below Command pattern 如果您在以下命令模式中遇到此问题

Answer: Says : Your remote doesn't know that the TV exists , or anything about it, its just calling an interface/method on your Command object. 答:说:您的遥控器不知道电视是否存在 ,或者它是否有关,它只是在Command对象上调用接口/方法。

But I see Remote Class is aware of TV class and it (remote) is creating object of TV class. 但是我看到远程类知道电视类,并且它(远程)正在创建电视类的对象。 Hence there is association there by a relation ie A coupling already exists there. 因此,通过一种关系存在关联,即那里已经存在耦合。

Can you please help me to understand what I am missing. 您能帮我了解我所缺少的吗? Do I misunderstand what is meant by decoupling or some thing other ? 我会误解去耦是什么意思吗?

I am really confusing with definition. 我真的对定义感到困惑。

I think instead of TV it should be an interface like ITV 我认为应该使用ITV这样的界面来代替TV

public class TVOffCommand : Command    
{
    ITV tv;

    public TVOffCommand (ITV aTV)
    {
        this.tv= aTv;
    }

    #region Command Members

    public object Execute()
    {
        return tv.Off();
    }

    #endregion
}

and your remote should invoke something like below: 并且您的遥控器应调用以下内容:

ITV tv = new TV() ITV电视=新电视()

remote = new Remote() //your client remote = new Remote()//您的客户端

command = new TVOffCommand(tv); 命令=新的TVOffCommand(tv);

remote.command(command); remote.command(命令);

Have a look at link this provides the concept http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial 看看提供此概念的链接http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

I don't think that it make sense to wrap the example regarding TV and remote control into command design pattern. 我认为将有关电视和远程控制的示例包装为命令设计模式没有任何意义。 You just have TV and remote control. 您只有电视和遥控器。 Even if you would add interfaces, it does not make sense as there is small (or no) level of abstraction. 即使您要添加接口,也没有意义,因为只有很少的 (或没有)抽象级别。

The last sentence is the whole point of command design pattern. 最后一句话是命令设计模式的重点。 There must be some abstraction and unawarness. 必须有一些抽象和纯真。 Typically you have some system which accept executable command (using defined IF = contract) which does "something". 通常,您有一些系统可以接受可执行“某些操作”的可执行命令(使用已定义的IF =合同)。

It's up to your implementations of this IF , typically there is more implementations than one. 这取决于您对这个IF的实现,通常有不止一种实现。 What was my last use? 我最近的用途是什么?

We have scheduling system using Quartz framework. 我们有使用Quartz框架的调度系统。 We have wrapped it into some kind of our service. 我们已经将其包装到我们的某种服务中。 This service accepts our IF having perform method in a certain time - using quartz. 该服务接受我们的IF在一定时间内具有perform方法-使用石英。

We have two types of executable tasks - one was executed immediately and second was was rescheduled with additional time - because of other reasons out of scope of this question. 我们有两种类型的可执行任务-一种被立即执行,另一种被重新安排了额外的时间-由于此问题范围之外的其他原因。

Both types of tasks were executed despite what they do , despite they real executable code. 两种类型的任务尽管执行了什么 ,但都执行 ,尽管它们都是真正的可执行代码。 And the wrapping service has no knowledge what's going on there and both types of tasks have no knowledge regarding scheduling system. 包裹服务并不知道发生了什么事,并有两种类型的任务有关于调度系统没有知识

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM