Sunday, 9 February 2014

Command Design pattern - Implementation [Menu]

Master Practical Java Architecture & Design Patterns!

Subscribe to Ram N Java for clean coding tutorials, GoF design patterns, and real-world system implementations!

🔔 Subscribe to Ram N Java

The Menu Invocation Challenge

In graphical user interfaces (GUIs) or interactive console menus, user interactions such as clicking a menu item or pressing a shortcut must trigger specific business actions (such as opening a file, saving changes, or printing a document).

Hardcoding receiver logic directly inside UI menu components tightly couples the user interface to domain actions. If you tie menu items directly to specific classes, reusing actions across different UI triggers (like toolbar icons, context menus, or keyboard shortcuts) becomes difficult and error-prone.

Architecture of the Command Pattern Solution

The Command Design Pattern solves this tight coupling by turning user requests into standalone objects. In this menu implementation, the system is structured around four core participants:

  • Command Interface: Declares an execution method (typically execute()) common to all menu commands.
  • Concrete Commands: Encapsulate a binding between an action and a receiver. When execute() is called, the command invokes the appropriate method on the receiver object.
  • Invoker (Menu / MenuItem): Holds a reference to a command and triggers it when a user action occurs, remaining completely agnostic of the receiver's underlying logic.
  • Receiver: Contains the actual business logic to perform the requested operation (such as document editing, file storage, or rendering).

Key Architectural Benefits

Applying the Command pattern to menu systems provides distinct design advantages:

  • Decoupled Invoker and Receiver: The menu component only knows how to execute a command, without depending directly on concrete receiver classes.
  • Open/Closed Principle: Add new menu items and custom actions by introducing new command classes without altering existing menu code.
  • Reusable Actions: Bind the same command instance to multiple UI controls (such as a top-level menu bar, a right-click context menu, and a keyboard shortcut).

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=04iqkBZe-JE

Click the below Image to Enlarge
Command Design pattern - Implementation [Menu]

Command Design pattern - Implementation [Menu]
Command Design pattern - Implementation [Menu] - Class Diagram

WordDocument.java

/*
 *  Receiver
 */

public class WordDocument
{
public void open()
{
System.out.println("Document Opened");
}

public void save()
{
System.out.println("Document Saved");
}

public void close()
{
System.out.println("Document Closed");
}

}

Command.java

public interface Command
{
public void execute();
}

OpenCommand.java

public class OpenCommand implements Command
{
private WordDocument wordDocument;

public OpenCommand( WordDocument wordDocument )
{
this.wordDocument = wordDocument;
}

@Override
public void execute()
{
wordDocument.open();
}
}

SaveCommand.java

public class SaveCommand implements Command
{

private WordDocument wordDocument;

public SaveCommand( WordDocument wordDocument )
{
this.wordDocument = wordDocument;
}

@Override
public void execute()
{
wordDocument.save();
}
}


CloseCommand.java

public class CloseCommand implements Command
{
private WordDocument wordDocument;

public CloseCommand( WordDocument wordDocument )
{
this.wordDocument = wordDocument;
}

@Override
public void execute()
{
wordDocument.close();
}
}


MenuOptions.java

/*
 *  Invoker
 */

public class MenuOptions
{
private Command openCommand;
private Command saveCommand;
private Command closeCommand;

public MenuOptions( Command open, Command save, Command close )
{
this.openCommand = open;
this.saveCommand = save;
this.closeCommand = close;
}

public void clickOpen()
{
openCommand.execute();
}

public void clickSave()
{
saveCommand.execute();
}

public void clickClose()
{
closeCommand.execute();
}
}

Client.java

public class Client
{
public static void main( String[] args )
{
WordDocument wordDocument = new WordDocument();
Command openCommand = new OpenCommand(wordDocument);
Command saveCommand = new SaveCommand(wordDocument);
Command closeCommand = new CloseCommand(wordDocument);
MenuOptions menu = new MenuOptions(openCommand, saveCommand, closeCommand);
menu.clickOpen();
menu.clickSave();
menu.clickClose();
}

}

Output

Document Opened
Document Saved
Document Closed

See also:

  • Command Design pattern - Introduction
  • Command Design pattern - Real time example [Hotel]
  • Command Design pattern - Real time example [Menu]
  • Command Design pattern - Class Diagram
  • Command Design pattern - Sequence Diagram
  • Command Design pattern - Object Creation and flow
  • Command Design pattern - Key points
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials