How to create a command template using another listener?

I have a window that works as a Visual Studio designer. Each document has two types:

  • View source
  • View constructor.

I have a toolbar that can issue different commands. The toolbar button has a CommandId line property that stores the command identifier, for example:

  • Cut, copy, paste;
  • Paste grid,
  • Auto format
  • ...

I had a problem creating a command template where the execution of the command differs depending on the view.

For an obvious example, the copy command will copy the selected text in the Source window, but copy the selected control in the Design view.

commandId CopyCommand, , , .

  • , (, , CopyCommand, SourceCopyCommand DesignCopyCommand, )?

  • , , ?

+3
2

Strategy Pattern State Pattern. , , - . IWindowEditCommands. . - , . . , , commandState, , .

. Command, Commands, .

public interface IWindowEditCommands
{
    string Copy();
    string Cut();
    void   Paste(string buffer);
}

public class Editor implements IWindowEditCommands
{
    private IWindowEditCommands commandState;

    //constructor and other methods

    public void SwitchToSourceView()
    {
        //do stuff
        commandState = new EditorSourceViewStrategy(this);
    }

    public void SwitchToDesignView()
    {
        //do stuff
        commandState = new EditorDesignViewStrategy(this);
    }

    //IWindowEditCommands methods
    public string Copy() { return commandState.Copy(); }
    public string Cut()  { return commandState.Cut(); }

    public void Paste(string buffer) { commandState.paste(buffer); }

}

public class EditorSourceViewStrategy implements IWindowEditCommands
{
    private Editor editor;

    public EditorSourceViewEditCommands(Editor editor)
    {
        this.editor = editor;
    }

    public string Copy() {return...} //return the selected source from the source view
    public string Cut()  {return...} //return the and delete the selected source from the source view
    public void Paste(String buffer) {} //insert the buffer text at the insertion point
}

public class EditorDesignViewStrategy implements IWindowEditCommands
{
    private Editor editor;

    public EditorDesignViewEditCommands(Editor editor)
    {
        this.editor = editor;
    }

    public string Copy() {return...} //return the selected TAGS from the source view
    public string Cut()  {return...} //return the and delete the selected TAGS from the source view
    public void Paste(String buffer) {} //insert the buffer text at the insertion point
}
+2

, ICommandContext ( , . " ", . 234). , , . Command , , CopyCommand , "Ctrl + C", , , ...

public interface ICommandContext 
{
void Cut();
void Copy();
void Past();
void AutoFormat(); 
}

public SourceView : ICommandContext
{
    public void Cut()
    {
        // Do stuff here...
    }
}

public class CopyCommand : Command
{
    public override Execute(ICommandContext context)
    {
        context.Copy();
    }
}

( dofactory):

, , , .

0

Source: https://habr.com/ru/post/1723159/


All Articles