91.204.201 Computing IV Singleton Pattern Xinwen Fu.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Design Patterns William A. Hoffman NYU OOP Class.
DESIGN PATTERNS Redesigning Applications And
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Katie C. O’Shea Dennis T. Tillman 11 February 2K2 Flyweight.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Design Pattern Dr. Zhen Jiang West Chester University url:
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
Creational Patterns
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Proxy.
Design Patterns Solving problems with already known solutions Unit - 13.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS616: Software Engineering Spring 2009 Design Patterns Sami Taha.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Stephenson College DP 98 1 Design Patterns by Derek Peacock.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Singleton Pattern Presented By:- Navaneet Kumar ise
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
7 April 2004CSci 210 Spring Design Patterns 2 CSci 210.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns Spring 2017.
How to be a Good Developer
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
object oriented Principles of software design
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Design Patterns Satya Puvvada Satya Puvvada.
Software Engineering Lecture 7 - Design Patterns
Singleton Pattern Pattern Name: Singleton Pattern Context
Chapter 8, Design Patterns Singleton
Presentation transcript:

Computing IV Singleton Pattern Xinwen Fu

PurposeDesign PatternAspect(s) That Can Vary CreationalAbstract Factory (99)families of product objects Builder (110)how a composite object gets created Factory Method (121)subclass of object that is instantiated Prototype (133)class of object that is instantiated Singleton (144)the sole instance of a class StructuralAdapter (157)interface to an object Bridge (171)implementation of an object Composite (183)structure and composition of an object Decorator (196)responsibilities of an object without subclassing Facade (208)interface to a subsystem Flyweight (218)storage costs of objects Proxy (233)how an object is accessed; its location BehavioralChain of Responsibility (251) object that can fulfill a request Command (263)when and how a request is fulfilled Interpreter (274)grammar and interpretation of a language Iterator (289)how an aggregate's elements are accessed, traversed Mediator (305)how and which objects interact with each other Memento (316)what private information is stored outside an object, and when Observer (326)number of objects that depend on another object; how the dependent objects stay up to date State (338)states of an object Strategy (349)an algorithm Template Method (360)steps of an algorithm Visitor (366)operations that can be applied to object(s) without changing their class(es)

By Dr. Xinwen Fu3 Singleton Pattern  Intent Ensure a class has only one instance Provide a global point of access to it  Motivation Sometimes we want just a single instance of a class to exist in the system For example, we want just one window manager We need to have that one instance easily accessible We want to ensure that additional instances of the class can not be created

By Dr. Xinwen Fu4 Structure  How do we implement the Singleton pattern? A private constructor! A static method to allow clients to get a reference to the single instance

By Dr. Xinwen Fu5 Consequences - Benefits  Controlled access to sole instance  Permits a variable number of instances (references)

Access Control and Inheritance  A derived class can access all the non- private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. By Dr. Xinwen Fu6

Friendship and inheritance  In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared.  However, this rule does not affect friends. By Dr. Xinwen Fu7

Example By Dr. Xinwen Fu8 1. #include 2. #include 3. #include 4. #include 5. using namespace std; 6. class Number { 7. public: 8. // 2. Define a public static accessor func 9. static Number *instance(); 10. static void setType(string t) { 11. type = t; 12. delete inst; 13. inst = 0; 14. } 15. virtual void setValue(int in) { 16. value = in; 17. } 18. virtual int getValue() { 19. return value; 20. } 21. // 1. Define a private static attribute 22. private: 23. static string type; 24. static Number *inst; 25. protected: 26. int value; 27. // 4. Define all ctors to be protected 28. Number() { 29. cout << ":ctor: "; 30. } 31. }; 32. string Number::type = "decimal"; 33. Number *Number::inst = 0; 34. class Octal: public Number { 35. // 6. Inheritance can be supported 36. public: 37. friend class Number; 38. void setValue(int in) { 39. char buf[10]; 40. sprintf(buf, "%o", in); 41. sscanf(buf, "%d", &value); 42. } 43. protected: 44. Octal(){} 45. };

By Dr. Xinwen Fu9 46. Number *Number::instance() { 47. if (!inst) 48. // 3. Do "lazy initialization" in the accessor function 49. if (type == "octal") 50. inst = new Octal(); 51. else 52. inst = new Number(); 53. return inst; 54. } 55. int main() { 56. // Number myInstance; - error: cannot access protected constructor 57. // 5. Clients may only use the accessor function to manipulate the Singleton 58. Number::instance()->setValue(42); 59. cout getValue() << endl; 60. Number::setType("octal"); 61. Number::instance()->setValue(64); 62. cout getValue() << endl; 63. }

Review By Dr. Xinwen Fu10

Teaching Evaluation By Dr. Xinwen Fu11