简体   繁体   中英

Aggregation relationship in class diagram?

I want to ask a question about aggregation relationship in the world of object oriented programming.

Now i have a class that composed of 2 classes.

We know in aggregation relationship that the main class has a reference to the aggregate class as an attribute

But the main class doesn't contain any attributes or methods except the aggregate classes.

Is this correct in the world of object oriented programming

for example:

main class called knight:

the others are horse and sword.

So the knight class are composed of horse and sword.

this is an aggregation relationship...

public class knight{
private horse h;
private sword s;

// no others attributes and methods
}

my question: is leaving the class knight without any attributes and methods as above correct ???

Yes, it is correct, but useless.

You have forgotten, that you have to expose somehow the knight's properties. Fields or some methods working with them should be public. Or your knight is not a knight, but a hermit :-)

For example:

public boolean putSwordOn(Sword sword){}; //sets current sword
public boolean putSwordInSheath(){};  // uses sword
public boolean pullSwordOut(){};      // uses sword
public boolean attack(Creature target){};   // uses sword
public void kissSwordAndSwearToServe(Knight senior); // uses sword
public boolean climbHorse(Horse horse){};   // sets current horse
public boolean gallop(Point direction){};   // uses horse

Also, you have a problem in terminology. Such connection (has a) is shown by association . And that is shown on the class diagram by a solid line, probably with points or a diamond or an arrowhead. An association has some aggregation . It is about how many instances of that type belong to that field and how do they belong. aggregation can be composition , shared or none . We can say that this association has aggregation of type none , but to name "aggregation" something, that has none aggregation is obviously incorrect. So, you are talking about association relationship.

Yes, why wouldn't it?

There's absolutely no reason why that would not be correct.

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