Factory Kit
Also Known As Abstract-Factory Intent Define a factory of immutable content with separated builder and factory interfaces. Explanation Real-world example Imagine a magical weapon factory that can create any type of weapon wished for. When the factory is unboxed, the master recites the weapon types needed to prepare it. After that, any of those weapon types can be summoned in an instant. In plain words Factory kit is a configurable object builder, a factory to create factories.
Factory Method
Also known as Virtual Constructor Intent Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Explanation Real-world example Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned. In plain words It provides a way to delegate the instantiation logic to child classes.
Fan-Out/Fan-In
Intent The pattern is used when a source system needs to run one or more long-running processes that will fetch some data. The source will not block itself waiting for the reply. The pattern will run the same function in multiple services or machines to fetch the data. This is equivalent to invoking the function multiple times on different chunks of data. Explanation The FanOut/FanIn service will take in a list of requests and a consumer.
Filterer
Name / classification Filterer Intent The intent of this design pattern is to introduce a functional interface that will add a functionality for container-like objects to easily return filtered versions of themselves. Explanation Real world example We are designing a threat (malware) detection software which can analyze target systems for threats that are present in it. In the design we have to take into consideration that new Threat types can be added later.
Fluent Interface
Intent A fluent interface provides an easy-readable, flowing interface, that often mimics a domain specific language. Using this pattern results in code that can be read nearly as human language. Explanation The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API. Those interfaces tend to mimic domain specific languages, so they can nearly be read as human languages. A fluent interface can be implemented using any of
Hexagonal Architecture
Also known as Ports and Adapters Clean Architecture Onion Architecture Intent Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases. Class diagram Applicability Use Hexagonal Architecture pattern when When the application needs to be independent of any frameworks When it is important that the application highly maintainable and fully testable Tutorials Build Maintainable Systems With Hexagonal Architecture Real world examples Apache Isis builds generic UI and REST API directly from the underlying domain objects Credits Alistair Cockburn - Hexagonal Architecture
Layers
Intent Layers is an architectural pattern where software responsibilities are divided among the different layers of the application. Explanation Real world example Consider a web site displaying decorated cakes for weddings and such. Instead of the web page directly reaching into the database, it relies on a service to deliver this information. The service then queries the data layer to assimilate the needed information. In plain words With Layers architectural pattern different concerns reside on separate layers.
Lazy Loading
Intent Lazy loading is a design pattern commonly used to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program’s operation if properly and appropriately used. Class diagram Applicability Use the Lazy Loading idiom when eager loading is expensive or the object to be loaded might not be needed at all Real world examples JPA annotations @OneToOne, @OneToMany, @ManyToOne, @ManyToMany and fetch = FetchType.
Metadata Mapping
Intent Holds details of object-relational mapping in the metadata. Explanation Real world example Hibernate ORM Tool uses Metadata Mapping Pattern to specify the mapping between classes and tables either using XML or annotations in code. In plain words Metadata Mapping specifies the mapping between classes and tables so that we could treat a table of any database like a Java class. Wikipedia says Create a “virtual object database” that can be used from within the programming language.
Model-View-Controller
Intent Separate the user interface into three interconnected components: the model, the view and the controller. Let the model manage the data, the view display the data and the controller mediate updating the data and redrawing the display. Explanation Real-world example Consider ICU room in hospital which displays the patients health information on device displays which are taking input from sensors connected to patient. Here, display’s job is to display the data that it receives from the controller which in turn gets update from sensor model.
Model-View-Intent
Intent MVI is a derivation of the original MVC architectural pattern. Instead of working with a proactive controller MVI works with the reactive component called intent: it’s a component which translates user input events into model updates. Explanation MVI is a Reactive Architecture Pattern which is short for Model -View-Intent. It introduces two new concepts: the intent and the state. UI might have different states — Loading State, Fetch Data State, Error State, and user events are submitted in the form of an Intent.
Model-View-Presenter
Intent Apply a “Separation of Concerns” principle in a way that allows developers to build and test user interfaces. Explanation Real-world example Consider File selection application that allows to select a file from storage. File selection logic is completely separated from user interface implementation. In plain words It separates the UI completely from service/domain layer into Presenter. Wikipedia says Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern, and is used mostly for building user interfaces.
Model-View-ViewModel
Also known as Model–View–Binder Intent To apply “Separation of Concerns” to separate the logic from the UI components and allow developers to work on UI without affecting the logic and vice versa. Explanation Wikipedia says Model–view–viewmodel (MVVM) is a software architectural pattern that facilitates the separation of the development of the graphical user interface (the view) – be it via a markup language or GUI code – from the development of the business logic or back-end logic (the model) so that the view is not dependent on any specific model platform.
Monad
Intent Monad pattern based on monad from linear algebra represents the way of chaining operations together step by step. Binding functions can be described as passing one’s output to another’s input basing on the ‘same type’ contract. Formally, monad consists of a type constructor M and two operations: bind - that takes monadic object and a function from plain object to monadic value and returns monadic value return - that takes plain type object and returns this object wrapped in a monadic value.
MonoState
Also known as Borg Intent Enforces a behaviour like sharing the same state amongst all instances. Class diagram Applicability Use the Monostate pattern when The same state must be shared across all instances of a class. Typically this pattern might be used everywhere a Singleton might be used. Singleton usage however is not transparent, Monostate usage is. Monostate has one major advantage over singleton. The subclasses might decorate the shared state as they wish and hence can provide dynamically different behaviour than the base class.
Multiton
Also known as Registry Intent Ensure a class only has a limited number of instances and provide a global point of access to them. Explanation Real-world example The Nazgûl, also called ringwraiths or the Nine Riders, are Sauron’s most terrible servants. By definition, there’s always nine of them. In plain words Multiton pattern ensures there are a predefined amount of instances available globally. Wikipedia says In software engineering, the multiton pattern is a design pattern which generalizes the singleton pattern.
Mute Idiom
Intent Provide a template to suppress any exceptions that either are declared but cannot occur or should only be logged; while executing some business logic. The template removes the need to write repeated try-catch blocks. Class diagram Applicability Use this idiom when an API declares some exception but can never throw that exception eg. ByteArrayOutputStream bulk write method. you need to suppress some exception just by logging it, such as closing a resource.
Naked Objects
Intent The Naked Objects architectural pattern is well suited for rapid prototyping. Using the pattern, you only need to write the domain objects, everything else is autogenerated by the framework. This pattern enforces a strong domain-driven design principle as the generated UI directly reflects your domain objects and allows users to view as well as interact with them directly (hence the ’naked’ part). This allows the users to become more of a problem-solver rather than a process-follower when using the UI.
Object Mother
Object Mother Define a factory of immutable content with separated builder and factory interfaces. Class diagram Applicability Use the Object Mother pattern when You want consistent objects over several tests You want to reduce code for creation of objects in tests Every test should run with fresh data Credits Answer by David Brown to the stackoverflow question: What is an ObjectMother? c2wiki - Object Mother Nat Pryce - Test Data Builders: an alternative to the Object Mother pattern
Object Pool
Also known as Resource Pool Intent When objects are expensive to create and they are needed only for short periods of time it is advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated objects tracking which ones are in use and which are available. Explanation Real world example In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they are extremely expensive to create.
Private Class Data
Intent Private Class Data design pattern seeks to reduce exposure of attributes by limiting their visibility. It reduces the number of class attributes by encapsulating them in single Data object. Explanation Real world example Imagine you are cooking a stew for your family for dinner. You want to prevent your family members from consuming the stew by tasting it while you are cooking, otherwise there will be no more stew for dinner later.
Property
Intent Create hierarchy of objects and new objects using already existing objects as parents. Class diagram Applicability Use the Property pattern when When you like to have objects with dynamic set of fields and prototype inheritance Real world examples JavaScript prototype inheritance
Prototype
Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Explanation First, it should be noted that the Prototype pattern is not used to gain performance benefits. It’s only used for creating new objects from prototype instances. Real-world example Remember Dolly? The sheep that was cloned! Let’s not get into the details but the key point here is that it is all about cloning.
Registry
Intent Stores the objects of a single class and provide a global point of access to them. Similar to Multiton pattern, only difference is that in a registry there is no restriction on the number of objects. Explanation In Plain Words Registry is a well-known object that other objects can use to find common objects and services. Programmatic Example Below is a Customer Class 1public class Customer { 2 3 private final String id; 4 private final String name; 5 6 public Customer(String id, String name) { 7 this.
Repository
Intent Repository layer is added between the domain and data mapping layers to isolate domain objects from details of the database access code and to minimize scattering and duplication of query code. The Repository pattern is especially useful in systems where number of domain classes is large or heavy querying is utilized. Explanation Real world example Let’s say we need a persistent data store for persons. Adding new persons and searching for them according to different criteria must be easy.
Resource Acquisition Is Initialization
Intent Resource Acquisition Is Initialization pattern can be used to implement exception safe resource management. Class diagram Applicability Use the Resource Acquisition Is Initialization pattern when You have resources that must be closed in every condition
Serialized Entity Pattern
Intent To easily persist Java objects to the database. Explanation Java serialization allow us to convert the object to a set of bytes. We can store these bytes into database as BLOB(binary long objects) and read them at any time and reconstruct them into Java objects. Programmatic Example Walking through our customers example, here’s the basic Customer entity. 1@Getter 2@Setter 3@EqualsAndHashCode 4@ToString 5@AllArgsConstructor 6public class Country implements Serializable { 7 8 private int code; 9 private String name; 10 private String continents; 11 private String language; 12 public static final long serialVersionUID = 7149851; 13 // Constructor -> 14 // getters and setters -> 15} Here is CountrySchemaSql, this class have method allow us to serialize Country object and insert it into the database, also have a method that read serialized data from the database and deserialize it to Country object.
Service Layer
Intent Service Layer is an abstraction over domain logic. It defines application’s boundary with a layer of services that establishes a set of available operations and coordinates the application’s response in each operation. Explanation Typically applications require different kinds of interfaces to the data they store and the logic they implement. Despite their different purposes, these interfaces often need common interactions with the application to access and manipulate its data and invoke its business logic.
Service Locator
Intent Encapsulate the processes involved in obtaining a service with a strong abstraction layer. Class diagram Applicability The service locator pattern is applicable whenever we want to locate/fetch various services using JNDI which, typically, is a redundant and expensive lookup. The service Locator pattern addresses this expensive lookup by making use of caching techniques ie. for the very first time a particular service is requested, the service Locator looks up in JNDI, fetched the relevant service and then finally caches this service object.
Service to Worker
Intent Combine a controller and dispatcher with views and helpers to handle client requests and prepare a dynamic presentation as the response. Controllers delegate content retrieval to helpers, which manage the population of the intermediate model for the view. A dispatcher is responsible for view management and navigation and can be encapsulated either within a controller or a separate component. Explanation Real world example In the classic MVC pattern, M refers to the business model, V refers to the user interface, and C is the controller.