lecture 16: singleton pattern

10
LECTURE 16: SINGLETON PATTERN Computer Science 313 – Advanced Programming Topics

Upload: zelig

Post on 23-Feb-2016

45 views

Category:

Documents


0 download

DESCRIPTION

Computer Science 313 – Advanced Programming Topics. Lecture 16: 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 16: Singleton Pattern

LECTURE 16:SINGLETON PATTERN

Computer Science 313 – Advanced Programming Topics

Page 2: Lecture 16: Singleton Pattern

How Can We Code…

Page 3: Lecture 16: Singleton Pattern

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

Page 4: Lecture 16: Singleton Pattern

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

Page 5: Lecture 16: Singleton Pattern

Where To Find Singletons?

Graphic & game engines System.out System.err Device drivers Bars Grocery stores Laundromats Design patterns

Page 6: Lecture 16: Singleton Pattern

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

Page 7: Lecture 16: Singleton Pattern

Singleton Usage

Singleton is harmful when a global variable public static field used for instance Eliminates ability to track updates &

changes Common anti-pattern is using global

variable Anti-patterns are habits that harm

developmentBig Ball Of MudBlowhard JamboreeKill Two Birds With One StoneDesign By Committee

Page 8: Lecture 16: Singleton Pattern

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

Page 9: Lecture 16: Singleton Pattern

Singleton Pattern Guides

DO NOT use for 1 instance wanted

Global value Instead use static

field Suggests poor

design

Check instantiation Use factory method

DO use for 1 instance possible

Global access point For a global

resource Serves as

gatekeeper

Optimize performance Can be easily

removed

Page 10: Lecture 16: Singleton Pattern

For Next Class

Lab #5 on Angel Implement Decorator 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?