Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    design pattern for this case ??

    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.

  • #2
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    knock knock !! any example please?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    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)
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    - 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.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •