开发者

How can I solve this problem with C# classes?

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:

  1. Image op开发者_高级运维ened (always happens)
  2. Remove border (two options)
  3. Despeckle image (5 options)
  4. Crop image (10 options)
  5. 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:

  • http://www.codeproject.com/KB/architecture/Command_Pattern.aspx

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:

public class ImageEditor
{
     public Stack<Command> undoList = new Stack<Command>();
     public void executeCommand(Command command)
     {
           command.performAction(this);
           undoList.push(command);
     }
     public void undo()
     {
           undoList.peek().undoAction(this);
           undoList.pop();
     }
}

public interface ICommand
{
     void performAction(ImageEditor editor);
     void undoAction(ImageEditor editor);
}

public class CreateBorderCommand : ICommand
{
     public int BorderWidth { get; set; }

     private Border MyBorderBox { get; set; }

     public void performAction(ImageEditor editor)
     {
           MyBorderBox = new Border(BorderWidth, editor.frame);
           editor.addElement(MyBorderBox);
     }
     public void undoAction(ImageEditor editor)
     {
           editor.removeElement(MyBorderBox);     
     }
}

Later on:

ImageEditor editor = new ImageEditor();
editor.executeCommand(new CreateBorderCommand() { BorderWidth = 10 });
...

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.


This can be solved by one of the standard 'Gang Of Four' design patterns. You should spend a bit of time reading about the Command Pattern. This pattern allows you to encapsulate the operations performed on an object within classes that implement the same interface. In your example, each of your steps becomes a command. Once you have a common interface for your commands you can then compose them into Macros.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