110 likes | 201 Views
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
E N D
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 • 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
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
Consequences - Benefits • Controlled access to sole instance • Permits a variable number of instances (references) By Dr. Xinwen Fu
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
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
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
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
Review By Dr. Xinwen Fu
Teaching Evaluation By Dr. Xinwen Fu