1 / 11

91.204.201 Computing IV

91.204.201 Computing IV . Singleton Pattern Xinwen Fu. 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

job
Download Presentation

91.204.201 Computing IV

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. 91.204.201 Computing IV Singleton Pattern Xinwen Fu

  2. 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 Fu

  3. 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 Fu

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

  5. 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 Fu

  6. 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 Fu

  7. Example protected: int value; // 4. Define all ctors to be protected Number() { cout << ":ctor: "; } }; string Number::type = "decimal"; Number *Number::inst = 0; class Octal: public Number { // 6. Inheritance can be supported public: friend class Number; void setValue(int in) { char buf[10]; sprintf(buf, "%o", in); sscanf(buf, "%d", &value); } protected: Octal(){} }; • #include <iostream> • #include <string> • #include <stdlib.h> • #include <stdio.h> • using namespace std; • class Number { • public: • // 2. Define a public static accessor func • static Number *instance(); • static void setType(string t) { • type = t; • delete inst; • inst = 0; • } • virtual void setValue(int in) { • value = in; • } • virtual int getValue() { • return value; • } • // 1. Define a private static attribute • private: • static string type; • static Number *inst; By Dr. Xinwen Fu

  8. Number *Number::instance() { • if (!inst) • // 3. Do "lazy initialization" in the accessor function • if (type == "octal") • inst = new Octal(); • else • inst = new Number(); • return inst; • } • int main() { • // Number myInstance; - error: cannot access protected constructor • // 5. Clients may only use the accessor function to manipulate the Singleton • Number::instance()->setValue(42); • cout << "value is " << Number::instance()->getValue() << endl; • Number::setType("octal"); • Number::instance()->setValue(64); • cout << "value is " << Number::instance()->getValue() << endl; • } By Dr. Xinwen Fu

  9. Review By Dr. Xinwen Fu

  10. Teaching Evaluation By Dr. Xinwen Fu

More Related