Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Is there a better approach to the problem? (many independent commands + many states)

Heya,

Here is a part of code im working on at the moment (it's a bit changed, but doesn't change the workflow at all): https://github.com/x64puzzle/daily-collections

Starting point is in the function call:
App\Templates\DailyCollectionProcess\DailyCollectionProcessTemplate::process(IDailyCollection $dailyCollection, array $dailyCollectionRequest).

The problem is following..

There are two possible commands (actions) to be taken, but there will be like two more added (but total number of commands shouldnt be more than 5-6, im quite confident) in a month or two, i assume.
And each of these commands have their own states (and transition from one to another)!
And all these states are available for all commands. But each command will have only some of the available states!

So, there are many possible commands and each command can have many possible states.
BUT, even the list of possible states is available across all commands, it does not mean all commands will have all these states!
Quite opposite, for example:

Available states: S1, S2, S3, S4
CommandA can be in state S1 and S2
CommandB can be in state S1, S3 and S4
CommandN can be in state Sn... (from the list of available states)


What i attempted to do so far, is to combine Command and State design pattern, as it looks something it could be used in the situation.

Why these two patterns?

Well, reasons:
1.) i will have many available commands/actions to execute
2.) i have no idea what these actions will be,
3.) im not quite sure when these actions will be called,
4.) each of these commands will have their own possible state, and when i say state, i mean transition from one to another as well (can be quite a long list of if..elseif statements, no thank you)

NOTE: When new commands and their states arrive, i WILL be able to control them in my program, like when to call new command and i will know what is the command going to do. I just do not know these information at this stage yet. So i have to be prepared.

The language is PHP, but it doesnt really matter. Not at all.

Do you have a better approach to the problem?
Or this one should work quite good?

Thank you for your time,


I dont know is it just me or Git removed all blank spaces between lines. All code is so tight lol wtf
Gopher


Possibly Related Threads...
Thread / Author Replies Views Last Post
by clovaksSep 6, 2023 05:52 AM 7 320 Sep 6, 2023 05:52 AM
Last Post: BiNBo
[Help] by kakarot007Apr 6, 2017 12:40 AM 6 291 Apr 6, 2017 12:40 AM
Last Post: foobar123
by GilesJun 30, 2016 04:18 AM 7 132 Jun 30, 2016 04:18 AM
Last Post: Giles