【发布时间】:2011-03-16 22:36:35
【问题描述】:
我正在编写一个绘画程序。我的基本课程是:
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
工作区对象保存代表程序状态的位图。 Command 类表示用于在工作空间上执行命令的命令模式,通过重置工作空间的状态和重放旧命令来撤消工作。界面对象将用户按下的按钮链接到命令,视图将工作区状态呈现到屏幕位图。
我的问题是表示命令。 ClearScreen 命令很简单;它只是告诉工作区用白色填充位图,它会立即发生。 BlurEffect 命令更复杂; blurring 需要一个参数来确定屏幕模糊程度,执行可能需要一些时间,并且用户通常希望在选择一个模糊参数之前尝试一些模糊参数(即他们需要在提交之前预览模糊效果的样子)。如何修改上述内容以支持这种预览?
我能想到的最好的办法是用类似的东西扩展 Command:
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
}
所以思路是,在界面中,点击“blur”按钮会创建一个新的BlurCommand对象,View中的“render the screen”方法会开始调用“preview”方法来渲染屏幕并“execute”仅在用户想要应用效果时调用。
这是我能做到的最干净的方法吗?我正在尝试坚持模型-视图-控制器设计,并且不希望我的预览行为使事情复杂化。
【问题讨论】:
标签: java user-interface design-patterns architecture interface