Note that there are some explanatory texts on larger screens.

plurals
  1. POCommand object pattern wannabe or the real thing?
    text
    copied!<p>The command object pattern is one that I still haven't been able to truly grasp and I found an implementation in the code I'm currently working on so I studied it long and hard to see if I could finally get it with a real world example. The problem is that I am sure this is not properly implemented and it is just an attempt by someone who just read about it and thought it made sense here.</p> <p>Allow me to show it to you (for confidentiality reasons it will be greatly simplified but I'll do my best to show the main concepts):</p> <pre><code>public class CommandOne { public CommandOne(Manager manager, MyForm form) { m_manager = manager; m_form = form; } public void Execute() { m_manager.CommandOne(m_form); } } public class CommandTwo { public CommandTwo(Manager manager, MyForm form) { m_manager = manager; m_form = form; } public void Execute() { m_manager.CommandTwo(m_form); } } </code></pre> <p>The first thing that strikes me as odd is that these two classes are not inheriting from any abstract class nor implementing a common interface.</p> <p>The code that uses these commands is as follows:</p> <pre><code>public class MyForm : System.Windows.Forms.Form { public MyForm(Manager manager) { m_manager = manager; } private void SomeMethod() { .... var cmd = new CommandOne(manager, this); cmd.Execute(); ... } private void OtherMethod() { .... var cmd = new CommandTwo(manager, this); cmd.Execute(); ... } } </code></pre> <p>So the way I see it, this form is absolutely coupled to all the classes involved except the manager which is being injected to it through its constructors. So with this code I really don't see any benefit of creating the "command" classes which basically are just delegating the call to the manager's methods since the form is instantiating them when it needs them and calling the execute method right afterwards.</p> <p>So could someone please explain what pieces, if any, is this implementation missing to truly be a command object pattern and, although it might be too subjective, what would be the benefit to implement it in this case?</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload