Перейти к содержимому

Command Pattern in Java - Undoable Cart Edits

Jayasekhar K

0:00 / 0:00

Command Pattern in Java - Undoable Cart Edits

0 просмотров · 5 дн. назад
Jayasekhar K
2 подписчика
0 просмотров · 5 дн. назад
Command design pattern in Java 21: each request is wrapped up as an object, so it can be stored, queued, logged and undone. Explained by building undo and redo for a shopping cart in an online store. We start with the problem — a hand-rolled undo that writes down what the customer asked for, so undoing "add 2 headphones" deletes the 3 they already had and undoing a coupon takes away the discount they never touched — and end with edits reified as objects that capture the state they need inside `execute`, an invoker holding two stacks of an interface it knows nothing about, a new kind of edit added without reopening a single working file, an audit trail that came free, and an honest look at what the pattern costs you. Also covers when not to use it, and how Command differs from Memento. No prior design-pattern knowledge needed. Full source code and written notes are in the repository. CHAPTERS 00:00 Introduction 01:00 The Scenario 01:48 The Obvious First Move 02:33 The Naive Approach — The Note Records the Request 03:40 Why That Hurts 04:16 The Command Pattern 05:06 Everyday Analogy: The Order Slip 05:55 The Roles 06:57 The Command — Three Methods 07:39 The One That Is Not Trivial to Reverse 09:08 The Invoker, Which Knows Nothing 10:07 The Test That Proves It 11:09 Running It 12:04 What to Remember 13:36 Thanks for Watching SOURCE CODE, WRITTEN NOTES AND AN INTERACTIVE ANIMATION github.com/jk-boot-up/java-design-patterns/tree/main/gradle-java/behavioural/command-pattern WHAT YOU NEED FIRST Java 21 and a working knowledge of classes and interfaces. No prior design-pattern knowledge is assumed. The full prerequisites are in docs/prerequisites.md in the repository.