1 / 12

Lecture 17: Singleton Pattern

Computer Science 313 – Advanced Programming Topics. Lecture 17: Singleton Pattern. How Can We Code…. Why One Is Better. Can create improvement from uniqueness Easy universal access to important resources Error checks and complex locks eliminated

minh
Download Presentation

Lecture 17: Singleton Pattern

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 17:Singleton Pattern

  2. How Can We Code…

  3. Why One Is Better • Can create improvement from uniqueness • Easy universal access to important resources • Error checks and complex locks eliminated • Avoid allocating large number of small objects • Simplifies calculating space-time tradeoffs

  4. One Is The Loneliest Number • Correctness easy with only one object • Trigger for an error is pretty easy to find • Enforcing order of actions much quicker • Problem over which instance dominates is solved

  5. Where To Find Singletons? • Graphic & game engines • System.out • Bars • System.err • Grocery stores • Device drivers • Laundromats • Design patterns

  6. Where To Find Singletons? • Graphic & game engines • System.out • Bars • System.err • Grocery stores • Device drivers • Laundromats • Design patterns

  7. Where To Find Singletons? • Graphic & game engines • System.out • Bars • System.err • Grocery stores • Device drivers • Laundromats • Design patterns

  8. Singleton Pattern • Ensures class has exactly 1 instance • Cannot create second instance of this class • Instance constant, but fields can change • Provides single access point to instance • To ease debugging, do not let reference leak • All uses of class must go through that instance

  9. Singleton Usage • Singleton is harmful when a global variable • publicstatic field used for instance • Eliminates ability to track updates & changes • Common anti-pattern is using global variable • Anti-patterns are habits that harm development Big Ball Of Mud Throwaway Code Kill Two Birds With One Stone Design By Committee

  10. Singleton Pattern • System uses exactly 1 instance of the class • Instantiates only 1 instance during entire program • Guarantees that program uses only single instance • Does not restrict methods in any way • Can define and use fields like normal • Creational pattern like the factory patterns • How we (do not) create instances is focus • Usage unimportant and should not be considered

  11. 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

  12. For Next Class • Lab #4 on Angel • Implement Singleton pattern & a factory • You can choose the factory to implement • Read pages 179 – 186 in the book • How do we implement these Singletons? • What are the myriad ways to screw them up? • Which system bugs are exposed by this pattern?

More Related