Skip to main content
Programming Principles

There are certain universal laws and principles in software development that guide architects, programmers, and anyone needing to design software. This page lists quite a few of those principles, although it's far from complete. This page is a fork of programming-principles repository by Lars Kappert, who has done most of the work collecting the material.


iluwatarAbout 9 min
30 Seconds of Java

Inspired by 30 seconds of code, this is a collection of reusable
tested copy-pasteable Java 17 compatible code snippets that you can understand in 30 seconds or less. If you're
interested in contributing to this library, please see
the instructions.


iluwatarAbout 11 min
Design Pattern Catalog

iluwatarAbout 5 min
Abstract Document

Intent

The Abstract Document design pattern is a structural design pattern that aims to provide a consistent way to handle hierarchical and tree-like data structures by defining a common interface for various document types. It separates the core document structure from specific data formats, enabling dynamic updates and simplified maintenance.


iluwatarAbout 4 minStructuralAbstractionDecouplingDynamic typingEncapsulationExtensibilityPolymorphism
Abstract Factory

Also known as

  • Kit

Intent

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Explanation

Real-world example

Imagine a furniture company that produces various styles of furniture: modern, Victorian, and rustic. Each style includes products like chairs, tables, and sofas. To ensure consistency within each style, the company uses an Abstract Factory pattern.

In this scenario, the Abstract Factory is an interface for creating families of related furniture objects (chairs, tables, sofas). Each concrete factory (ModernFurnitureFactory, VictorianFurnitureFactory, RusticFurnitureFactory) implements the Abstract Factory interface and creates a set of products that match the specific style. This way, clients can create a whole set of modern or Victorian furniture without worrying about the details of their instantiation. This maintains a consistent style and allows easy swapping of one style of furniture for another.


iluwatarAbout 3 minCreationalAbstractionDecouplingGang of FourInstantiationPolymorphism
Active Object

Intent

The Active Object design pattern provides a safe and reliable way to implement asynchronous behavior in concurrent systems. It achieves this by encapsulating tasks within objects that have their own thread and message queue. This separation keeps the main thread responsive and avoids issues like direct thread manipulation or shared state access.


iluwatarAbout 3 minConcurrencyAsynchronousDecouplingMessagingSynchronizationThread management
Adapter

Also known as

  • Wrapper

Intent

The Adapter pattern converts the interface of a class into another interface that clients expect, enabling compatibility.

Explanation

Real-world example

Consider that you have some pictures on your memory card and you need to transfer them to your computer. To transfer them, you need some kind of adapter that is compatible with your computer ports so that you can attach a memory card to your computer. In this case card reader is an adapter. Another example would be the famous power adapter; a three-legged plug can't be connected to a two-pronged outlet, it needs to use a power adapter that makes it compatible with the two-pronged outlets. Yet another example would be a translator translating words spoken by one person to another


iluwatarAbout 3 minStructuralCompatibilityDecouplingGang of FourInterfaceObject compositionWrapping
Acyclic Visitor

Intent

The Acyclic Visitor pattern decouples operations from an object hierarchy, allowing you to add new operations without modifying the object structure directly.

Explanation

Real world example

An analogous real-world example of the Acyclic Visitor pattern is a museum guide system. Imagine a museum with various exhibits like paintings, sculptures, and historical artifacts. The museum has different types of guides (audio guide, human guide, virtual reality guide) that provide information about each exhibit. Instead of modifying the exhibits every time a new guide type is introduced, each guide implements an interface to visit different exhibit types. This way, the museum can add new types of guides without altering the existing exhibits, ensuring that the system remains extensible and maintainable without forming any dependency cycles.


iluwatarAbout 3 minBehavioralDecouplingExtensibilityInterfaceObject composition
Aggregator Microservices

Intent

To aggregate responses from multiple microservices and return a consolidated response to the client.

Explanation

Real world example

Imagine an online travel booking platform. When a user searches for a vacation package, the platform needs to gather information from several different services: flights, hotels, car rentals, and local attractions. Instead of the user making separate requests to each service, the platform employs an Aggregator Microservice. This microservice calls each of these services, collects their responses, and then consolidates the information into a single, unified response that is sent back to the user. This simplifies the user experience by providing all necessary travel details in one place and reduces the number of direct interactions the user needs to have with the underlying services.


iluwatarAbout 3 minArchitecturalAPI designClient-serverData processingDecouplingIntegrationMicroservicesScalability
Ambassador

Intent

Provide a helper service instance on a client and offload common functionality away from a shared resource.

Also known as

  • Sidecar

Explanation

Real world example

Imagine a busy hotel where guests frequently request restaurant reservations, event tickets, or transportation arrangements. Instead of each guest individually contacting these services, the hotel provides a concierge. The concierge handles these tasks on behalf of the guests, ensuring that reservations are made smoothly, tickets are booked on time, and transportation is scheduled efficiently.

In this analogy, the guests are the client services, the external providers (restaurants, ticket vendors, transportation) are the remote services, and the concierge represents the ambassador service. This setup allows the guests to focus on enjoying their stay while the concierge manages the complexities of external interactions, providing a seamless and enhanced experience.


iluwatarAbout 4 minIntegrationAPI designDecouplingFault toleranceProxyResilienceScalability