Strategy pattern 00:00:00 Make a prediction You will isolate an interchangeable calculation and explain why selecting an algorithm differs from tracking an object's state. A journey is four kilometres. Standard pricing and weekend pricing both implement quote. Who should decide which formula is used: the generic checkout or the policy selected for this trip? The application can select the appropriate policy when assembling the trip. Checkout asks that policy for a quote. The policy owns the formula, leaving checkout free of type-specific branches. 00:00:35 Noor's sled A sled can use runners for snow or wheels for a dry path. The traveller chooses the fitted kit for this journey; the route still goes to the same destination. A strategy is selected for a job and fulfills an agreed operation. This differs from a machine changing its allowed actions because earlier events changed its state. Strategy is a behavioral design pattern. It takes an algorithm out of the class that uses it and puts it behind an interface. The class, now called the context, holds a reference to one strategy and delegates to it. Conditions that pick an algorithm spread. The same branch appears in pay, in refund and in label, and the copies drift apart. With an object per way, all answers about one way stand together. 00:01:23 Choose a fare without changing checkout A trip covers four kilometres. One price policy charges a flat fare, another charges by distance. The caller selects the policy before asking for a quote. Checkout passes the same trip to the selected strategy and receives a money value. It neither branches on the strategy type nor reimplements the formula. Run the same journey through both strategies and compare their expected totals. Adding a weekend policy should require no edit to checkout. Price the same trip with standard and peak pricing. Change only the policy, then compare the two results. A route planner or payment step should not need to change. If the algorithm needs the time of day, supply it explicitly so tomorrow's test can reproduce today's fare. 00:02:13 Replace an ordering rule A playlist can sort by song title or duration. Passing a comparator as a function is enough when the strategy has no state. A whole inheritance hierarchy would add ceremony without improving the contract. Choose two tracks whose alphabetical order disagrees with their duration order. Apply both sorting strategies and observe the reversal. A test using tracks that agree on both keys would incorrectly pass even if the wrong strategy were selected. One strategy requiring undocumented extra input breaks interchangeability. Add a coherent context object or separate a truly different contract. When the variation is stateless and small, passing a function is often simpler than creating a class hierarchy. Python sorting can accept a function that derives a sortable key from each record. Supply the title key to order songs alphabetically or the duration key to order them by playing time. Both use the same sorting machinery. A caller with just one simple policy may never need a named class for it. 00:03:20 Try an interview follow-up Would you use classes or functions? For a single stateless operation I would start with a function. A closure can carry one setting. I would choose an object when related operations must share state, when a resource has a lifetime, or when the implementation needs an explicit identity in configuration. The caller should depend on the behaviour it actually uses. A checkout can quote standard or express shipping. Supply the chosen calculation policy and price the same basket both ways. Then change the express surcharge. The policy changes while the checkout workflow stays put. Keep selection separate from calculation so the rule for choosing express can be tested independently. 00:04:05 What to take with you If several ways do one job with the same inputs and answers, give the job to a context and each way to an object behind one interface. What differs goes into constructors, what is shared goes into the method. The choice moves to one place and stays checkable. Read the strategy once per operation, share only what has no state, and fall back only after a certain no.