Fragmented - An Android Developer Podcast

The Fragmented Podcast is a podcast for Android Developers hosted by Donn Felker and Kaushik Gopal. Our goal is to help you become a better Android Developer. We chat about topics such as Testing, Dependency Injection, Patterns and Practices, useful libraries, and much more. We will also be interviewing some of the top developers out there. Subscribe now and join us on the journey of becoming a better Android Developer.

http://www.fragmentedpodcast.com

subscribe
share






episode 16: 016: Effective Java for Android developers : Item 2


In our third Fragment installment, we introduce Josh's second Item: Consider a builder when faced with many constructor parameters.

Stay tuned for more items from our "Effective Java for Android developers" Fragment series.

[audio src="http://traffic.libsyn.com/fragmented/ep_14_fragmented.mp3" preload="none" autoplay="false"]

[Download][1]

Show Notes
  • Effective Java (2nd Edition) - Joshua Bloch
Consider a builder when faced with many constructor parameters Patterns available for constructing object with parameters:
  • Telescoping Constructor Pattern - Provideconstructor with only required parameters, another with a single optional param, a third with 2 optional params... and so on.
    • Advantage: Works well for small number of parameters
    • Disadvantage: Does NOT scale well
  • JavaBeans Pattern - Callparameterless constructor to create the object; then call setter methods to set required parameter and each optional param of interest.
    • Advantage: Scales well, easy (but wordy) to read resulting code
    • Disadvantage: Allows inconsistency (if all required params not called); impossible to make classes immutable if using this pattern.
  • Builder pattern - winner!
    • Advantage: Simulates named optional parameters; allows immutable objects to be constructed; flexible
    • Disadvantage: more ceremony to actually construct the Builder Class and finally use.
Example usage from Android source:
  • AlertDialog Builder [androidxref.com]
Tip:
  • Use IntelliJ to generate your Builders easily
Contact us:
  • @fragmentedcast [twitter.com]
  • @donnfelker [twitter.com]
  • @kaushikgopal [twitter.com]


fyyd: Podcast Search Engine
share








 September 1, 2015  12m