Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Singleton

 Singleton

Singleton in Java

Oursky Limited

November 19, 2012
Tweet

More Decks by Oursky Limited

Other Decks in Programming

Transcript

  1. Code Review Require appHelper to be singleton Where do you

    come from?? No found in instance variables Monday, 19 November, 12
  2. Code Review Require appHelper to be singleton Where do you

    come from?? No found in instance variables From Super Class? Monday, 19 November, 12
  3. Not a singleton pattern if a class implement Helper and

    have a code: appHelper = new AppHelper() two AppHelper objects will present in the App Monday, 19 November, 12
  4. Intent Ensure a class only has one instance, and provide

    a global point of access Monday, 19 November, 12
  5. Motivation Sometimes we want just a single instance of a

    class to exist in the system For example, we want just one window manager. Or just one factory for a family of products. We need to have that one instance easily accessible And we want to ensure that additional instances of the class can not be Monday, 19 November, 12
  6. Singleton Implementation OK, so how do we implement the Singleton

    pattern? We'll use a static method to allow clients to get a reference to the single instance and a private constructor Monday, 19 November, 12
  7. public class Singleton { / / The private reference to

    the one and only instance. private static Singleton instance = null; /** * The Singleton Constructor. * Note that it is private! * No client can instantiate a Singleton object! */ private Singleton() {} /** * Returns a reference to the single instance. * Creates the instance if it does not yet exist. * (This is called lazy instantiation.) */ public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } Monday, 19 November, 12
  8. Discussion Note that the singleton instance is only created when

    needed. This is called lazy instantiation. What if two threads concurrently invoke the instance() method? Any problems? Two instances of the singleton class could be created! How could we prevent this? Make the instance() synchronized. Synchronization is expensive, however, and is really only needed the first time the unique instance is created. Do an eager instantiation of the instance rather than a lazy instantiation Monday, 19 November, 12
  9. public class Singleton { / / The private reference to

    the one and only instance. / / Let’s eagerly instantiate it here. private static Singleton instance = new Singleton(); /** * The Singleton Constructor. * Note that it is private! * No client can instantiate a Singleton object! */ private Singleton() {} /** * Returns a reference to the single instance. * Creates the instance if it does not yet exist. * (This is called lazy instantiation.) */ public static Singleton getInstance() { return instance; } } Eager instantiation This is guaranteed to be thread safe Monday, 19 November, 12
  10. Interface In English, an interface is a device or system

    that unrelated entities use to interact. In fact, other object-oriented languages have the functionality of Java's interfaces, but they call their interfaces protocols (like in objective-C). Monday, 19 November, 12
  11. Interface A Java interface defines a set of methods but

    does not implement them. A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior. e.g. View.OnClickListener a class that implements onClickListener conforms the agreement that itself is able to handle click event. Monday, 19 November, 12