Reusable Route Pattern

Learn the Reusable Route design pattern and its usage.

Intent

This pattern allows agnostic business logic to be repeatedly used in different service contexts. Reusability is one of the main tenets of object-oriented and functional programming paradigms. It is also one of the main promises of component-based and service-oriented architectures. In this lesson, we will look at this principle from a Camel application point of view, where it expresses itself as the Reusable Route pattern.

Context and problem

Reuse is a common and overrated goal in many software projects to avoid redundancy. It can be implemented at many levels, starting from methods and class level reuse to complex processing flow reuse and complete service reuse (as advertised by SOA). When implementing a complex system composed of multiple services, there might be bits of logic repeated in multiple places, in the same service, or different services. That repetition can vary from small endpoint configuration to complex sets of processing steps. Rather than duplicating the implementation (through copy and paste) and making it harder to maintain in the long term, it is better to reuse a single implementation in multiple places.

Forces and solution

There are two main strategies for reusing an implementation in different places:

Development time reuse

With this approach, we need access to the reusable logic (the source code or the compiled binary artifact) at build time. At the end of the build process, the reusable logic becomes part of the using service itself. With this mechanism, business logic can be included and reused in multiple services. It is not always the case that reusable implementation is needed at build time. It is possible to use some abstraction such as a Java interface rather than the concrete implementing class and have the dependency provided at deployment rather than build time. Despite that, the principle remains the same: an instance of the reusable logic is included in the main service and becomes part of it at ...

Access this course and 1400+ top-rated courses and projects.