简体   繁体   中英

How to show the relationship between a class and another class that takes the instance of the class's instance as an input with a sequence diagram?

I will use the same example from my previous question and modify it.

I have a class called House . The instance of this class is house .

class House:
    def __init__(self, steel, money):
        self.steel = steel
        self.money = money

    def housePlan():
        houseHeight = self.steel/self.money
        houseEdgeLength = self.money

I have another class called Person . This class gets several inputs and creates an instance of House . House can exist without the Person class.

class Person:
    def __init__(self,name, steel, money):
        self.name = name
        self.steel = steel
        self.money = money
   
    def buildHouse():
        house = House(self.steel, self.money)

How can I show the relationship between these two classes with UML sequence diagrams?

How can I show the relationship between these two classes with UML sequence diagrams?

The goal of a Sequence Diagram is not to show relationships between classes, a Sequence Diagram describes an Interaction by focusing on the sequence of Messages that are exchanged, along with their corresponding OccurrenceSpecifications on the Lifelines ( formal/2017-12-05 § 17.8 Sequence Diagrams )

From your code buildHouse creates a new instance of House so there is an object creation Message . Because house is a local variable the instance is immediately lost and then we can consider it is immediately deleted by the garbage collector of Python, so a DestructionOccurrenceSpecification depicted by a cross in the form of an X at the bottom of a Lifeline (§ 17.4.4.2 DestructionOccurrenceSpecification ).

在此处输入图像描述

(I used a found message for buildHouse because the caller is unknown nor relevant in your question)

House can exist without the Person class

If you speak about the class, yes for sure because House definition is not nested in Person .

If you globally speak about instances, there is nothing saying only a Person can instantiate a House so yes too.

If you refer to your previous question in my answer I do not use a composition so the deletion of an instance of Person does not imply the deletion of the associated instance of House .

But again in buildHouse the new instance of House is immediately lost because not returned nor saved in a global variable nor saved in an attribute of Person , and then will be deleted by the garbage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM