Although this project is not based on any particular tutorial, I mainly used the Refactoring Guru explanation as reference when implementing the code.
There is a base class representing a business entity (MIPOccurrence
). This entity has base operations that define its basic behaviour (IOccurrence
). However, we assume a dashboard needs additional operations to generate charts. Instead of adding these chart operations into the entity, we extended the entity by using the decorator pattern (ChartDecoratorMIPOccurrence
).
-
IOccurrence
represents the set of common operations for anyMIPOccurrence
entity. This interface is used by all classes in this project; -
MIPOccurrence
is the base class. It implements theIOccurrence
operations. This represents the basic behaviour of an occurrence; -
BaseDecorator
is the superclass that defines a decorator. AsMIPOccurrence
, it implementsIOccurrence
operations. Thus, bothMIPOccurrence
&BaseDecorator
areIOccurrence
. Classes that add new behaviour toMIPOccurrence
must extendBaseDecorator
. This class has a parameter in the constructor, used to wrap the base class (MIPOccurrence
); -
ChartDecoratorMIPOccurrence
represents the operations necessary for creating dashboard charts. This class extendsBaseDecorator
and, therefore, is also anIOccurrence
. To extendMIPOccurrence
behaviour, it wrapsMIPOccurrence
by using the constructor parameter, on the class instantiation. TheChartDecoratorMIPOccurrence
method,getChartData()
usesMIPOccurrence
base operations, implemented fromIOccurrence
, to generate data for the chart.
The Figure shows the test execution. A MIPOccurrence
is firstly created. Its method getOccurrences()
is called to test the entity is working. Second, the ChartDecoratorMIPOccurrence
class is instantiated, as it is BaseDecorator
, due to the inheritance.
During the instantiation, ChartDecoratorMIPOccurrence
wraps the MIPOccurrence
instance. Finally, the ChartDecoratorMIPOccurrence
method, getChartData()
, uses the getOccurrences()
method to extract data for generating the chart.
Notice that this diagram is deeper than necessary, as it shows both BaseDecorator
and IOccurrence
- but, it may help understand better the inheritance role.
mvn clean test