Presentation is loading. Please wait.

Presentation is loading. Please wait.

91.204.201 Computing IV Singleton Pattern Xinwen Fu.

Similar presentations


Presentation on theme: "91.204.201 Computing IV Singleton Pattern Xinwen Fu."— Presentation transcript:

1 91.204.201 Computing IV Singleton Pattern Xinwen Fu

2 CS@UML 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)

3 CS@UML 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

4 CS@UML 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

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

6 CS@UML 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

7 CS@UML 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

8 CS@UML 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. };

9 CS@UML 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. }

10 CS@UML Review By Dr. Xinwen Fu10

11 CS@UML Teaching Evaluation By Dr. Xinwen Fu11


Download ppt "91.204.201 Computing IV Singleton Pattern Xinwen Fu."

Similar presentations


Ads by Google