-
Notifications
You must be signed in to change notification settings - Fork 7
Tutorial : Create a new Molecule Component
There is two ways to create a new Molecule Component :
- Create a new Component from scratch : write a new Class inheriting from the Component hierarchy
- Re-using an existing Class : augmenting that class with Component behavior
To develop a new Component from scratch, we must sublass the MolAbstractComponentImpl abstract Class.
Imagine that we want to reuse an open-source library that implements the behavior for our use case. We want to reuse a class from this existing implementation in our application to add the capability to use this behavior. This class is not a Molecule Component, and does not share the same Class hierarchy as Molecule Components. Therefore this class does not answer the Molecule Component’s interface, and cannot be reused directly as a Component. To manually plug this Class into a molecule component, we have to write glue code for the component to use the API of this Class. This requires an additional effort to write nonfunctional code, which introduces noise in the application code. This makes such architecture less to understandable and to maintainable.
With Molecule, we reuse any existing Class by augmenting that Class with Component behavior. This Class becomes seamlessly usable as a Component in a Molecule architecture.
We add the Molecule Component interface MolComponentImpl to the existing Class. Any class that implements this interface is usable as a Molecule component. Then, we affect the type Component to the class as a standard Molecule Component.
Whatever the method to create a component (from scratch or with an existing Class), the construction and assignment of the contract is the same.
The component type implements the component contract (used/provided services, consumed/produced events, used/provided parameters). The type is implemented through a Trait.