Skip to content

fabioono25/design-patterns

Repository files navigation

Design Patterns

The idea is to organize all design patterns (GoF and no GoF) in a series of definitions and examples.

Design Principles

Identify the aspects of your application that vary and separate them from what stays the same.

Program to an interface, not an implementation.

Favor composition over inheritance.

GoF Pattern Categories

The patterns are grouped into categories, based on their purposes:

  • Creational Patterns: involve object instantiation and provide a way to decouple a cliente from the objects it needs to instantiate.

  • Behavioral Patterns: how classes and objects interact and distribute responsibility.

  • Structural Patterns: let you composte classes or objects into larger structures.

Another classification is wheter the pattern deals with classes or objects:

  • Class Patterns: describes how relationships between classes are defined via inheritance. Stablished at compile time.

  • Object Patterns: relationship between objects primarily defined by composition. Created typically at runtime (more flexible and dynamic).

Creational Design Patterns

  • Singleton: The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is useful when you want to restric the instantiation of a class to a single object, such as a configuration manager or logging service.

  • Factory Method: The Factory Method pattern defines an interface for creating objects but allows subclasses to decide which class to instantiate. This pattern is useful when you have a class with multiple subclasses and want to delegate the responsibility of object creation to the subclasses.

  • Abstract Factory: The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It's useful when you need to create families of objects that should be used together, like creating different components of a GUI toolkit.

  • Prototype: The Prototype pattern involves creating a new object by copying an existing object (prototype) rather than creating a new instance from scratch. It's useful when the cost of creating a new object is higher, and you want to achieve performance improvements by copying existing instances.

  • Builder: The Builder pattern separates the construction of a complex object from its representation. It allows the same construction process to create different representations of the object. This pattern is useful when you want to create complex objects step by step, providing flexibility in the construction process.

   

Structural Design Patterns

  • Adapter: The Adapter Pattern allows objects with incompatible interfaces to work together by providing a wrapper that converts one interface into another. It is used when existing classes need to work with new or third-party classes without modifying their source code.

  • Proxy: The Proxy Pattern provides a surrogate or placeholder for another object to control access, add behavior, or defer expensive operations. It is used to control access to objects, add additional behavior, or delay expensive operations until necessary.

  • Composite: The Composite Pattern composes objects into tree structures to represent part-whole hierarchies. It treats individual objects and compositions of objects uniformly. It is used to represent hierarchical structures and enables clients to treat individual objects and compositions uniformly.

  • Bridge: The Bridge Pattern separates the abstraction (interface) from its implementation, allowing them to vary independently. It is used to decouple an abstraction from its implementation, enabling both to evolve independently.

  • Decorator: The Decorator Pattern attaches additional responsibilities to objects dynamically by wrapping them in decorator objects. It is used to add behavior or responsibilities to objects without altering their code, providing a flexible alternative to subclassing.

  • Facade: The Facade Pattern provides a simplified interface to a complex system of classes, acting as a high-level entry point. It is used to provide a unified and simplified interface to a set of interfaces in a subsystem.

  • Flyweight: The Flyweight Pattern shares common state (intrinsic) among multiple objects to reduce memory usage and improve performance. It is used to manage a large number of fine-grained objects efficiently, sharing common state to reduce memory overhead.

   

Behavioral Design Patterns

  • Observer: The Observer Pattern defines a dependency between objects so that when one object changes state, its dependents (observers) are notified and updated automatically. It is used to establish a one-to-many relationship between objects, allowing multiple objects to be informed of changes to a subject.

  • Chain of Responsibility: The Chain of Responsibility Pattern allows multiple objects to handle a request without explicitly specifying which object should process it. It forms a chain where each object in the chain has a chance to process the request or pass it to the next object. It is used to achieve loose coupling between sender and receiver objects, enabling multiple objects to process a request without the sender needing to know the exact processing sequence.

  • Command: The Command Pattern encapsulates a request as an object, allowing for parameterization of clients with different requests, queuing of requests, and logging of command history. It is used to decouple the sender of a request from its receiver, enabling flexible command processing, undo/redo functionality, and dynamic behavior changes.

  • Iterator: The Iterator Pattern provides a way to access elements of a collection sequentially without exposing its underlying representation. It is used to traverse elements of a collection without exposing its structure, enabling uniform iteration across different types of collections.

  • Mediator: The Mediator Pattern defines an object that encapsulates communication between a set of objects (colleagues), allowing them to interact without direct references to each other. It is used to centralize communication and reduce coupling between objects that interact frequently, promoting better maintainability and extensibility.

  • Memento: The Memento Pattern captures and restores an object's internal state, providing the ability to revert an object to a previous state. It is used to implement undo/redo functionality, versioning, and state management in applications.

  • State: The State Pattern allows an object to change its behavior when its internal state changes. It encapsulates different behaviors in separate state objects and delegates the behavior to the current state. It is used to manage an object's behavior based on its state, reducing complex conditional statements and improving maintainability.

  • Strategy: The Strategy Pattern defines a family of algorithms, encapsulates each algorithm, and makes them interchangeable. It allows a client to choose an algorithm from a family of algorithms dynamically. It is used to provide flexibility in selecting and switching between different algorithms or strategies at runtime.

  • Template Method: The Template Method Pattern defines the structure of an algorithm in a base class, with specific steps deferred to subclasses. It allows subclasses to override or extend parts of the algorithm. It is used to create reusable algorithm templates, enabling customization of specific steps by subclasses.

  • Visitor: The Visitor Pattern lets you add further operations to objects without having to modify them. It separates the operations from the object structure. It is used to perform various operations on a set of elements without altering their classes, promoting extensibility and avoiding class proliferation.

  • Interpreter: The Interpreter Pattern defines a language's grammar as a set of classes and provides an interpreter to evaluate sentences in the language. It is used to solve problems related to parsing and interpreting languages, such as expressions, queries, or domain-specific languages.

       

Enterprise Patterns

Enterprise patterns are designa patterns that address high-level architectural concerns that are applied in complex enterprise-level applications. These patterns focus, in general, on dealing with issues related to scalability, integration, distribution, managing high volumes of data.

  • Specification: The Specification Pattern is a behavioral pattern used to define a set of criteria that can be used to filter and match objects. It provides a way to encapsulate complex conditions or rules for selecting objects in a clean and reusable manner. It is commonly used in the context of Domain-driven design.

About

Design Patterns studies

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages