2012-12-14 2 views
0

uml의 강력한 포함 관계를 어떻게 구현할 수 있습니까? 작곡이라고도합니다.봉쇄 관계 구현

규모의 예를 제공하려면 : I는 0 개 이상의 인터페이스를 포함 할 수있는 클래스 요소가 있습니다

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

을 한 후 나는 내 수업 인터페이스가 : 무엇

class Interface(name: String, description: String) 

을 존중해야 할 제약 조건은 무엇입니까?

  • 구성 요소에 인터페이스를 입력하면이 인터페이스를 다른 구성 요소 안에 배치 할 수 없습니다.
  • 구성 요소를 삭제하는 경우 해당 인터페이스도 모두 삭제해야합니다.

봉쇄를 강제하는 다른 제한 사항이 있습니까? 내가 처음 제약 조건을 구현하려면 어떻게

는 :

나는 형 구성 요소의 signComp라는 클래스 인터페이스에 필드를 추가하고 구성 요소의 설정 방법의 상호 작용에 제약을 넣어 줄 알았는데.
예 : 인터페이스에 signComp가 null 인 경우 인터페이스에 삽입하고 구성 요소 current에 signComp를 설정하고 그렇지 않으면 할당을 취소합니다.

성공적인 구현입니까? 아니면 다른 방법이 있습니까?

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 

그리고 다음과 같이 사용 :

답변

1

당신이 뭔가를 시도 할 수 불변의 접근을 원하는 경우 구성 요소 사이의 일대일 매핑이 존재하기 때문에

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,)))) 

두 번째 제약 조건은 집합에서 구성 요소를 제거하여 간단히 충족됩니다.

components - a // Set()