Abstract factory 00:00:00 Make a prediction You will create compatible products as a family, then predict which changes are easy or expensive for the factory contract. A dark settings screen asks one factory for its button and a separate light factory for its dialog. Can the two individually valid objects still produce a wrong screen? Yes. They can disagree as a family. Select one factory for that screen and obtain both controls through it. Product interfaces alone do not necessarily enforce cross-product compatibility. 00:00:34 Noor furnishes the reading room A reading room is furnished from one catalogue. Choose forest or sea before ordering the desk, chair and lamp. A fourth room style is easy to order; adding a new furniture category requires updating every catalogue. A concrete factory produces a compatible family. Adding a new family implements an existing contract. Adding a new product kind changes the contract each family implements. The Abstract factory is a creational design pattern. An interface declares one creation method for each kind of product. Each class that implements it makes the products of one family. When related objects are created in many places, each place decides again which variant to use. Sooner or later two places disagree, and a mixed set appears that compiles, starts, and is wrong. 00:01:28 Keep a dark interface consistent A settings screen needs buttons and dialogs that share a theme. If each widget chooses its own theme, a dark button can end up in a light dialog. Choose one theme factory when the screen starts. Ask that same factory for both products and use the button inside the matching dialog. Switch the factory to a light family without editing the screen's construction logic. Adding a completely new product type, however, changes the factory contract and every family. Select the dark factory once and request both a button and a menu. Compare that to taking a button from dark and a menu from light. The individual products may each be valid while the combination breaks the theme promise. Test them as one family. 00:02:18 Build matching data-access components A store needs a connection and a transaction object compatible with that connection. A provider-specific factory can produce both from one family. A test with incompatible products demonstrates the relationship the factory is meant to preserve. Create a session from backend A and attempt to execute a command from backend B. Different resource ownership can make an apparently compatible pair unusable. Obtain both through the chosen backend factory and still test that every returned product belongs to that family. The abstract factory shape alone may still let callers manually mix products from different families. Preserve the selected family at the composition boundary. If only one kind of product varies independently, a single factory function is usually sufficient. Java AWT Toolkit. Abstract superclass of all implementations of the Abstract Window Toolkit. Its subclasses bind components to a particular native toolkit, and Toolkit.getDefaultToolkit returns the one for this machine. Swing look and feel. A LookAndFeel supplies a matching ComponentUI class for each of Swing's user interface ids through UIDefaults. The family is a table of names, a table-driven form of the idea. 00:03:40 Try an interview follow-up Does the type system prevent mixing? No. Both families share the static types, so a client with two factories can mix, and a factory can return a product of another family. One final factory per client, contract tests, an owner check, or a type parameter for the family prevent it. A toolkit must deliver buttons and menus that work together under one accessibility backend. Build both through the selected component factory. Now trace what happens if a caller creates the menu directly from a different backend. Keep family selection at one composition point and test a complete widget pair. 00:04:23 What to take with you Draw the grid: kinds across, families down. A concrete factory is a row. The client sees the interfaces only, and the factory is chosen once, near the start. Adding a row touches nothing. Adding a column touches every factory, so use defaults or capability interfaces when others implement yours.