Reconciling assholes for nearly a decade.

Design challenge

Our project manager is having a design contest among the development teams. The prize is a 500$ (per person) for the team with the best solution.

Question: A task is defined by 'client-code'. That task is assigned to 'executing-code'.

First came up with a C-sytle design:

void f()
{
// do the job...
}

execute(void *task)
{
void (*pFunc)();
pFunc= (void(*)())task;   
pFunc();
}

Once written it is easy to replace f() with any other function. Replacing the executer is more complex and requires dealing with c-pointers (like if you wish to execute a function which receives parameters).

Our next design is C++.

class Command
{
public:
Command();
virtual ~Command();
virtual void Execute() = 0;
}

class ConcreteCommand:Command
{
void Execute()
{
}
private:
// data members
...
}

class Executer
{
f(Command *p)
{
  p->Execute()
}
}

Using this solution, the user has only to implement a 'concrete-command'. No pointers know how is needed.

What is your view of these designs?
Do you have any other to offer?

Laura
Permalink Laura 
September 14th, 2006 6:56am
In the C++ variant, why do you need the Executer class.
The base Command class can serve as the executing code.
Permalink Send private email Locutus of Borg 
September 14th, 2006 7:02am
If you are also using Lisp in your class, you could write the client to return a lambda expression which could be executed by the server.
Permalink Send private email jingalala jingalala ™ 
September 14th, 2006 7:10am
Borg, here is an example of using the Command.

main()
{
Command *p = new BorgCommand();
f(p); // pass to function
p->Execute; // call directly
}

f(Command *p)
{
  p->Execute()
}

How can the base class execute?
Permalink Laura 
September 14th, 2006 7:36am
In C#, I'd create an interface, then have the executing task implement the interface, and it would get loaded dynamically at runtime.  That way I could have it do anything that the parameters used in the interface allowed.

Here's an article that describes the process.

http://www.developerfusion.co.uk/show/4371/
Permalink xampl 
September 14th, 2006 7:41am
hehe she said "BorgCommand"

"How can the base class execute?"

Something like this:

class Command
{
  public:
    Command();
    virtual ~Command();
    virtual void Execute()
    {
      DoWork();
    }

  protected:
    virtual void DoWork() = 0;
}

class BorgCommand:Command
{
  // Override and implement the task
  void DoWork()
  {
  }
 
  private:
  // data members
...
}

main()
{
  Command *p = new BorgCommand();
  p->Execute; // call directly
}
Permalink Send private email Locutus of Borg 
September 14th, 2006 7:48am
what is the actual "challenge"? design *what*?
Permalink $-- 
September 14th, 2006 8:01am
man dlopen
  -tim
Permalink Send private email a2800276 
September 14th, 2006 10:20am
There has to be more to it than that.
Permalink son of parnas 
September 14th, 2006 10:21am
Would you people stop doing comp sci homework, please?
Permalink Send private email Aaron F Stanton 
September 14th, 2006 6:37pm
Why?
Permalink Send private email Locutus of Borg 
September 15th, 2006 6:28am
Because it's too tempting to post my own homework if you don't stop.
Permalink Send private email Aaron F Stanton 
September 15th, 2006 7:46am
Bring it on ;)
Permalink Send private email Locutus of Borg 
September 15th, 2006 8:16am

This topic is archived. No further replies will be accepted.

Other topics: September, 2006 Other topics: September, 2006 Recent topics Recent topics