Debugging Java | Delegation pattern with constructor based dependency injection

Several real world programming problem statements talk about the difficulties faced while trying to align differently fashioned processing of similar data. Taking a simple example, of processing of payroll/salaries of a company's employees. Payroll processing can get complicated if employees are based out of different geographies; salary components would vary, tax brackets etc will also differ. Core logic of such an application would require an adequate system which would manage such processing effectively.  

The post speaks about a simple implementation of delegation pattern in Java.
Delegation pattern, as the name suggests, is a scenario where the tasks/activities are delegated from one class to another. While there are several ways to do it(read: inheritance etc.), delegation pattern suggests the usage of composition (has-a relationship) instead of using inheritance (is-a relationship). 

We divide and represent our application into the following components:
Delegator Class(DependencyDelegator)
Performer Interface(DelegatedInterface)
Performer Class(DelegatedClassOne,DelegatedClassTwo)
Spring Config Class(SpringConfigClass)

As per the has-a paradigm, Delegator class has Performer's object as data members. All the performers get subscribed through a collection(map) in the Delegator. Performers are identified by markers, which act as key for the map. The method callDelegateEmitMethod in the Delegator is exposed to other applications/systems as an API method. Within the method the relevant performer gets called, based on the incoming marker, and takes up the task. It can emit an event/forward the processing up ahead as well. Maintaining delegation through interface, allows the DependencyDelegator class to inherit any other behaviors too, if need be. Furthermore, facilitating AI through processing legacy data and generating insights & forecasts can get simplified too.

Delegation pattern finds distinctive usage in applications which manage processing of different types of transactions. More-so, when there is range of Objects to cater to, as a part of processing different transactions. With the need of efficient tech. to handle the omni-present data and content created during financial transactions, social media feeds etc., the pattern forms center stage . 

Play around with the delegation pattern here. Blend the pattern with other mechanisms such as spring boot, RESTful end points, messaging frameworks etc. NOTE: The implementation uses Spring 4.3.9 for dependency injection.

No comments:

Post a Comment