DELAPARRA
Index
Color
NL
Category: Design patterns

Command

Explained by Updated

What is Command?

Command: Encapsulate a request as an object, letting you parameterise methods, queue requests, log them, and support undoable operations. The Command pattern turns an action into an object. Instead of calling sendInvoice($invoice) directly, you create a SendInvoice object that holds everything needed to perform the action later. That opens up useful possibilities. Commands can be queued, logged, retried, scheduled or undone. A text editor stores each edit as a command, so it can undo them in reverse order.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Command

What should you keep in mind with Command?

Laravel's queued jobs are command objects. A GenerateReport job carries its data, goes onto a queue and is executed by a worker, possibly on another server. Artisan console commands follow the same idea. Keep commands small and serialisable, and make them safe to run twice. A job that runs again after a timeout should not send the same email twice.

At a glance

CategoryDesign patterns
Explained by
Updated

More Design patterns terms

All terms starting with C

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.