• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If it better to ask for a parameter directly or have it passed in from another class?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

I'm tracing through Android framework's source code hoping to learn some design rationales, and I have a design question.

What's the design benefit of having a class fed with the information it requires instead of asking for it directly?

For example, I have a PackageManager class which asks WindowManager for display information, which then passes it on to ResourceManager.
My question is, what's the downside of having ResourceManager asking WindowManager for this information directly since PackageManager doesn't use this display information at all?

code follows:



My guess is that if ResourceManager asks WindowManager directly, it is tightly coupled to WindowManager, therefore in the future if the display target changes to Printer, it won't be able to accommodate such change.
By having display information passed in, in the future if there is another class called PrinterManager, PrinterManager's display info can be passed in to ResourceManager, making ResourceManager independent of which display target it is working with.

Please help me understand this design decision.

thanks
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leo Chen wrote:
My guess is that if ResourceManager asks WindowManager directly, it is tightly coupled to WindowManager, therefore in the future if the display target changes to Printer, it won't be able to accommodate such change.
By having display information passed in, in the future if there is another class called PrinterManager, PrinterManager's display info can be passed in to ResourceManager, making ResourceManager independent of which display target it is working with.



Yes, you've nailed it! This approach is very useful when unit testing, too, you can pass in a fake class to ResourceManager to make unit tests independent of WindowManager. I suggest you read about Dependency Injection and Mock Object!
reply
    Bookmark Topic Watch Topic
  • New Topic