photo of a turtle underwater

With the advancement in the software industry, there have also been advancements in the way we design and architect our software.

Hexagonal architecture is one such architectural pattern where we design our software in such a way that we can add or remove modes of communicating with the external world without changing the core functionality of the application.

In the Hexagonal architectural pattern, we wrap the core functionality of the application into imaginary boundaries depending on whether it is the domain i.e. POJO objects, or the application functionalities i.e. the Service layer or the frameworks(e.g. the commonly used libraries) used to build the application. The wrapping starting with the domain object being the innermost layer and framework libraries being the outermost ones. For representational purposes, these layers are drawn in a Hexagon shape corresponding to the pattern name ‘Hexagonal’.

Further adding to these layers of Hexagons are two important components i.e. Ports and Adapters. Ports represent points from where the application communicates with the external world. Adapters, on the other hand, are the concrete implementations of the ports which actually enable the communication specified by the ports.

Communications with the external world can be of two types, incoming or outgoing. Whenever any person or application requests some information from our application it’s considered to be an incoming communication. Whereas, whenever our application requests some information from the external world, be it a database or any other application on the internet, such communication is considered outgoing communication. Based on communication type ports are also categorized as inbound or outbound i.e. incoming -> inbound port and outgoing -> outbound.

Since adapters are the concrete implementations of the ports. An example of an input adapter would be a Rest Controller which can be called by the external world to get information about resources. And an example of an outgoing adapter would be a Repository(Data Access Layer) class responsible for fetching data from an external database for e.g. a SQL-based or No-SQL-based DB. 

For each request, be it incoming or outgoing the flow happens layer by layer, meaning to say for an incoming request the Framework Libraries call the Service layer and then the Service layers fetch the appropriate Domain Object. Similarly for an outgoing communication request originates from the Domain Object, propagating through the Service Layer and thereby using the Framework Library to fetch the data from the external database, be it SQL or No SQL database.

By spstech

11 thought on “The Hexagon design pattern”
  1. Excellent blog here! Also your site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as fast as yours lol

  2. You really make it seem so easy with your presentation but I find this matter to be really something that I think I would never understand. It seems too complex and very broad for me. I am looking forward for your next post, Ill try to get the hang of it!

  3. Hello are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any coding knowledge to make your own blog? Any help would be greatly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *