views:

49

answers:

1

I have two classes. One is the Part class, its purpose is to deal with visual requirements. The other is the Whole class, its purpose is to deal with operational requirements. Of course Whole class has Part class data members. I want to manage Part class operational requirements with Whole class and Whole class visual requirements with Part class. Tell me a way to design this two classes smoothly?

A: 

If the Part class's lifetime depends upon the Whole class (i.e) if Whole class dies, so does the Part class, then use Composition since their lifetime depends on each other.

Or instead, if the lifetime of Part doesn't depend on the Whole, go for Aggregation..

For e.g,

A bike (Whole) will not continue to live, if it's Engine(Part) is removed. - Composition

A bike (Whole) will continue to live, even if it's headlight(Part) is broken. - Aggregation

It just depends upon the context by how you are gonna use your classes..

Hope it helps..

liaK