-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Roberto Fronteddu edited this page Mar 7, 2023
·
36 revisions
Welcome to the java-patterns-solid wiki!
Principle | Description |
---|---|
Single Responsibility | There should never be more than one reason for a class to change. Classes should provide a single, focused functionality and address a specific concern. |
Open-Closed | Software entities should be open for extension but closed for modification (i.e. it should be possible to extend existing behavior without changing existing code). |
Liskov Substitution | It should be possible to substitute a base class object with one of its children and this should not alter the behavior/characteristics of a program (i.e. a child's parent should pass the parent unit tests) |
Interface Segregation | Clients should not be forced to depend upon interfaces that they do not use. Large interfaces should be avoided (violations of this rule generally have concrete classes with empty methods or with methods that just throw UnsupportedExceptions or similar) |
Dependency Inversion | High-level modules should not depend upon low-level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. |
Deal with the process of creation of objects of classes.
Pattern | Description |
---|---|
Builder | Delegates the construction of an object to a class that separates the process into multiple steps. |
Simple Factory | A Product Creator class creates different types of Concrete Products based on a simple condition. |
Factory Method | Use inheritance to specialize a Product Creator class. Each concrete Creator implements a specific concrete Product. |
Prototype | Uses a class immutable state to expedite the creation of other instances of the same class. |
Singleton | Creates a globally available unique instance of a class |
Abstract Factory | Use concrete implementations of an abstract factory to create objects that are meant to work together. |
Object Pool | Create a class that manages the creation of objects. This class provides methods to get and release instances of the pooled object. The pooled object provides a method to reset itself after release. |
Deal with how classes and objects are arranged or composed.
Pattern | Description |
---|---|
Adapter | Object Adapter: Wrap a target object with an adapter that implements the interface expected by the client, and uses composition to call the adapted methods. Class Adapter: extend the adapted object to implement the interface expected by the client. |
Bridge | Decouples abstraction from implementation by defining two separate inheritance hierarchies for them. The two are bridged together using composition. |
Decorator | Wrap/extend a component, use the component to provide basic functionalities and add on its behavior as required. |
Composite | Struct objects (Leaves) in a tree-like structure behind a single interface (Component), maintain these objects within an object (Composite) that delegates specific operations to them. The interface allows us to treat all objects in the hierarchy uniformly. |
Facade | The Facade provides methods to interact with multiple subsystems and achieve specific tasks. Each subsystem provides specific functionalities and is mediated by the Facade to reduce complexity. |
Flyweight | Address the need of creating several objects of the same type by aggregating the shared state in a single object (Intrinsic State) and supporting operations that accept the unshared state provided by a client (Extrinsic State). |
Proxy | The proxy mediates the interaction with another class (secure methods, support lazy initialization, provide a local representation of a remote resource). It can be dynamic if created at runtime (used to provide general functionality/interaction with a class of objects) or static if created at compile time (used when functionalities are not so general). |
Deal with how classes and objects interact and communicate with each other.
Pattern | Description |
---|---|
Chain Of Responsability | Client uses a chain of handlers to process requests. A handler either processes a request, passes it to a successor if it can't, or does nothing if there is no successor and can't process it. |
Command | Encapsulate an operation in a class so that a runner can execute it at a different time. |
Interpreter | Represent simple grammar as a data structure in which each grammar rule is a separate class. |
Mediator | Encapsulate the communication between objects, when one object changes, the Mediator communicates the change to all the others. |
Iterator | Encapsulates sequential access to an aggregate in an object (the Iterator). |
Memento | Encapsulates the state of an object so that it can be restored later. |
Observer | Implements a one-to-many relationship where a Subject/Observable notifies one or more Observer/Listener about a change in its state. |
State | Encapsulates the behavior of a Context object in different concrete classes implementing the State interface. Each ConcreteState encapsulates a specific behavior. |
Strategy | Encapsulates a behavior in a class. The main class uses the Strategy interface, and the algorithm's implementation is left to concrete Interfaces. |
Template Method | Defines a series of steps, leaving to an implementation how these steps are implemented. |
Visitor | Add new functionalities to objects without modifying them by implementing a new concrete visitor. |
Null Object | Provides a different representation for the absence of an object by creating a concrete implementation that does nothing and has no side effects. |