Composition: Ownership, same life time, not change in the middle.
Car and engine
Aggregation: Ownership, maybe same life time, may change in
the middle, People and toothbrush(same life time, changeable)
Teacher and student( not same life time, changeable), Container
and pointer(same life time, changeable).
Associate: No Ownership, Includes as a member, different life
time, person and father(Not Nullity), person and wife( Nullity)
Dependency: No Ownership, Not Includes as a member, person and
friend.
Dependency definition: If class X’s member function argument
is class Y, X is dependency of Y.
Dependency definition extention: For a class X, all functions,
including free functions, that both "Mention" X and "supplied
with" X are logically part of X, because they form part of the
interface of X.
OOP example 1: If it’s a mother-son(Composition) relationship, and it’s
compiler-given(not dynamic), just use member obj. Pay attention, If
Engine has no default ctor, you have to use initialization list in car ctor.
initialization list can be used to call base ctor.
OOP example 2: If it’s an aggregation relationship, If has same life time, use
uniqu_ptr. If you want to change, use uniqu_ptr reset function or move
semantic from another uniqu_ptr. TootuBrush example, use unique_ptr.
OOP example 3: if it’s wife-husband(Association) relationship, If you want
to express Strong Not Nullity, use reference, such as Mother-Son
association, You need to use initialization list to init mother. If
Nullity, such as wife, just use raw pointer, weak_ptr or shared_ptr.
Because no ownership involved, don’t use uniqu_ptr atall.
OOP example 3-1: What’s different with raw pointer, weak_ptr or
shared_ptr?
OOP example 5: Suppose Man and Computer is has-a relationship and same
life time. Don’t use pointer or reference, just copy from a common computer,
and maybe later you can customize your computer, and it will not effect
common one. We can copy from commonComputer and init all Worker
object. And this time use initialization list can improve efficiency.
OOP example6: Change a semantic, Unit may have a Bus, but ownerpolicy tell us that bus can be shared by different unit. and life timepolicy tell us that bus and unit has separate life time. so here, we should
use shared_ptr.