Commanding Attention — The Design Pattern

Sameeha Rahman
3 min readJul 13, 2019

Hey All! This week we will be looking into a behavioral design pattern, Command. If you can recall from my first post, a behavioral design pattern focuses on how classes and objects communicate with each other. The main focus of the command pattern is to inculcate a higher degree of loose coupling between involved parties (read: classes).

Uhhhh… What’s that?

Coupling is the way that two (or more) classes that interact with each other, well, interact. The ideal scenario when these classes interact is that they do not depend heavily on each other. That’s Loose coupling. So, a better definition for loose coupling would be, classes that are interconnected, making the least use of each other.

The need for this pattern arose when requests need to be sent without consciously knowing what you are asking for or who the receiver is.

In this pattern, the invoking class is decoupled from the class that actually performs an action. The invoker class only has the callable method execute, which runs the necessary command, when the client requests it.

Let’s take a basic real-world example, ordering a meal at a fancy restaurant. As the flow goes, you give your order (command) to the waiter (invoker), who then hands it over to the chef(receiver), so you can get food. Might sound simple… but a bit meh to code.

--

--

Sameeha Rahman
Sameeha Rahman

Written by Sameeha Rahman

Front End Developer at Switchd Ltd. | MSc in Computer Science @ QMUL (2022)

No responses yet