The ports and adapters pattern isolates core business logic so it can be used in a variety of contexts and tested in isolation from components that provide data and events. At runtime, pluggable adapters for specific input sources can be injected into the core business logic to provide access to events and data. Adapters can be swapped at build-time or runtime to create different configurations of the software system. Use this pattern when the system must support multiple input devices or when there is a risk that input devices could change.
This pattern was initially described by Alistair Cockburn under the name Hexagonal Architecture.[8] See the table.
Category | Module or Component & Connector |
Elements |
Layer—Contains domain or business logic that has no knowledge of where data or events it uses originates. Port—Describes the interface between a layer and an adapter. Ports allow layers to be decoupled from concrete adapters. Adapter—Code that interacts with external data sources, devices, or other components that layers can use to gain access to data or events. |
Relations |
Exposes—Indicates the ports available from a specific layer. Implements—Describes the ports which constrain an adapter. Injects—Indicates which adapters will be available to a given layer. |
Rules for Use |
Layers usually but are not required to expose ports. Layers without ports are sometimes referred to as inner layers. Adapters may satisfy the constraints of one or more ports. An adapter may only be injected into a port when that adapter implements the interface required by that port. Depending on the mechanisms used to realize elements and relations, the pattern may refer to design-time or runtime interactions. Be clear and consistent in your models whether you are showing module or C&C structures. |
Strengths |
Promotes testability, maintainability, and modifiability. Different teams can work on different layers or adapters. |
Weaknesses | Mechanisms must be developed to select adapters used at runtime. Runtime qualities such as security and reliability are decided by the adapters. Third-party adapters should be selected with care. |
Here is an example of a ports-and-adapters diagram. In this example, radar simulators can be swapped for adapters to real radar systems without changing the core business logic. The logging and communication bus can also be exchanged depending on the situation.
18.220.163.144