Posts

Archunit test

  ArchUnit  integrates nicely with the  JUnit  test framework, and so, they are typically used together.  All we have to do is add the  archunit-junit 5  dependency to match our  JUnit  version: < dependency > < groupId > com.tngtech.archunit </ groupId > < artifactId > archunit-junit5 </ artifactId > < version > 0.14.1 </ version > < scope > test </ scope > </ dependency > Step 1: The first step is to create a set of Java classes that will be checked for rules violations . We do this by instantiating the  ClassFileImporter  class and then using one of its  importXXX()  methods: JavaClasses jc = new ClassFileImporter () .importPackages( "com.baeldung.archunit.smurfs" ); Step 2:  Let’s try to implement the first rule defined above using this API. We’ll use the  classes()  method as our anchor and add additional constraints ...

Hexagonal Architecture

Image
  Hexagonal architecture is a model of  designing software applications around domain logic  to isolate it from external factors. The domain logic is specified in a business core, which we’ll call the inside part, with the rest being outside parts.  Access to domain logic from the outside is available through  ports  and  adapters .  here we divide our application into three layers:  application (outside), domain (inside), and infrastructure (outside): Through  the application layer, the user or any other program interacts  with the application. This area should contain things like user interfaces, RESTful controllers, and JSON serialization libraries. It includes anything  that exposes entry to our application, and orchestrates the execution of domain logic. In the domain layer, we keep the code that touches and implements business logic . This is the core of our application. This layer should be isolated from both the applica...

Adaptor design patterns.

Two incompatible interfaces or systems can cooperate by using the adapter design pattern, a structural design pattern. Because of incompatible interfaces, it serves as a bridge between two classes that would not otherwise be able to communicate. The adapter approach is very helpful when attempting to incorporate third-party libraries or legacy code into a new system. Real-World Example of Adapter Design Pattern Let’s understand this concept using a simple example: Suppose you have two buddies, one of them speaks French exclusively and the other English exclusively. The language barrier prevents them from communicating the way you want them to. You act as an adapter, translating messages between them. Your role allows the English speaker to convey messages to you, and you convert those messages into French for the other person. In this way, despite the language difference, your adaptation enables smooth communication between your friends. This role you play is similar to the Adapter ...

visitor design pattern

Image
  The Visitor design pattern is a  behavioral pattern  that allows you to add new operations to a group of related classes without modifying their structures. Real-World Example of Visitor Design Pattern A simple example of the Visitor design pattern is in an online shopping cart. Imagine you have different items like books, electronics, and clothing. Each item can accept a visitor that performs actions like calculating the total price or applying discounts. This way, you can add new features without changing the item classes, making the system easier to maintain and update. UML Class Diagram of Visitor design pattern Components of Visitor Design Pattern The Visitor design pattern consists of several key components that work together to enable its functionality. Here’s a breakdown of these components: Visitor Interface : This interface declares a visit method for each type of element in the object structure. Each method is designed to handle a specific element type. Concr...