Template method pattern 00:00:00 Make a prediction You will protect a shared operation sequence while allowing one step to vary, and test the invariant that every subclass must preserve. A JSON importer overrides the whole import routine and writes records before validating them. Which part should have been customizable? Parsing should vary while the surrounding read, validate and store order stays controlled by the common workflow. A malformed JSON record must not reach storage merely because a subclass rearranged the sequence. 00:00:33 The screen-printing frame A screen printer always positions the sheet, applies colour and lifts the frame. A different stencil changes the design without moving the drying step before the ink. Define the stable algorithm in the template operation. Offer hooks only where variation is intended; keep the order enforcing correctness under the template's control. Template method is a behavioral design pattern. A base class has a method with the skeleton of an algorithm. The method calls steps. Some steps are written in the base class, others are left to subclasses. It removes copies. Steps that are equal are written once, and a new rule reaches all variants. 00:01:14 Change one step of an import An import must read input, parse records, validate them and store valid results. If CSV and JSON implementations copy this sequence, one may accidentally skip validation. Keep the order in a template method. Subclasses provide parsing but the shared workflow still performs validation afterwards. Feed malformed data to both importers and verify storage receives no invalid records. The test protects the sequence that stays fixed across formats. Provide a parser that returns one invalid row. Parsing varies by file type; rejection before commit is a shared rule. Confirm the database remains unchanged for both formats. Allowing subclasses to override the whole import method would make that ordering guarantee optional. 00:02:08 Keep extension points narrow A report generator always opens a document, writes content and closes it. A hook can customize content. If a subclass can override the entire generator and omit cleanup, the template no longer protects the invariant. Limit the overridable operations or use composition. Cause the document-writing hook to fail after opening a file. A base-owned cleanup step must still run. If every subclass is expected to remember to close manually, the shared lifecycle was only a convention. A focused test can assert that the failed run released its resource. A base class that calls an overridable method from its constructor can invoke subclass behaviour before the subclass is initialized. Choose hooks at safe lifecycle points. A composed pipeline or injected parser can preserve the same sequence without requiring inheritance. Java AbstractList. A skeletal list. For an unmodifiable list the documentation asks for get and size only, the rest is built on them. HttpServlet. A subclass overrides at least one method, usually doGet, doPost, doPut or doDelete. service dispatches the request to them. 00:03:26 Try an interview follow-up Can one instance run in two threads? One template instance can safely serve overlapping runs if the implementation keeps all per-run state in arguments or a fresh context. A mutable field containing this run's input lets a second run overwrite the first. If the framework requires such fields, give each execution a separate instance. A shared import workflow reads rows, validates them and commits them. Two formats parse their rows differently. Let subclasses supply parsing while the workflow controls the order. Give the second parser one invalid row and check that the workflow prevents the commit. This test protects the part subclasses must preserve. 00:04:12 What to take with you Line up the variants. Equal rows become private fixed steps, different rows abstract steps, optional rows hooks with a safe default. One public final method holds the order. The base class calls the subclass, never from a constructor, and treats each step as foreign code: cleanup in finally, data through arguments, a written contract.