This is the second article that presents an object-oriented programming (OOP) approach to effectively separate data from domain-specific logic in data-oriented programming, utilizing the dynamic class extensions using the Java Class Extension Library. You can read the first article there.
Let us reconsider the same scenario where we are building a warehouse application designed to handle the shipping of various items. We have established a hierarchy of classes to represent the goods we have:
To implement shipping logic for each item, one might be tempted to add a method directly to each class. While this is straightforward, it can lead to bloated classes filled with unrelated operations -- such as shipping, storing, retrieving from a database, and rendering.
Instead of mixing these responsibilities, it is better to keep items primarily as data classes and separate domain-specific logic from them.
In the first article, we presented a way to achieve that separation using static class extensions with the Java Class Extension library. We defined and implemented extensions as usual Java classes and then utilized the Java Class Extension library to find matching extension classes and create extension objects. This approach is simple, direct, and efficient.
However, some developers would prefer doing that without introducing more extension classes and in a more functional style. The Java Class Extension library now provides such an ability by introducing dynamic class extensions. So, it is possible to define extensions by composing them as sets of lambda operations and let the Java Class Extension library create extensions dynamically on the fly.
Class DynamicClassExtension provides a way to mimic class extensions (categories) by composing extensions as a set of lambda operations. To specify an extension:
Finding an extension and calling its methods is simple and straightforward:
Shipping a collection of items is equally straightforward:
Java Class Extension library provides a valuable alternative for class extensions (not supported in Java) with just a little more verbose code and a little more complex implementation. Both and approaches offer comparable performance, so their choice ultimately depends on personal preferences, style, habits, and specific requirements.
For most cases, a shared instance of DynamicClassExtension should be used. However, if there is a need for different implementations of extensions in different places or domains it is possible to create and utilize new instances of DynamicClassExtension.
DynamicClassExtension takes care of inheritance, so it is possible to design and implement a class extension hierarchy that fully or partially resembles the original classes' hierarchy. If there are no explicit extension operations specified for a particular class - its parent extension will be utilized. For example, if there are no explicit extension operations defined for objects -- base the and the operations specified for will be used instead.
The caching of extension objects is supported out of the box. Cache utilizes weak references to release extension objects that are not in use. However, to perform a full cleanup, either the should be used, or automatic cleanup can be initiated via the . If automatic cache cleanup is used -- it can be stopped by calling the .
The following are the limitations of DynamicClassExtension:
The library is free under the terms of the MIT License and available for download at GitHub.
By leveraging this library, developers can maintain a clean separation between data classes and domain-specific logic while adhering to OOP principles. This results in a flexible and extensible codebase that enhances maintainability and reduces errors.