0

How can I implement a strong containment relationship of uml. Also called composition.

To give an example in Scale: I have a class component, which may contain zero or more Interface:

class Component (name: String, description: String, Interaction: Set[Interface])

and then I have my class Interface:

class Interface(name: String, description: String)

What are the constraints that should be respected containment?

  • If I enter an interface in a component, this interface cannot be placed inside other Component.
  • If I delete a Component must also delete all of its interface.

There are other constraints to be enforced a containment?

How do I implement the first constraint:

I thought I'd add a field to the class Interface called signComp of type Component and put constraints on the set method Interaction of Component.
For example: For each Interface that must be added to the Component, if the interface has signComp to null then insert the Interface and sets signComp with the Component current otherwise cancel the assignment.

This is a successful implementation? Or are there other ways.

user1826663
  • 357
  • 1
  • 3
  • 15

1 Answers1

1

If you wanted take an immutable approach you could try something like this:

case class Component(name: String, description: String, interaction: Set[Interface])

case class Interface(name: String, description: String)

def addInterface(components: Set[Component], c: Component, i: Interface) =
  if(components.find(_.interaction contains i) isEmpty)
    components + c.copy(interaction = c.interaction + i)
  else
    components

And use it like this:

val i1 = Interface("i1", "interface 1")
val a = Component("a", "description a", Set(i1))
val b = Component("b", "description b", Set())    
val components = addInterface(Set(a), b, i1)  // Set(Component(a,,Set(Interface(i1,))))
addInterface(components, b, Interface("i2", "interface 2")) // Set(Component(a,,Set(Interface(i1,))), Component(b,,Set(Interface(i2,))))

Since there exists a one-to-one mapping between components, your second constraint would be met simply by remove the component from the set:

components - a // Set()
yakshaver
  • 2,472
  • 1
  • 18
  • 21