...

View Full Version : design pattern for this case ??



PHPycho
05-18-2008, 05:08 AM
Hello forums!!
I would like to know which design pattern would be useful in the following case:
There are many modules and each module consists of many actions(multi stage actions). for example:
Module A consists of action1.1, action1.2, action1.3 etc.
Module B consists of action2.1, action2.2, action2.3 etc.
The actions can be sequential(For example:action1.1-> action1.2-> action1.3) or can be altered as per preferences( For example:action1.1-> action1.3-> action1.2) within a module.

How this can be accomplished using OOP design pattern or else? Any ideas ??
Thanks in advance for the valueable help.

PHPycho
05-18-2008, 07:01 AM
knock knock !! any example please?

Fou-Lu
05-19-2008, 02:58 AM
I don't understand what it is you are looking for tbh. For a module based idea, anything that belongs within a single module should be placed within that module or object. The difference always comes to interaction between these objects, that depends on what you like to do.
I prefer using a module factory pattern, and forcing each module to extend from a base module or implement a module interface. This allows me to control what Module A can access from Module B. The next problem is load orderings, what if Module A depends on Module B, and Module B depends on Module A. Planning can always be difficult to use.

In either case, what you are likely looking for is a concept of a delegate / function pointer / callback routine. PHP refers to its function pointers as callbacks, and unfortunately you cannot typecast for a specific callback (that I have been able to determine anyway). However, you can pass in the processing function as a call to another function:


function double($num)
{
return $num * 2;
}
function square($num)
{
return $num * $num; // or pow it, or whatever you like to do
}

function calculate($num, $func)
{
if (!function_exists($func))
{
throw new Exception('Function provided does not exist!');
}
return call_user_func($func, $num); // I think you can actually use $func($num) in newer versions of php
}

$num = 2;
$calculteSquare = calculate($num, 'square');
$calculateDouble = calculate($num, 'double');

Does that answer what you are looking for? It takes planning to use function pointers to your advantage, but they will help you in the long run (especially if you are running a factory)

PHPycho
05-20-2008, 09:40 AM
- Are modules classes and actions methods?
-- yes
- Do they need additional Parameters?
-- may or may not
- Are there requirements to the module configuration?
-- Class runs the methods as sequence of methods(like i stated: action1.1-> action1.2-> action1.3) in a default but it can be configureable also

Hope this may help.

Fou-Lu
05-21-2008, 04:37 AM
Now you have me confused.

As I said, I'm not certain what you are looking for in specific.
No, modules don't technically need to be objects, but they are far more portable that way.
Parameters are irrelevant in php, much like perl. Run your parameter lists and build them off of func_get_args(), and you can use whatever you want for parameters without giving an official prototype for them.
With dynamic function choices, a delegate is the best route - they will be difficult to implement in combination with a func_get_args() type of function though, and can lead to several vulnerabilities if you try. Choose one or the other.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum