Skip to content

Overview#

Creational Patterns#

Definition

Creational design patterns are a category of design patterns in software engineering that deal with object creation mechanisms. The aim of these patterns is to create objects in a manner suitable to the situation, enhancing flexibility and reuse of existing code. Rather than creating objects directly using constructors, these patterns provide a way to encapsulate and abstract the instantiation process. This helps to make a system independent of how its objects are created, composed, and represented.

Creational Patterns List#

Pattern Description Usage
๐ŸŒ Singleton Ensures that a class has only one instance, and provides a global point of access to it. This is useful when exactly one object is needed to coordinate actions across the system. Used in logging, database connections, and configuration settings where a single shared resource is operated upon by various parts of an application.
๐Ÿ› ๏ธ Builder Separates the construction of a complex object from its representation, allowing the same construction process to create different representations. This pattern is especially useful for creating complex objects step by step. Commonly used in the creation of complex or composite objects with multiple parts and configurations, such as building different types of documents (like HTML, PDF, etc.), UIs, or complex data objects.
๐Ÿญ Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern allows a client to work with various product families without being tied to the concrete classes of the products. Widely used in UI toolkits and libraries to manage and maintain families of objects like buttons, checkboxes, and windows that share a common theme or platform dependency without hard-coding their classes.
๐Ÿงฑ Factory Method Defines an interface for creating an object, but lets subclasses decide which class to instantiate. The Factory Method lets a class defer instantiation to subclasses. Utilized in applications where class instantiation may depend on user input or configuration settings. It's often seen in application frameworks where the library code needs to instantiate classes that are extended by application developers.
๐Ÿ” Prototype Used to create duplicate objects while keeping performance in mind. This pattern involves copying existing objects without the intricacies of creating them from scratch, typically using a 'clone' method. Useful in scenarios where object creation is costly or complex but a similar object is already existing. This pattern is often used in gaming for creating multiple instances of complex objects like characters, trees, or even entire levels.

Structural Patterns#

Definition

Structural patterns are design patterns that focus on simplifying the design of a system by identifying efficient ways to establish relationships between entities. They help organize different parts of a system in such a way that they can be composed or extended easily to create larger structures. By managing the composition of objects and classes, structural patterns ensure that changes in the system's structure can be made with minimal alterations to individual components, promoting better software maintainability and scalability. Common examples of structural patterns include Adapter, Bridge, Composite, Decorator, Faรงade, Flyweight, and Proxy. Each of these patterns addresses specific problems related to system structure and object composition.

Structural Patterns List#

Pattern Description Usage
๐Ÿ”„ Adapter Allows objects with incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces by wrapping the interface of one class into a format that another class can understand. Adapting data from one format to another (e.g., XML to JSON), interfacing with third-party or legacy code, or providing a new interface to existing systems.
๐ŸŒ‰ Bridge decouples an abstraction from its implementation so that the two can vary independently. It involves an interface which acts as a bridge, making the functionality of concrete classes independent from interface implementer classes. Used in GUI frameworks to separate abstractions (like Window) from platform-specific implementations (like WindowImpl for different operating systems), or in applications to handle changes in the implementation of components without affecting clients.
๐ŸŒณ Composite Composes objects into tree-like structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly. Graphic rendering systems (like graphical shapes), manipulation and representation of hierarchical structures (like file systems).
๐Ÿ›๏ธ Faรงade Provides a simplified interface to a complex system of classes, a library, or a framework. It encapsulates a complicated subsystem with a simpler interface, reducing its complexity and hiding its underlying details. Integrating complex libraries or APIs (like video rendering engines or complex business modules), simplifying the usage of multiple interconnected components or systems.
๐Ÿชถ Flyweight Used to reduce the memory usage by sharing as much data as possible with similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. In graphics-heavy applications where objects such as bullets in a game or characters and glyphs in a text processor are duplicated frequently, managing memory usage in networked data (like routers or switches handling packet flows).
๐Ÿšช Proxy Provides a surrogate or placeholder for another object to control access to it, enhance it, or delay its creation until necessary. It acts as an intermediary between the client and the real object. Implementing access controls, supporting lazy instantiation (particularly in resource-intensive operations), and providing smart reference to an object (like reference counting or safe memory operations).

