Fragmented - Android Developer Podcast

018: Effective Java for Android developers : Item 3


Listen Later

In this mini Fragment, we introduce Joshua's third Item: Enforce the Singleton property with a private constructor or an enum type.

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

Show Notes
  • Effective Java (2nd Edition) - Joshua Bloch
  • Enforce the Singleton property with a private constructor or an enum type

    Approaches

    1. Create a public static final INSTANCE variable and privatize constructor
    2. Same as 1 but privatize variable and expose access with provide factory method getInstance
    3. Single element Enums
    4. Considerations

      • First two approaches are open to Serialization attacks (deserializing creates new instance)
      • To protect from those declare the fields transient + provide readResolve method
      • Enums are concise, provide free serialization and ironclad Singleton guarantees and are functionally equivalent to first approach
      • Supplemental reading (for the diligent ones that follow shownotes)

        • Dianne Hackborn regretting exposing the base Application class
        • DH again saying Application is a Singleton with crappier semantics
        • Elvis sightings
        • Contact us:
          • @fragmentedcast [twitter.com]
          • @donnfelker [twitter.com]
          • @kaushikgopal [twitter.com]
          • ...more
            View all episodesView all episodes
            Download on the App Store

            Fragmented - Android Developer PodcastBy Donn Felker, Kaushik Gopal

            • 5
            • 5
            • 5
            • 5
            • 5

            5

            68 ratings


            More shows like Fragmented - Android Developer Podcast

            View all
            Design Details by Brian Lovin, Marshall Bock

            Design Details

            361 Listeners

            Developer Tea by Jonathan Cutrell

            Developer Tea

            402 Listeners

            Does Not Compute by Sean Washington, Rockwell Schrock

            Does Not Compute

            53 Listeners

            Toolsday by Una Kravets, Chris Dhanaraj

            Toolsday

            48 Listeners

            Swift Unwrapped by JP Simard, Jesse Squires, Spec Network, Inc.

            Swift Unwrapped

            90 Listeners