30 Seconds of Java

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 8 min
Design Pattern Catalog

iluwatarAbout 4 min
Programming Principles

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 10 min
Abstract Document

Intent

Use dynamic properties and achieve flexibility of untyped languages while keeping type-safety.

Explanation

The Abstract Document pattern enables handling additional, non-static properties. This pattern uses concept of traits to enable type safety and separate properties of different classes into set of interfaces.


iluwatarStructuralExtensibilityAbout 2 min
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

To create a kingdom we need objects with a common theme. The elven kingdom needs an elven king, elven castle, and elven army whereas the orcish kingdom needs an orcish king, orcish castle, and orcish army. There is a dependency between the objects in the kingdom.


iluwatarCreationalGang of FourAbout 3 min
Active Object

Intent

The active object design pattern decouples method execution from method invocation for objects that each reside in their thread of control. The goal is to introduce concurrency, by using asynchronous method invocation, and a scheduler for handling requests.

Explanation


iluwatarConcurrencyPerformanceAbout 1 min
Acyclic Visitor

Intent

Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the troublesome dependency cycles that are inherent to the GoF Visitor Pattern.

Explanation

Real world example

We have a hierarchy of modem classes. The modems in this hierarchy need to be visited by an external algorithm based on filtering criteria (is it Unix or DOS compatible modem).


iluwatarBehavioralExtensibilityAbout 2 min
Adapter

Also known as

Wrapper

Intent

Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

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


iluwatarStructuralGang of FourAbout 3 min
Aggregator Microservices

Intent

The user makes a single call to the aggregator service, and the aggregator then calls each relevant microservice.

Explanation

Real world example

Our web marketplace needs information about products and their current inventory. It makes a call to an aggregator service which in turn calls the product information microservice and product inventory microservice returning the combined information.


iluwatarArchitecturalCloud distributedDecouplingMicroservicesAbout 1 min
Ambassador

Intent

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

Explanation

Real world example

A remote service has many clients accessing a function it provides. The service is a legacy application and is impossible to update. Large numbers of requests from users are causing connectivity issues. New rules for request frequency should be implemented along with latency checks and client-side logging.


iluwatarStructuralDecouplingCloud distributedAbout 2 min
2
3
4
5
...
16