Factory method 00:00:00 Make a prediction You will locate the construction step inside a fixed workflow and show what changes when a second product type arrives. A CSV import and a JSON import read different formats but follow the same validation and storage steps. Which operation should an extension override? Let the extension create or supply the appropriate reader. The common workflow still decides when to read, validate and store, so adding JSON need not duplicate the import sequence. 00:00:33 The candle workshop A candle workshop has one routine for pouring, cooling and packing. Each branch chooses a mould at the marked step; a hill workshop makes one shape and a harbour workshop another. The routine is the creator's workflow, the chosen mould is the product, and the marked step is the factory method. The rest of the routine depends on the product contract. Factory Method is a creational pattern. A base class, the creator, contains a workflow that needs an object. It gets the object from a method of its own, and subclasses override that method. So the base class uses an object whose class it does not know. A constructor call names a class. Code full of them can not be extended without being edited, and can not be tested without the real thing. 00:01:22 Let a parser choose its reader An import workflow opens a reader, validates rows and stores them. CSV and JSON imports need different readers but the surrounding sequence is the same. The workflow calls a factory method rather than constructing a CSV reader directly. A CSV importer overrides that method to supply its reader. Add a JSON importer and run both through the same validation logic. The extension is a choice inside the creation step, not a second copy of the entire import workflow. Hand a CSV importer one comma-separated row and a JSON importer one object. Each creator supplies its own reader while the workflow still asks for one record. Then feed malformed input to both. The shared contract must describe how failures reach the caller. 00:02:14 A factory method needs a reason A small menu with two permanently fixed drink types can use a simple creation function. If a framework owns a larger workflow and clients should customize which product it creates, an overridable creation method becomes useful. Ask who controls the workflow before proposing inheritance. Imagine two drinks with fixed recipes and no shared workflow that subclasses need to extend. A function choosing between the two may already express the full requirement. Add a factory method when the variation has a real customization point, not merely because two object types exist. Overriding the entire import just to choose a reader lets the new variant skip validation. Keep the extension point narrow. A constructor or simple function is enough when there is no framework-owned workflow with a varying creation step. A Java collection supplies the iterator used to traverse it. The loop asks for the next element without knowing how the collection stores its contents. A list and a set can therefore create different iterators for the same traversal interface. The caller still needs the collection’s ordering and mutation guarantees. 00:03:27 Try an interview follow-up May a factory method return a cached object? Yes, also one from a pool or a copy of a prototype. But the contract must say new or shared. If the product is mutable or closable, sharing breaks callers that think it is theirs. A lease object solves that: closing it returns the object to the pool. An importer creates a parser for a comma-separated file. A second importer accepts a different format. Let the shared import workflow call a factory method when it needs its parser. Trace which subclass chooses the product and which code still owns the import steps. A simple factory function may suffice if workflow subclassing adds nothing. 00:04:13 What to take with you Write the steps once, in a creator. Where the steps need an object, call a method that subclasses override. Clients see the creator and the product interface, so a new kind is a new product, a new creator and one line of choice. Then ask whether you need the tree at all. If a value chooses, use a simple factory or a sealed registry. If only the object differs, pass a function. Keep the promises of creation in writing: ready, new or shared, cleaned up on failure, and never called from a constructor.