1 / 16

Lecture 18: Singleton Implementation

Computer Science 313 – Advanced Programming Topics. Lecture 18: Singleton Implementation. Singleton Pattern. Pattern when system uses 1 instance of the class Allocates only 1 instance during program Constructor private, so cannot allocate second instance

denton
Download Presentation

Lecture 18: Singleton Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 313 – Advanced Programming Topics Lecture 18:Singleton Implementation

  2. Singleton Pattern • Pattern when system uses 1 instance of the class • Allocates only 1 instance during program • Constructor private, so cannot allocate second instance • Methods unrestricted and can be defined however • Similarly, pattern places no limits on fields’ values • Use static method for reference to instance • Since it is staticcan call method anywhere • Could store reference in other classes, but… • … would make tracking uses & debugging difficult

  3. Singleton Pattern Guides Do not use for • 1 instance wanted • Global value • Instead use static field • Poor design suggested • Instantiation check • Use factory method Do use for • 1 instance possible • Global access point • For a global resource • Gatekeeper for actions • Optimize performance • Can be easily removed

  4. Common Themes in Singleton • Every Singleton includes following members • static field holding the instance • To get instance defines publicstaticmethod • Singleton usually includes this member also • Limit instantiation defining private constructor • May include in class, but much rarer • Allow subclassing with protected constructor • Getter as simple factory also found in this case

  5. How To Create Factory Singleton public class GameFactory {private static GameFactoryme;protected GameFactory() { }public static GameFactoryinstance(intuserAge){if (me == null) { if (userAge < 13)me = new AnimatedGameFactory(); else me = new HardCoreGameFactory();} return me;} }

  6. Oops, Our Bad… public class PrinterSpool {private static PrinterSpoolme;privatePrinterSpool() { }public static PrinterSpoolinstance() {if (me == null) {me = new PrinterSpool(); }returnme;} }

  7. Synchronization is Hard • Synchronization veryexpensive • Insures only 1 instance created • Must remain usable or program not helpful • Even best design meaningless if too slow • 4 ways to hide costs of synchronization • Each way shifts where costs occur • Minimize costs for how code used

  8. When Method Rarely Called • Explicit, but expensive, synchronization used public class Engine {private static Engine me;private Engine() { }public static Engine instance() { if (me == null) { me = new Engine(); } return me;} }

  9. When Method Rarely Called • Explicit, but expensive, synchronization used • Synchronized routines performed at each call public class Engine {private static Engine me;private Engine() { }public static synchronized Engine instance() { if (me == null) { me = new Engine(); } return me;} }

  10. When Instantiation is Cheap • Uses system’s implicit synchronization public class Engine {private static Engine me;private Engine() { }public static Engine instance() { if (me == null) { me = new Engine(); } return me;} }

  11. When Instantiation is Cheap • Uses system’s implicit synchronization • Eagerly create instance & keep it until needed public class Engine {private static Engine me = new Engine();private Engine() { }public static Engine instance() { return me;} }

  12. For Use on Any JVM • Also uses system’s implicit synchronization public class Engine {private static Engine me;private Engine() { }public static Engine instance() { if (me == null) { me = new Engine(); } return me;} }

  13. For Use on Any JVM • Also uses system’s implicit synchronization • But delays creation instance until needed public class Engine {private class EngineHolder { private static Engine instance=new Engine();}private static Engine me;private Engine() { }public static Engine instance() { return EngineHolder.instance;} }

  14. On Modern (1.5+) JVMs • Double-locking only costs on instance creation public class Engine {private static Engine me;private Engine() { }public static Engine instance() { if (me == null) { me = new Engine(); } return me;} }

  15. On Modern (1.5+) JVMs • Double-locking only costs on instance creation • Accessing static field expensive for rest of program public class Engine {private volatilestatic Engine me;private Engine() { }public static Engine instance() { if (me == null)synchronized (Engine.class) { if (me == null) { me = new Engine(); }} return me;} }

  16. For Next Class • Lab #4 on Angel • Have 1.5 weeks left to complete this lab • Do not delay, it will take time to complete • Midterm #1 in class on Friday • Open-book, open-note exam (as usual) • Use any lecture’s slides IF you have notes on them • No computers, calculators, or friends, however

More Related