{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

22-singletoncommandadapter

22-singletoncommandadapter - Singleton Command Adaptor...

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

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

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

View Full Document Right Arrow Icon
Lecture Goals • Cover Material from Chapters 5 — 7 of the Design Patterns Textbook • Singleton Pattern • Command Pattern • Adaptor Pattern • Facade Pattern 2
Image of page 2
Singleton Pattern: Definition • 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 first, Singleton may seem difficult to achieve… typically, once you define a class, you can create as many instances as you want • Foo f = new Foo(); Foo f1 = new Foo(); Foo f2 = new Foo()… • 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
Image of page 3

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

View Full Document Right 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-fledged 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
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
Image of page 5

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

View Full Document Right 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
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” The “include Singleton” statement causes the Example class to be modified such that its new() method becomes private and an instance() method is added to retrieve an instance. As a bonus, it
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern