Articles: Java
Articles and code samples in Java: design patterns, architecture and guides with working code.
- Specification: business rules as objects you can combine Article · 12 min read
The Specification pattern from Domain-Driven Design: named rules with isSatisfiedBy, combining with and, or and not, one rule for validation and querying, translation to SQL via expression trees, the Criteria API and Q objects. Code in…
- Circuit breaker: turning a slow failure into a fast one Article · 11 min read
The Circuit Breaker pattern from the ground up: closed, open and half-open states, thresholds and windows, filtering business errors, working with timeouts, retries and bulkheads. Configuration in Polly, opossum, Resilience4j, gobreaker…
- LLM fundamentals: tokens, context windows and next-token prediction Article · 21 min read
How a large language model really works – from tokenization and the transformer, through context windows and sampling, to costs, hallucinations and a first API call in five languages.
- Embeddings and vector databases: searching by meaning Article · 22 min read
What an embedding is, how to compute cosine similarity, which model to pick and how to store millions of vectors in pgvector, Qdrant or Chroma – with code in Python, TypeScript, C#, Java and Go.
- AI in Java: the official SDK, Spring AI and LangChain4j in practice Article · 18 min read
A map of the AI ecosystem on the JVM – when the official Anthropic SDK is enough, when Spring AI and when LangChain4j – plus streaming, structured responses in records, tools, RAG with pgvector and tests with a fake model.
- Visitor: new operations without changing classes Article · 16 min read
The Visitor pattern through a menu: dishes, drinks and sets stay untouched, while pricing, an allergen report or an export are separate visitor objects. Double dispatch, a comparison with pattern matching and code in five languages.
- Vertical slice architecture: code by feature, not by layer Article · 11 min read
Jimmy Bogard's Vertical Slice Architecture: every feature of the system is a separate slice from the endpoint to the database. Structure and a complete slice in C#, TypeScript, Java, Go and Python, how it combines with CQRS and the…
- Template Method: a fixed recipe, interchangeable ingredients Article · 13 min read
The Template Method pattern through importing supplier price lists: the base class fixes the order of steps, and subclasses provide parsing and optional hooks. The Hollywood principle, an inheritance-free variant in Go and code in five…
- Strategy: interchangeable algorithms instead of an if-else ladder Article · 13 min read
The Strategy pattern through courier assignment in a pierogi place: a family of interchangeable algorithms behind one interface, swapped at runtime. Code in C#, TypeScript, Java, Go and Python.
- State: an object that changes behaviour with its life stage Article · 13 min read
The State pattern through an order in a pierogi place: New, Paid, InKitchen, Delivered and Cancelled as separate objects that guard the allowed transitions themselves. A state machine without an if-else ladder, code in five languages.
- Singleton – one instance, and the reasons to hand it to a DI container Article · 10 min read
Singleton guarantees that a class has exactly one instance available to the whole application. We show thread-safe implementations in five languages and explain why today it is usually better to register the object as a singleton in a DI…
- Proxy – a stand-in that controls access to the real object Article · 11 min read
Proxy has the same interface as the real object and stands in front of it, deciding when – and whether – to pass a call on. An invoice archive example with access control and a lazy connection in C#, TypeScript, Java, Go and Python.
- Prototype – a new object as a copy of a ready-made template Article · 10 min read
Prototype creates objects by cloning a configured instance instead of building them from scratch. We explain the difference between shallow and deep copies using exam templates as an example.
- Onion architecture: the domain model in the middle, infrastructure outside Article · 11 min read
Jeffrey Palermo's Onion Architecture: four tenets, the layers of the core (model, domain services, application services), the difference between a domain service and an application service, and code in C#, TypeScript, Java, Go and Python…
- Observer: notify everyone without knowing anyone Article · 13 min read
The Observer pattern through a pierogi place's pantry: when the cheese runs out, the menu, the shopping list and alerts react on their own, and the pantry does not know they exist. Subscribing, unsubscribing, pitfalls and code in five…
- The Monolith: one application, one deployment, and why it is often the right start Article · 11 min read
What a monolith really is, when it is the best choice, how it turns into a big ball of mud and how to get out of it without a revolution. Structure in .NET, Java, Node.js, Go and Python.
- The Modular Monolith: hard boundaries without the cost of the network Article · 13 min read
One process and one deployment, but modules with a public API, their own data and communication through events. How to build it in .NET, Java (Spring Modulith), Node.js, Go and Python, and how to guard the boundaries with tests.
- Microservices: independent deployments, real costs and when it pays off Article · 11 min read
The microservice style according to Lewis and Fowler, without the marketing: boundaries by business capability, own data, synchronous and asynchronous communication, resilience, prerequisites and the road from the monolith. Code in C#,…
- Memento: a state snapshot without breaking encapsulation Article · 13 min read
The Memento pattern through a menu editor: Halina saves versions of the menu and returns to them with one click, while the history keeps snapshots without seeing what is inside. Copies instead of references, memory costs and code in five…
- Mediator: components talk through a coordinator Article · 12 min read
The Mediator pattern through the order form of a pierogi place: the delivery choice, address, time slot and button do not know each other, and the rules of their cooperation live in one place. Plus an honest look at MediatR and code in…
- Layered architecture: presentation, logic, data – and where it breaks Article · 12 min read
The oldest and most popular way of structuring business applications: closed and open layers, layers versus tiers, code in C#, TypeScript, Java, Go and Python, ArchUnit and NetArchTest tests, and the road to the hexagon.
- Iterator: walking a collection without looking inside Article · 11 min read
The Iterator pattern through orders from a delivery app fetched page by page: the loop in the report knows nothing about pagination, and pages load lazily, only when needed. Generators, yield and iter.Seq in five languages.
- Flyweight – how to fit 60,000 stadium seats in memory Article · 12 min read
Flyweight separates the state shared by many objects from the state unique to each one, and shares the former. A stadium seat map example from a ticket booking system in C#, TypeScript, Java, Go and Python.
- Factory Method – let the subclass decide what gets created Article · 11 min read
Factory Method separates the algorithm that uses an object from the decision about which exact object is created. A ticket booking example in C#, TypeScript, Java, Go and Python.
- Facade – one simple entry point to a complex subsystem Article · 11 min read
Facade exposes one convenient operation that coordinates many subsystem classes behind the scenes. An example of course enrollment with payment, access and a welcome email in C#, TypeScript, Java, Go and Python.
- Event sourcing: store the facts, not just the latest state Article · 13 min read
State as a sequence of immutable events: streams, rehydration by folding, optimistic concurrency, projections, snapshots and event versioning. An aggregate and an event store in C# (Marten), TypeScript, Java, Go and Python.
- Event-driven architecture: who publishes, who listens and what can go wrong Article · 13 min read
Event-driven architecture in practice: Fowler's four meanings of "events", choreography and orchestration, delivery guarantees, the outbox, idempotent consumers, ordering and contracts. Code in C#, TypeScript, Java, Go and Python.
- Decorator – adding behaviour layer by layer Article · 11 min read
Decorator wraps an object in another object with the same interface and adds behaviour before or after the call. An example of caching, retries and logging for an exchange rate provider in C#, TypeScript, Java, Go and Python.
- DDD strategic design: subdomains, bounded contexts and context maps Article · 12 min read
The strategic part of Domain-Driven Design in practice: the ubiquitous language, core, supporting and generic subdomains, bounded contexts, context map patterns and the anticorruption layer. Examples in C#, TypeScript, Java, Go and Python.
- CQRS: one model for changing, another for reading Article · 12 min read
Command Query Responsibility Segregation without overdoing it: from Bertrand Meyer's CQS, through three levels of CQRS, to projections and eventual consistency. Commands, queries and projections in C#, TypeScript, Java, Go and Python.
- Composite – trees in which a leaf and a branch look the same Article · 11 min read
Composite lets you treat a single object and a whole group of objects in exactly the same way. A course curriculum example with modules and lessons in C#, TypeScript, Java, Go and Python.
- Command: a request wrapped in an object Article · 12 min read
The Command pattern through a waiter's terminal in a pierogi place: every action on the ticket is an object with execute and undo, so it can be undone, redone, kept in a history and sent to a queue. Code in five languages.
- Clean Architecture: the dependency rule and use cases at the centre Article · 12 min read
Robert C. Martin's Clean Architecture without the myths: four circles, the dependency rule, input and output ports, the presenter. Structure and a use case in C#, TypeScript, Java, Go and Python, plus an honest list of costs.
- Chain of Responsibility: pass it on until someone takes care of it Article · 13 min read
The Chain of Responsibility pattern through complaints in a pierogi place: a bot, a shift manager and the owner form a chain, and a complaint travels until it reaches someone authorised. The classic variant and the middleware pipeline,…
- Builder – complex objects step by step Article · 12 min read
Builder collects an object's parameters in readable steps and hands over a finished, valid product only in build(). A catering order example in C#, TypeScript, Java, Go and Python.
- Bridge – two axes of change without a class explosion Article · 11 min read
Bridge separates the abstraction (what we do) from the implementation (how we do it), so both hierarchies can evolve independently. An example of notifications and delivery channels in C#, TypeScript, Java, Go and Python.
- Adapter – how to get along with code that speaks another language Article · 11 min read
Adapter translates the interface of a foreign class into the interface your code expects. An example of integrating a third-party geocoding SDK in C#, TypeScript, Java, Go and Python.
- Abstract Factory – whole families of objects that fit together Article · 11 min read
Abstract Factory creates a complete set of related objects from one family and makes sure nobody mixes parts from different sets. An example with two payment provider integrations.