Tagged “Gof”
28 articles with this tag.
- Abstract FactoryCreate families of related objects that must be used together, without naming their concrete classes. A runnable Java 21 example builds SQL for PostgreSQL and MySQL from one client, plus how Abstract Factory differs from Factory Method, trade-offs and interview questions.
- AdapterConvert an interface you cannot change into the one your code expects. A runnable Java 21 example wraps a legacy payment SDK, translating units, status codes and exceptions — plus class versus object adapters, trade-offs and interview questions.
- Adapter vs Decorator vs Proxy vs FacadeFour structural patterns that all wrap other objects. The one question that tells them apart, side-by-side Java sketches, and how to answer the classic interview question.
- GoF OverviewA map of the 23 Gang of Four design patterns: the three categories, class versus object scope, one-line intents, JDK examples, and how to choose between patterns that look alike.
- BridgeSplit a class that varies in two independent dimensions into two hierarchies connected by composition, so they can evolve separately. A runnable Java 21 notification example (alerts and digests × email and SMS), Bridge versus Adapter and Strategy, trade-offs and interview questions.
- BuilderConstruct complex objects step by step. Covers both the GoF Builder (director plus interchangeable builders) and the fluent Effective Java builder for immutable objects, with runnable Java 21 examples.
- Chain of ResponsibilityPass a request along a chain of handlers until one handles it, decoupling the sender from the receiver. A runnable Java 21 expense-approval chain plus a modern list-of-handlers pipeline, servlet filters and Spring Security as real chains, trade-offs and interview questions.
- CommandTurn a request into an object so you can queue it, log it, undo it or replay it. A runnable Java 21 text editor with undo, redo and macro commands, Command versus Strategy, lambdas as commands, and Command in executors, job queues and CQRS.
- CompositeCompose objects into tree structures and treat single objects and groups uniformly. A runnable Java 21 product-bundle example with nested discounts, transparency versus safety, sealed interfaces, and where Composite appears in the JDK, Spring and UI frameworks.
- DecoratorAdd responsibilities to an object dynamically by wrapping it in objects with the same interface. A runnable Java 21 HTTP client stacked with logging, caching and retry decorators, why order matters, java.io as the classic example, Decorator versus inheritance and proxies.
- FacadeGive a complex subsystem one simple entry point. A runnable Java 21 checkout facade coordinating inventory, payment, shipping and notifications with compensation, the Principle of Least Knowledge, Facade versus application service and API gateway, trade-offs and interview questions.
- Factory MethodLet subclasses decide which class to instantiate. A runnable Java 21 report-export example, a clear distinction from the 'simple factory' and static factory methods, UML, trade-offs and interview questions.
- Factory Patterns ComparedSimple factory, static factory method, GoF Factory Method, Abstract Factory and Builder — what each one is, how they differ, and which to choose.
- FlyweightSupport huge numbers of fine-grained objects by sharing their common, immutable state. Intrinsic versus extrinsic state, a flyweight factory, a runnable Java 21 map-marker example sharing 3 styles across 100,000 markers, and Flyweight in the JDK (Integer cache, String interning).
- InterpreterRepresent a small language's grammar as classes and evaluate sentences by walking the syntax tree. A runnable Java 21 feature-flag rule language with sealed records and a recursive-descent parser, when a DSL is worth it, and Interpreter in SpEL, regex and query languages.
- IteratorTraverse a collection without exposing how it is stored. A runnable Java 21 iterator that hides cursor-based API pagination behind Iterable and Stream, internal versus external iteration, fail-fast iterators and Spliterator, with trade-offs and interview questions.
- MediatorCentralise how a group of objects interact so they no longer refer to each other directly. A runnable Java 21 checkout form where widgets talk only to a mediator, many-to-many versus hub-and-spoke coupling, Mediator versus Observer and Facade, and the god-object risk.
- MementoCapture and restore an object's internal state without breaking encapsulation. A runnable Java 21 drawing canvas with checkpoints, narrow and wide memento interfaces using a private record, bounded history, immutability for cheap snapshots, and Memento versus Command for undo.
- ObserverNotify many dependent objects automatically when one object changes. A runnable Java 21 event publisher with subscribe/unsubscribe, failure isolation and CopyOnWriteArrayList, push versus pull, lapsed-listener leaks, Spring application events, reactive streams and the step up to messaging.
- Observer vs Mediator vs Event BusThree ways for objects to communicate without direct calls: Observer broadcasts from a subject to its listeners, Mediator centralises coordination logic, and an event bus decouples publishers and subscribers completely. Coupling, control flow, failure handling and how each scales to messaging and sagas.
- PrototypeCreate new objects by copying a configured prototype instead of building them from scratch. Copy constructors versus Cloneable, shallow versus deep copies, a prototype registry, and a runnable Java 21 example that shows the shallow-copy bug.
- ProxyControl access to an object through a stand-in with the same interface. Virtual, protection, caching and remote proxies in a runnable Java 21 example, JDK dynamic proxies, how Spring's @Transactional and lazy JPA associations rely on proxies, and Proxy versus Decorator.
- SingletonEnsure a class has one instance and a global access point — and why that is harder, and more harmful, than it looks. Enum singletons, the holder idiom, double-checked locking with volatile, testability problems and the dependency-injection alternative, with a runnable Java 21 example.
- StateLet an object change its behaviour when its internal state changes, by delegating to state objects instead of switch statements. A runnable Java 21 order lifecycle (awaiting payment, paid, shipped, delivered, cancelled), enum and sealed alternatives, State versus Strategy, and state machines in practice.
- StrategyDefine a family of interchangeable algorithms behind one interface and choose between them at runtime — with a runnable Java 21 discount-calculation example, UML, trade-offs and interview questions.
- Strategy vs State vs Template MethodThree patterns that all vary behaviour: Strategy swaps a whole algorithm by composition, State swaps behaviour as an object's lifecycle changes, and Template Method varies steps by inheritance. Side-by-side code, the deciding questions and how to choose.
- Template MethodDefine the skeleton of an algorithm in a base class and let subclasses fill in specific steps. A runnable Java 21 data-import pipeline with abstract steps and hooks, the Hollywood Principle, Spring's JdbcTemplate and callbacks, and when to prefer Strategy and composition.
- VisitorAdd new operations to a stable class hierarchy without changing its classes. Double dispatch explained, a runnable Java 21 shape hierarchy with area and SVG-export visitors, and the modern alternative: sealed interfaces with exhaustive pattern-matching switch. The expression problem and trade-offs.