I'm writing a painting program. My basic classes are:
class Workspace { Bitmap b; List<Command> undoList; }
class Command { void execute(); }
class ClearScreen extends Command
class BlurEffect extends Command
class View { Bitmap screen; }
class Interface
The workspace object holds the bitmap which represents the program state. The Command class represents the Command pattern for executing commands on the workspace where undoing works by resetting the state of the workspace and replaying old commands. The interface object links button presses from the user to commands and the view renders the workspace state to the screen bitmap.
My issue is with representing commands. The ClearScreen command is simple; it just tells the workspace to fill the bitmap with white and it happens instantly. The BlurEffect command is more complex; blurring takes a parameter for how much to blur the screen, execution can take a bit of time and the user usually wants to try a few blur parameters before selecting one (i.e. they need to preview what the blur effect will look like before committing). How can I modify the above to support this kind of previewing?
The best I can come up with is to extend Command with something like:
class BlurCommand extends Command
{
void setBlurAmount(float x) ...
// View can use this to render a preview to the
// screen bitmap, where the workspace bitmap isn't modified in the process
void preview(Workspace w, Bitmap b)
void execute() // apply blur effect to workspace
}
So the idea is, in the interface, clicking the "blur" button creates a new BlurCommand object, the "render the screen" method in View will then start calling the "preview" method to render the screen and "execute" is only called when the user wants to apply the effect.
Is this the cleanest way I can do this? I'm trying to stick to the Model-View-Controller design and don't want my preview behaviour complicating things.