C++ осень 2013 лекция 8

Embed Size (px)

Citation preview

  • 1. C++

2. 8. . , GoF 1. -. . 2. : , , , . 3. GoF. 4. . CAP-. . S.O.L.I.D. 5. 6. 2 3. - : - () - . , , : ; ; ; ; ; ( ) 3 4. . - , : ; ; . - , , : 1. . 2. . 3. . 4. . 4 5. 1-2. 5 6. 3-4. 6 7. : (), (). : . () . : , . . : . .7 8. . (., , , ). , (. Programming to interfaces): , ; , . . , . . 8 9. () () . : () class (function) template. design pattern. . (Erich Gamma), . (Richard Helm), . (Ralph Johnson) . (John Vlissides), (. GoF Gang of Four) Design Patterns: Elements of Reusable Object-Oriented Software.9 10. : . GoF, , . : ; ; . : ; (); () .10 11. GoF (5) (8) (11) 11 12. : 12 13. : (. inheritance) (. composition) - , . ? (whitebox reuse) (black-box reuse)? ( )? () () () 13 14. : 14 15. : 15 16. Implementer () () 1 .. *1 .. *0 .. * () ( ) Implementer Implementer Implementer impl; &impl; *impl;16 17. ? (. aggregation) (. acquaintance) . , , . , , , , , , , 17 18. : (. delegation) ( , . delegator) (, . delegate). . ( ) ( ). GoF (, .). . . 18 19. : this ( ) 19 20. : (. immutable object) , , () . . : ; () () . : , ; , ; .20 21. : C++ : ( ); ( const mutable); , ( ) , ( ).21 22. : , (. interface) , () . (ADT) . , . , . 22 23. : 23 24. : : ; , ; , , .24 25. : (, . kit) , () . , : , ; ; .: , , , , . 25 26. : 26 27. : , . , : ; .: , , , , .27 28. : 28 29. : ( , . virtual constructor) , . , : ; , ; .: , , , . 29 30. : 30 31. : , -, . , : ; , ; .: , , .31 32. : 32 33. : (, . , . singleton) , , , . , : ; ; .: .33 34. : class Singleton { public: static Singleton* Instance(); protected: Singleton(); private: static Singleton* _instance; }; Singleton* Singleton::_instance = NULL;Singleton* Singleton::Instance() { if(_instance == NULL) _instance = new Singleton; return _instance; }34 35. : : () , ; , , .35 36. : (, . wrapper) , . , : , ; ; , () , () .: , , .36 37. : 37 38. : (/, . handle/body) , , , . , : ; ; ; ; , .: , , , .38 39. : 39 40. : , . , : ; .: , , , .40 41. : 41 42. : (, . wrapper) , . , : ; , .: , .42 43. : 43 44. : , , , . , : , (); , , , ; () ().: , . 44 45. : (1 / 2) (, . surrogate) , . : ; ; ; ( ) () , , . 45 46. : (2 / 2) , : , .: , .46 47. : 47 48. : : ; ; .48 49. : (, . action; , . transaction) , . , : ; -; , ; .: , , , . 49 50. : 50 51. : template class SimpleCommand : public Command { public: typedef void (Receiver::* Action)(); SimpleCommand(Receiver r, Action a) : _receiver(r), _action(a) { } virtual void Execute(); private: Action _action; Receiver* _receiver; };51 52. : template void SimpleCommand::Execute() { (_receiver->*action)(); } // SomeClass* receiver = new SomeClass; // ... Command* command = new SimpleCommand (receiver, &SomeClass::Action); // ... command->Execute(); 52 53. : (, . cursor) , . , : ; () ; ( ) .: , . 53 54. : (1 / 2) : ; ( ); , , ; , ; , first(), next(), isDone() currentItem(); , ; , (isDone() true). 54 55. : (2 / 2)55 56. ? ( ) ( )56 57. : 57 58. : , . , : ; , ; , ; , , .: , -. 58 59. : 59 60. : (, . dependents; /, . publish/subscribe) , , . , : () , , ; , , ; , .: , .60 61. : 61 62. ? (push) (pull) () 62 63. : , . , : ; , , .: , .63 64. : 64 65. : (, . policy) , , . , : ; , () (); , ; , .: , . 65 66. : 66 67. - template class Context { public: // ... void Operation() { _strategy.DoAlgorithm(); } private: Strategy _strategy; }; class SomeStrategy { public: void DoAlgorithm(); }; // Context context;67 68. : , . () . , : ; () , , ; - (. hook operations).: .68 69. : 69 70. ?- ( ( ) ) 70 71. // void DerivedClass::Operation() { ParentClass::Operation(); // ... } // void ParentClass::Operation() { // ... HookOperation(); } void ParentClass::HookOperation() { } void DerivedClass::HookOperation() { /* ... */ }71 72. : , , . , : , ; ( !), , ; , . , .: , , . 72 73. : 73 74. : : . C++ . : C++ , ( ) -. C++ . 74 75. class Visitor { public: virtual void VisitElementA(ElementA*); virtual void VisitElementB(ElementB*); // ... protected: Visitor(); };75 76. class Element { public: virtual ~Element(); virtual void Accept(Visitor&) = 0; protected: Element(); }; class ElementA : public Element { public: ElementA(); virtual void Accept(Visitor &visitor) { visitor.VisitElementA(this); } };76 77. . , . : ; ; CAP- (. , 2000). 2 3 : (1) ; (2) ; (3) . 77 78. CAP- The CAP Theorem. You can have at most two of these properties for any shared-data system: Consistency, Availability, Tolerance to network Partitions. CA ACID- (., LDAP): ACID , , , ; AP (., DNS); CP , , .78 79. [ ] (. Law of Demeter for Functions / Methods, LoD-F) , - ( ) . , LoD-F : : ; ; , ; . LoD-F .79 80. S.O.L.I.D. (1 / 2) S.O.L.I.D. - , C++ Report . (Robert Martin) 2000- . S.O.L.I.D., 1980 1990- ., : [. ]; / [. (Bertrand Meyer)]; [. (Barbara Liskov) . (Jeannette Wing)]; [. ]; [. ]. 80 81. S.O.L.I.D. (2 / 2) , S[RP] O[CP] / , L[SP] , I[SP] , ( , )D[IP] , 81 82. 6 - , GoF. , .82 83. 84. 84 85. : : () ; ; .: . : ; ; . 85 86. : : , ; , , ; , . : ; ; .86 87. : (1 / 2) : ; (. hook operations); , .87 88. : (2 / 2) : ; : - .88 89. : (1 / 2) : () , ; ; -, - ; .89 90. : (2 / 2) : ( , ) . : ( , ); ( C++) ( , ) ; .90 91. : (1 / 2) : ; ; ; ; , ( ).91 92. : (2 / 2) : ; . : ; ; C++ .92 93. : : ; ; . : ; . 93 94. : : ( ) ( ); ; .94 95. : (1 / 2) : ; ; .95 96. : (2 / 2) : - ( ); ; -, ( ), ; - ; -.96 97. : (1 / 2) : ; ; .: . (): ; . 97 98. : (2 / 2) (): ( !) ; ( , ) ( ) ; ; ; ; (, Z-); , . 98 99. : (1 / 2) : , ; .: , ; , .99 100. : (2 / 2) : () ; , , ; .100 101. : : , ; (), ; . : , ; . 101 102. : , . , (Sic!) : , ; ; ; ( !) .: , , , . 102 103. : 103 104. : : ; , , ; ( !) .: , , . : , ; . 104 105. : : ; : ; : , , (. copy-on-write), ..; : . : ; () . 105 106. : : -; , () ; . : , ; (. ).106 107. : : ; ; .107 108. : (1 / 2) : ; ; ; , .108 109. : (2 / 2) : , , -. : ; , .109 110. : (1 / 2) : , ; ; ; ( ) , () ; .110 111. : (2 / 2) : ; , . : ; ; ; ; ; () ; ; . 111 112. : (1 / 2) : ; , ; ; , ; , ; -.112 113. : (2 / 2) : ; ; : -, ; .113 114. : (1 / 2) : , ; .: , ; .114 115. : (2 / 2) : , , ; - ; ; .115 116. : : , . : ; .116 117. : (1 / 2) : ; ; ( ) , ; , .: ; . 117 118. : (2 / 2) : ; , , .: , .118