I am creating an application that the user will be able to setup a series of functions to run on an object (image manipulation stuff).
Each different function has either no configurable options, or its own uniques set of options.
This is hard to explain, so let me show a simple example:
- Image opened (always happens)
- Remove border (two options)
- Despeckle image (5 options)
- Crop image (10 options)
- Save image
The problem is that I need to allow the user to create a custom series of cleanup functions that will be used to process images in bulk.
What is the best way to go about this?
I’d highly recommend you look into the Command Pattern:
Essentially, this involves creating “Command” subclasses for each type of action the user can perform – and pushing them unto a stack once the command has been executed.
Each command knows how to “do” itself, and also “undo” itself.
Thus, undo is a relatively straight forward process of popping commands off the stack, and calling the undo method on them.
Because each instance of a Command can contain it’s own state (“options”), you can create exactly the commands you want to use ahead of time, and “batch” them to get the result you are looking for.
pseudo-code:
Later on:
If you really want, you could make it a little more involved, and make all of the command definitions serializable – allowing you to create a list of them and read in the list and execute them later.