Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will be the OOP approach? (or YOUR approach?)

Tags:

java

oop

I'm having difficulties with some general OOP & Java approach. There are various ways to let classes/objects communicate with each other. To give a simple example:

  • I need object A to perform action X.
  • Object A needs P, Q and R to perform this action X.

Will then Object A retrieve P, Q and R by itself (within action X), or must these values be parameters for action X?

like image 245
hsmit Avatar asked Oct 15 '22 05:10

hsmit


1 Answers

This is too general a question to be answered concretely. Either approach can be good in certain situations. Some factors:

  • passing P, Q and R as parameters makes A easier to reuse and test (see Dependency Injection)
  • if P, Q and R are not used anywhere else outside A, they could be made method local
  • if P, Q and R are also used in other methods of A, they could be made members
like image 90
Péter Török Avatar answered Oct 18 '22 13:10

Péter Török