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
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.
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.
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?
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/
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
}
what is the actual "challenge"? design *what*?
$--
September 14th, 2006 8:01am
There has to be more to it than that.
son of parnas
September 14th, 2006 10:21am
Would you people stop doing comp sci homework, please?
Because it's too tempting to post my own homework if you don't stop.