Development

Java의 명령 디자인 패턴 :이 기사는 명령 설계 패턴을 다룰 수 있으며, 이는 요청을 객체로 캡슐화하여 다른 요청, 대기열 또는 로그 요청으로 클라이언트를 매개 변수화하고 undoable 작업을 지..

sonpro 2023. 3. 12. 14:10
반응형

Command design pattern

Command Design Pattern in Java

In software development, design patterns are reusable solutions to common problems that arise during software development. The Command design pattern is one of the behavioral design patterns that encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. In this article, we will explore the Command design pattern in Java and how it can be used to improve the design of your software.

Overview of the Command Design Pattern

The Command design pattern is a behavioral pattern that decouples the sender of a request from the receiver of the request. It does this by encapsulating a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. The Command design pattern consists of four main components:

  • Command: This is an interface that defines the execute() method that encapsulates the receiver's action.
  • ConcreteCommand: This is a class that implements the Command interface and defines the receiver's action.
  • Invoker: This is a class that invokes the execute() method on the Command object.
  • Receiver: This is a class that performs the action defined by the ConcreteCommand.

The following UML diagram shows the structure of the Command design pattern:

Command Design Pattern UML Diagram

Example of the Command Design Pattern in Java

Let's consider an example of a simple text editor that allows users to perform various operations such as copying, pasting, and deleting text. We can use the Command design pattern to implement these operations in a way that decouples the user interface from the text editor's implementation.

First, we define the Command interface:

public interface Command {
    void execute();
}

Next, we define the ConcreteCommand classes that implement the Command interface:

public class CopyCommand implements Command {
    private TextEditor textEditor;

    public CopyCommand(TextEditor textEditor) {
        this.textEditor = textEditor;
    }

    public void execute() {
        textEditor.copy();
    }
}

public class PasteCommand implements Command {
    private TextEditor textEditor;

    public PasteCommand(TextEditor textEditor) {
        this.textEditor = textEditor;
    }

    public void execute() {
        textEditor.paste();
    }
}

public class DeleteCommand implements Command {
    private TextEditor textEditor;

    public DeleteCommand(TextEditor textEditor) {
        this.textEditor = textEditor;
    }

    public void execute() {
        textEditor.delete();
    }
}

In the above code, we have defined three ConcreteCommand classes: CopyCommand, PasteCommand, and DeleteCommand. Each of these classes encapsulates a specific operation that can be performed on the text editor.

Next, we define the Invoker class that invokes the execute() method on the Command object:

public class Menu {
    private List<Command> commands = new ArrayList<>();

    public void addCommand(Command command) {
        commands.add(command);
    }

    public void executeCommands() {
        for (Command command : commands) {
            command.execute();
        }
    }
}

In the above code, we have defined a Menu class that maintains a list of Command objects. The addCommand() method is used to add a Command object to the list, and the executeCommands() method is used to execute all the commands in the list.

Finally, we define the Receiver class that performs the action defined by the ConcreteCommand:

public class TextEditor {
    private String text = "";

    public void copy() {
        // Copy the selected text to the clipboard
    }

    public void paste() {
        // Paste the text from the clipboard at the current cursor position
    }

    public void delete() {
        // Delete the selected text
    }
}

In the above code, we have defined a TextEditor class that performs the copy, paste, and delete operations.

Now, we can use the Command design pattern to implement the text editor's operations in a way that decouples the user interface from the text editor's implementation:

public static void main(String[] args) {
    TextEditor textEditor = new TextEditor();

    Command copyCommand = new CopyCommand(textEditor);
    Command pasteCommand = new PasteCommand(textEditor);
    Command deleteCommand = new DeleteCommand(textEditor);

    Menu menu = new Menu();
    menu.addCommand(copyCommand);
    menu.addCommand(pasteCommand);
    menu.addCommand(deleteCommand);

    // Execute the commands
    menu.executeCommands();
}

In the above code, we have created a TextEditor object and three Command objects: CopyCommand, PasteCommand, and DeleteCommand. We have added these commands to a Menu object and executed them using the executeCommands() method.

Conclusion

The Command design pattern is a powerful pattern that can be used to improve the design of your software. It decouples the sender of a request from the receiver of the request and encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. In this article, we have explored the Command design pattern in Java and how it can be used to implement a simple text editor.

반응형