Behavioral Patterns#

Definition

Behavioral patterns are design patterns that focus on algorithms and the assignment of responsibilities between objects. They deal with communication between objects, making the interaction easier and more flexible. Behavioral patterns help in defining not just the roles objects play in the system, but also the way they communicate with each other, which enhances the coordination and delegation of responsibilities within the system.

Behavioral Patterns List#

Pattern Description Usage
๐Ÿ”— Chain of responsibility Allows multiple objects to handle a request without coupling the sender of the request to the receiver. Requests are passed along a chain of objects until one handles it. Used in GUI frameworks where an event might be handled at multiple levels of a component hierarchy or in applications with complex business rules.
๐ŸŽฎ Command Encapsulates a request as an object, thereby allowing users to parameterize clients with different requests, queue or log requests, and support undoable operations. Used in implementing redo-undo functionality, menu actions in applications, and transactional behavior.
๐Ÿ“– Interpreter This pattern is used to define a grammatical representation for a language and provides an interpreter to deal with this grammar. Used in SQL parsing, the symbol processing in compilers or interpreters, or for implementing small scripting engines.
โžฐ Iterator Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Used in various collection classes in almost all modern programming libraries to abstract the traversal of collections like lists, trees, and graphs.
๐Ÿค Mediator This pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly. Used in the development of GUIs where components need to interact in complex but controlled ways, or in communication systems where multiple devices interact.
๐Ÿ“œ Memento Used to restore an object to its previous state (undo via rollback) Used in applications where the ability to restore an object to a previous state (like a save point in games or transaction rollback) is required.
๐Ÿ‘€ Observer Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Used in implementing distributed event-handling systems, in model-view-controller (MVC) architectures, or in any place where objects need to act in response to state changes in another object.
๐Ÿชจ State Allows an object to alter its behavior when its internal state changes, appearing as if it changed its class. Used in applications with a finite number of states (like TCP connection states) or modes (like tool modes in graphics editors).
๐Ÿน Strategy Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Used in sorting routines, file compression, or any context where different algorithms might be chosen at runtime.
๐Ÿ“ Template Method Pattern Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. Used in frameworks to provide re-usable methods with customizable steps, or in applications requiring a fixed sequence of steps with customizable operations.
๐Ÿ˜๏ธ Visitor Used to separate an algorithm from an object structure on which it operates. A practical way to add new operations to existing object structures without modifying those structures. Used in manipulating complex data structures, like parsing a Composite tree, or when implementing cross-cutting concerns.

Other Patterns#

Pattern Description Usage
๐Ÿ”— Lazy Initialization Pattern Allows to postpone object creation. Used when creating objects should only be created when they are needed.
๐ŸŽฎ Module Pattern Encapsulates a request as an object, thereby allowing users to parameterize clients with different requests, queue or log requests, and support undoable operations. Used in implementing redo-undo functionality, menu actions in applications, and transactional behavior.
๐Ÿ“– Null Object Pattern This pattern is used to define a grammatical representation for a language and provides an interpreter to deal with this grammar. Used in SQL parsing, the symbol processing in compilers or interpreters, or for implementing small scripting engines.
โžฐ Object Pool Pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Used in various collection classes in almost all modern programming libraries to abstract the traversal of collections like lists, trees, and graphs.
๐Ÿค Data Access Pattern This pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other explicitly. Used in the development of GUIs where components need to interact in complex but controlled ways, or in communication systems where multiple devices interact.
๐Ÿ“œ Data Transfer Object Pattern Used to restore an object to its previous state (undo via rollback) Used in applications where the ability to restore an object to a previous state (like a save point in games or transaction rollback) is required.
๐Ÿ‘€ Model View Controller Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Used in implementing distributed event-handling systems, in model-view-controller (MVC) architectures, or in any place where objects need to act in response to state changes in another object.