22-singletoncommandadapter

22-singletoncommanda - Singleton Command Adaptor Kenneth M Anderson University of Colorado Boulder CSCI 4448/5448/05/2009 University of Colorado

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 22 — 11/05/2009 © University of Colorado, 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Goals • Cover Material from Chapters 5 — 7 of the Design Patterns Textbook • Singleton Pattern • Command Pattern • Adaptor Pattern • Facade Pattern 2
Background image of page 2
Singleton Pattern: Defnition • The Singleton Pattern ensures a class has only one instance (or a constrained set of instances), and provides a global point of access to it • Useful for objects that represent real-world resources, such as printers, in which you want to instantiate one and only one object to represent each resource • Also useful for “management” code, such as a thread/connection pool • At Frst, Singleton may seem difFcult to achieve… typically, once you deFne a class, you can create as many instances as you want • ±oo f = new ±oo(); ±oo f1 = new ±oo(); ±oo f2 = new ±oo()… • The key (in most languages) is to limit access to the class’s constructor, such that only code in the class can invoke a call to the constructor (or initializer or <insert code that creates instances here>) • Indeed, as you will see, different languages achieve the Singleton pattern in different ways 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Singleton Pattern: Structure static getInstance() : Singleton private Singleton() static my_instance : Singleton Singleton Singleton involves only a single class (not typically called Singleton). That class is a full-Fedged class with other attributes and methods (not shown) The class has a static variable that points at a single instance of the class. The class has a private constructor (to prevent other code from instantiating the class) and a static method that provides access to the single instance 4
Background image of page 4
World’s Smallest Java-based Singleton Class public class Singleton { 1 2 private static Singleton uniqueInstance; 3 4 private Singleton() {} 5 6 public static Singleton getInstance() { 7 if (uniqueInstance == null ) { 8 uniqueInstance = new Singleton(); 9 } 10 return uniqueInstance; 11 } 12 } 13 14 Meets Requirements: static var, static method, private constructor Example source has this class in ken/smallest augmented with test code 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
World’s Smallest Python-Based Singleton Class class Singleton(object): 1 2 _instance = None 3 4 def __new__(cls, *args, **kwargs): 5 if not cls._instance: 6 cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) 7 return cls._instance 8 9 if __name__ == '__main__' : 10 a = Singleton() 11 b = Singleton() 12 13 print "a = %s" % (a) 14 print "b = %s" % (b) 15 16 Different Approach: static var, override constructor only 8 lines of code! Example source has this class in ken/smallest 6
Background image of page 6
World’s Smallest Ruby-based Singleton Class require 'singleton' 1 2 class Example 3 include Singleton 4 end 5 6 a = Example.instance 7 b = Example.instance 8 9 puts "a = #{a}" 10 puts "b = #{b}" 11 12 c = Example.new 13 14 Yet a different approach, using a mechanism in Ruby called a “mixin”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/11/2010 for the course CSCI 5448 taught by Professor Anderson during the Fall '09 term at Colorado.

Page1 / 40

22-singletoncommanda - Singleton Command Adaptor Kenneth M Anderson University of Colorado Boulder CSCI 4448/5448/05/2009 University of Colorado

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online