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

14. Command & Chain of Responsibility in Java | Undo Operations & Request Validation

Think Make Explore

0:00 / 0:00

14. Command & Chain of Responsibility in Java | Undo Operations & Request Validation

14 просмотров · 1 день назад
Think Make Explore
7 подписчиков
14 просмотров · 1 день назад
An editor needs undo while a request pipeline needs ordered checks. Model commands and reversible state separately from handler chains and short-circuiting. Episode 14 of 32 | Low-Level Design: From Fundamentals to Interview Practice Original Java 21 lesson distinguishing Command with guarded undo from Chain of Responsibility with ordered short-circuit checks. A versioned document prevents stale undo from overwriting newer changes; a separate request validator stops at the first rejection. Complete source, assertions, prediction pause and actual console behavior are included. Expanded teaching edition: dedicated source-bound purpose, input/result, state/effect, failure, collaboration and concrete-example explanations for every explicitly declared type, constructor and method. Complete source and actual execution retained; no fixed-duration target. Original script, code, and diagrams. Narration generated locally with the speaker's authorization. Learning references: Java 21 language and library reference: https://docs.oracle.com/en/java/javas... Chapters 0:00 Welcome and what we will cover 0:13 The problem: reversible actions and ordered checks are different jobs 0:48 Specify execution and undo preconditions 1:26 Define the limits of this example 2:08 Identify command, receiver, request and handlers 2:44 Separate receiver ownership from chain routing 3:21 Use Command and Chain for their distinct reasons 4:03 Editing: separate action history from request decisions 5:29 Document: one receiver owns both visible text and revision identity 6:52 Document construction: nonnull text with an implicit zero revision 8:14 Document.text: observe content without exposing a writable buffer 9:30 Document.version: expose the revision token, not an undo position 10:47 Document.replace: validate and calculate before committing both fields 12:17 Command: expose action and reversal without promising universal undo 13:35 Command.execute: invocation is separate from construction 14:52 Command.undo: ask for a reversal under implementation-specific guards 16:11 Append: retain configuration and lifecycle metadata beside the receiver 17:44 Append construction: retain intent without capturing history too early 19:12 Append.execute: capture current text and mark success only after replacement 20:44 Append.undo: restore old content only while the saved revision is current 22:10 Request: supplied context flags and a nonnegative count 23:29 Request construction: reject impossible counts, not policy outcomes 24:49 Check: one optional rejection per handler 26:10 Check.reject: empty is a local pass, not final acceptance 27:35 Decision: a plain result record with no custom consistency guard 28:59 validate: snapshot the configured order and return the first rejection 30:35 EditingTest: distinguish lifecycle safety from chain ordering 32:07 EditingTest.check: count actual state and routing assertions 33:28 EditingTest.rejects: prove a rejected operation rather than suppress it 34:52 EditingTest.main: nineteen observations across two separate mechanisms 36:23 EditingDemo: show two outcomes without implying a combined transaction 37:41 EditingDemo.main: preserve the newer edit and skip the later check 39:13 Complete receiver, command and generated value inventory 41:02 Revision history explains why matching text does not authorize older undo 42:33 Measured coverage and the chain's trust boundary 44:05 Editing.java: lines 1-9 45:37 Editing.java: lines 10-17 47:46 Editing.java: lines 18-25 49:30 Editing.java: lines 26-33 51:24 Editing.java: lines 34-41 53:23 Editing.java: lines 42-49 55:02 Editing.java: lines 50-57 57:06 Editing.java: lines 58-61 57:35 EditingTest.java: lines 1-9 58:44 EditingTest.java: lines 10-17 59:36 EditingTest.java: lines 18-25 1:00:39 EditingTest.java: lines 26-33 1:01:47 EditingTest.java: lines 34-41 1:03:01 EditingTest.java: lines 42-49 1:03:57 EditingTest.java: lines 50-57 1:05:03 EditingTest.java: lines 58-65 1:06:50 EditingTest.java: lines 66-69 1:07:15 EditingDemo.java: lines 1-9 1:08:22 EditingDemo.java: lines 10-17 1:09:52 EditingDemo.java: lines 18-25 1:11:09 EditingDemo.java: lines 26-33 1:12:55 EditingDemo.java: lines 34-36 1:13:02 Predict the result before the demonstration 1:13:26 Actual output: guarded undo and first-rejection routing 1:15:02 Interview recap: guarantees, limits, and follow-ups Quick navigation 0:00 welcome 0:13 problem 0:48 requirements 2:08 model 2:44 relationships 3:21 oop patterns 4:03 inventory 44:05 code 1:13:02 demo 1:15:02 recap