Decorator pattern 00:00:00 Make a prediction You will trace wrapper order through a writer and predict when changing that order changes the result. A writer compresses data and encrypts it. If we swap those wrappers, should we expect identical output size or identical bytes? No. Compression looks for structure; encrypted output normally lacks those useful patterns. Write the order as an actual pipeline and undo the transformations in the reverse order when reading. 00:00:29 Sam dresses a snowman A snowman wears a vest, then a scarf over it. Removing the outer layer reveals the scarf first. Adding a second identical vest still changes the whole snowman a second time. Each decorator wraps an object behind the same interface and contributes one behaviour. Ordering and repeated wrapping are observable, so trace them explicitly. Decorator is a structural design pattern. An object is put inside another object that has the same interface, forwards calls to it, and adds something before, after or around them. Subclasses fix a combination when you compile, and the number of combinations doubles with each optional extra. 00:01:12 See why wrapper order matters A writer accepts a document. To compress before encryption, put a compression wrapper nearest the caller and let it delegate compressed bytes to an encryption wrapper. Both wrappers keep the writer contract. Each adds one transformation and forwards the result to the next writer. Read back by undoing the transformations in reverse order. Reversing the writer chain changes the actual bytes and may make compression ineffective. Use a compressible string of repeated letters to make ordering visible. Compressing before encryption usually reduces the data; encrypting first removes most repeated structure and makes compression ineffective. On reading, reverse the exact write pipeline so the client receives the original bytes. 00:01:58 Add behaviour to one cup A basic coffee reports its description and price. A milk decorator holds a coffee, adds the milk charge and extends the description. A second cup can stay black without creating a subclass for every combination. Test the extra charge exactly once per wrapper. The plain cup costs three. Add a one-dollar shot once and the quote becomes four; wrap it again and the quote becomes five. If the product permits only one of that option, the configuration layer must reject the duplicate. A decorator applies every layer it receives. Two wrappers that both add the same fee can double-charge one cup. Make intentional repetition and accidental wrapping distinguishable in tests. If the only variation is a fixed yes-or-no flag and there is no valuable stacking, a small conditional may be simpler. Java I/O. FilterInputStream wraps some other input stream and may transform data or add functionality. BufferedInputStream, DataInputStream and others extend it. Java Collections. Collections.unmodifiableList and synchronizedList return views around a given list: same interface, one behaviour added. 00:03:19 Try an interview follow-up Thirty services each wrap their clients differently and an incident took hours to read. What do you change? One library that builds the chain from a short list of named layers, with a fixed order, a printable description, spans per layer and a deadline that travels with the call. Services choose parameters, not order. A lint rule forbids building clients elsewhere, migration one service at a time behind a switch. A checkout wraps a base price in a percentage discount and then adds a flat delivery fee. Reverse the wrappers and compute the price again. Are delivery fees discountable? Once the business answers, make the order explicit and test with real numbers. Identical method signatures do not make wrapper order irrelevant. 00:04:11 What to take with you Wrap the object in something that is one and has one. Forward everything, add one thing, leave the inside alone. n extras are n classes. Read a chain from outside: the outer layer sees the request first. Authorize outside cache, buffer next to the slow thing, the layer with extra methods outermost.