I have a UML 2.5 class diagram with a lot of composition relationships.
I'm tying to implement them in Kotlin (I am new in Kotlin sorry):
class RuleScenarioState (val description : String){
var action: RuleStateAction? = null
var stateType : ScenarioStateType? = null
var completeCriteria : StateCompleteCriteria? = null
private class ComplexCompleteCriteria private constructor(val customCriteriaImplementation : String): StateCompleteCriteria() {
}
private class ExpressionCompleteCriteria private constructor(val expression : RegulationExpression): StateCompleteCriteria() {
}
private class RegulationNodeCompleteCriteria private constructor(val regulationNodeTreeId : UUID): StateCompleteCriteria() {
}
private class CustomCompleteCriteria private constructor(val customCriteriaImplementation : String): StateCompleteCriteria() {
}
private class CustomRuleStateAction private constructor(val customActionImplementationName : String): RuleStateAction() {
}
private class ClassificationRuleActionState private constructor(val classificationExpression : RegulationExpression): RuleStateAction() {
}
}
Unfortunately I have no idea how this classes will be used, I just have a diagram which need to implement. I think that it is bad idea to create instances with intensive state manipulations as shown above, but how to assure one-to-one instances relationship? How to implement properly UML composition relationship in Kotlin?
CodePudding user response:
What does the UML model require?
I'm new to Kotlin as well. When you read about composition in this language, you must be aware of the difference between object composition, which simply means to have a property that is an object, and composition in the UML meaning, which is also called composite aggregation.
UML composite aggregation is a kind of association that ensures two things:
- that a component instance (e.g.
RuleStateAction
) is exclusively owned by a single composite (e.g.RuleScenarioState
); - that the composite (e.g.
RuleScenarioState
) has the responsibility for the existence and the storage of its components (e.g.RuleStateAction
), and in particular that its components are destroyed should the composite be terminated.
How to implement it in Kotlin?
The first requirement means to declare a private property to the class, using private var
or private val
. You should also make sure not to leak the object to the outside word by returning it in a way or in another. This can be a tricky point in some cases and may require to clone the object.
The second requirement may be achieved by creating the components in the composite. Kotlin is garbage collected, so if only the composite knows about the component, once the composite is no longer used, so is its component (you could also consider making it closeable, but let's not add unnecessary difficulty ;-)
Several unrelated remarks:
According to your design you should use some abstract classes. You seem to have avoided them in your code, but they are essential for this design to work. So you'd have a private var that is initialized with an instance of a concrete class inherited from the abstract class.
your diagram tells nothing about multiplicity of components. If it's only only one there is nothing special to add. But if it is many, you'll have to consider collections.