nus-cs2113-AY2223S1 / forum

4 stars 0 forks source link

How to draw PlantUML for chained method call #37

Closed ngdeqi closed 1 year ago

ngdeqi commented 1 year ago

Hi all, how should I draw a sequence diagram for the chained method call, nameList.getByIndex(2).getHeight() from Main?

public class Main {

    public static void main(String[] args) {
        NameList nameList = new NameList();
        int height = nameList.getByIndex(2).getHeight();
    }
}

public class NameList {
    :
    public Person getByIndex(int index) {
        : 
        return Person;
    }  
}

public class Person {
    :
    public int getHeight() {
        return this.height;
    }
}

I am not sure if getHeight() should be invoked by Main or NameList object.

wcwy commented 1 year ago

Hi, AFAIK, in nameList.getByIndex(2).getHeight(), essentially what it does is to call nameList.getByIndex(2) first, and on the object returned it calls getHeight() method.

You can picture nameList.getByIndex(2).getHeight() into something like this:

Person temp = nameList.getByIndex(2)
int height = temp.getHeight();

Since the method getHeight() is called on the object after the object is returned back to main(), the getHeight should be invoked by Main in sequence diagram.

okkhoy commented 1 year ago

Please take a look at the tutorial question that deals with this scenario.

ngdeqi commented 1 year ago

Thank you for the explanation! @wcwy