Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Generalization over Aggregation

I have this class model where, Bank is a class which is now going for computerized Banking network. This must have ATM(Automatic Teller Machine) and also Human Cashier.

I used Generalization and have taken a class called AccountHandlers which inherits Bank class. This AccountHandlers further have ATM and HumanCashier aggregated to it.

Now the thing is, my friend was arguing that i have taken the whole thing wrong. According to him AccountHandlers must be aggregated to Bank and that ATM and HumanCashier must inherit to AccountHandlers.

I am a bit confused over it. How can i model it!! or is that both method correct?

like image 373
Nagaraj Tantri Avatar asked Dec 22 '22 23:12

Nagaraj Tantri


1 Answers

I would go back to the basics.

You should ask yourself if an ATM is an AccountHandler, or if an AccountHandler has an ATM. That should give you a general answer as to the question of using inheritance or composition.

Both would be correct. Only one would be a good design and that is dependent on what your application is trying to do.

Generally, there is a rule of thumb (taken from Effective Java) that states that you should favor composition over inheritance. Take that with a grain of salt, and make sure you are designing your app the right way. (For more info see Prefer composition over inheritance?)

like image 194
Yuval Adam Avatar answered Jan 02 '23 09:01

Yuval Adam