Using Java Class Extension Library for Data-Oriented Programming - Part 2
Discover an OOP approach to effectively separate data from domain-specific logic in data-oriented programming, utilizing the Java Class Extension Library.
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.
Problem
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 ship() method directly to each Item 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’s 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 Java Class Extension library. So 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. Though some developers would prefer doing that without introduction of more extension classes and in 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 to create extensions dynamically on the fly.
Dynamic Class Extensions with Java Class Extension Library
Class DynamicClassExtension provides a way to mimic class extensions (categories) by composing extensions as a set of lambda operations. To specify an extension:
For example, the following code creates Item_Shippable extensions for Item classes. There are explicit ship() method implementations for all the Item classes. Though, the log() method is implemented for the Item class only so extensions for all the Item descendants will utilize the same log() method.
Finding an extension and calling its methods is simple and straightforward:
Shipping a collection of items is equally straightforward:
Supporting a new Item class using the Java Class Extension library requires just adding the operations for that new Item class. No need to change any other code. That is it.
Conclusion
Java Class Extension library provides a valuable alternative for class extensions (not supported in Java) with just a little more verbose code and little more complex implementation. Both static and dynamic approaches offer comparable performance, so the choice between them ultimately depends on personal preferences, style, habits, and specific requirements.
Details
For the most of the cases a shared instance of DynamicClassExtension should be used. But if there is a need to have different implementations of extensions in different places or domains it is possible to create and utilize new instances of DynamicClassExtension.
Inheritance Support
DynamicClassExtension takes care of inheritance so it is possible to design and implement class extensions hierarchy that fully or partially resembles original classes' hierarchy. If there's no explicit extension operations specified for particular class - its parent extension will be utilized. For example, if there's no explicit extension operations defined for AutoPart objects - base the ship() and the log(boolean) operations specified for Item will be used instead.
Caching
Cashing of extension objects are supported out of the box. Cache utilizes weak references to release extension objects that are not in use. Though, to perform full cleanup either the cacheCleanup() should be used or automatic cleanup can be initiated via the scheduleCacheCleanup(). If automatic cache cleanup is used - it can be stopped by calling the shutdownCacheCleanup().
About Java Class Extension Library
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.