O'Reilly logo

Design Patterns in Java™, Second Edition by William C. Wake, Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

24 Command

THE ORDINARY WAY to cause a method to execute is to call it. There may be times, though, when you cannot control the timing of or the context in which a method should execute. In these situations, you can encapsulate a method inside an object. By storing the information necessary for invoking a method in an object, you can pass the method as a parameter, allowing a client or a service to determine when to invoke the method.

The intent of the COMMAND pattern is to encapsulate a request in an object.

A Classic Example: Menu Commands

Toolkits that support menus usually apply the COMMAND pattern. Each menu item is outfitted with an object that knows how to behave when the user clicks the item. This design keeps GUI logic ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required