Sysprog 10

500 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
500
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Sysprog 10

  1. 1. C/C++ For Linux <ul><ul><li>Session 10 </li></ul></ul><ul><ul><li>C++ - Session 5 </li></ul></ul>
  2. 2. Outline <ul><li>STL Continued </li></ul><ul><ul><li>Functors, algorithms, strings, streams </li></ul></ul><ul><li>Very brief intro to boost </li></ul><ul><li>Some design patterns </li></ul>
  3. 3. Functor <ul><li>Abstraction of anything callable </li></ul><ul><li>Operator() </li></ul><ul><li>Templatized by argument types </li></ul><ul><li>Generator, unary, binary </li></ul>
  4. 4. Adapters <ul><li>An interface adapter </li></ul><ul><li>binder </li></ul><ul><li>mem_fun </li></ul><ul><li>ptr_fun </li></ul>
  5. 5. Predicates <ul><li>A functor with a bool return value </li></ul><ul><li>Why? </li></ul><ul><ul><li>Containers - Utilities </li></ul></ul><ul><ul><li>algorithms </li></ul></ul>
  6. 6. Common Algorithms <ul><li>find/search/equal_range </li></ul><ul><li>replace/remove_if </li></ul><ul><li>foreach </li></ul><ul><li>sort </li></ul>
  7. 7. Strings <ul><li>Abstracts pointer manipulation </li></ul><ul><li>Concat + </li></ul><ul><li>Construct with char * </li></ul><ul><li>c_str() </li></ul><ul><li>Find, replace </li></ul><ul><li>iterator </li></ul>
  8. 8. Streams <ul><li>Flow of characters </li></ul><ul><li>Operators <<,>> </li></ul><ul><li>Stream manipulation </li></ul><ul><li>sstream </li></ul><ul><li>iostream, cout/cin </li></ul><ul><li>Fstream </li></ul><ul><li>Your streams? </li></ul>
  9. 9. Boost <ul><li>www.boost.org </li></ul><ul><li>Smart pointers </li></ul><ul><li>foreach </li></ul><ul><li>Bind </li></ul><ul><li>Format </li></ul><ul><li>any </li></ul>
  10. 10. Design patterns <ul><li>Problem repeats. </li></ul><ul><ul><li>Elements of the problem </li></ul></ul><ul><ul><li>Elements of the solution </li></ul></ul><ul><ul><li>Not code </li></ul></ul><ul><li>Independent of language </li></ul><ul><li>Gamma et al, Design Patterns: Elements of Reusable Object-Oriented Software </li></ul><ul><li>http://www.vincehuston.org/dp/ </li></ul>
  11. 11. Types of Patterns <ul><li>Creational: The construction of objects </li></ul><ul><li>Structural: The mechanisms, the What </li></ul><ul><li>Behavioral: The internals of the object, the How </li></ul><ul><li>Overlap, iterative design </li></ul><ul><li>Interaction between types </li></ul><ul><li>Refactoring </li></ul>
  12. 12. Factory <ul><li>Separate the creation of the object from the code operating on it. </li></ul><ul><li>Abstract Factory: </li></ul><ul><ul><li>Abstract object class + n concrete </li></ul></ul><ul><ul><li>Abstract factory class + n concrete </li></ul></ul><ul><ul><li>Client chooses which to create </li></ul></ul><ul><li>Factory Method: </li></ul><ul><ul><li>Abstract object class + n concrete </li></ul></ul><ul><ul><li>A method (not ctor) creates the object based on parameter </li></ul></ul><ul><ul><li>Perfect when client's choice is passable </li></ul></ul>
  13. 13. Builder <ul><li>Complex creation, could build part or all at different times </li></ul><ul><li>Elements: </li></ul><ul><ul><li>Abstract Object + n concrete </li></ul></ul><ul><ul><li>Abstract Builder + n concrete </li></ul></ul><ul><ul><li>buildPart, buildAll, getObject </li></ul></ul><ul><ul><li>(Client chooses some object or some builder from director) </li></ul></ul><ul><ul><li>(Director guides building) </li></ul></ul><ul><li>Consider data modeling with events </li></ul>
  14. 14. Other Creational <ul><li>Prototype: New based on a copy </li></ul><ul><ul><li>new operator </li></ul></ul><ul><ul><li>copy constructor </li></ul></ul><ul><ul><li>Consider with a factory method </li></ul></ul><ul><li>Singleton: </li></ul><ul><ul><li>GetInstance </li></ul></ul><ul><ul><li>Static methods </li></ul></ul>
  15. 15. Structural – Adapter <ul><li>Impedance matching – two separate interfaces </li></ul><ul><li>Elements: </li></ul><ul><ul><li>Adaptee with interface I1 </li></ul></ul><ul><ul><li>Adapter with interface I2 </li></ul></ul><ul><ul><li>Client uses I2 to perform function of I1 </li></ul></ul><ul><li>Where have we seen this? </li></ul>
  16. 16. Composite <ul><li>Hierarchy, e.g filesystem </li></ul><ul><li>Elements: </li></ul><ul><ul><li>A Component interface </li></ul></ul><ul><ul><li>A leaf, concrete of component </li></ul></ul><ul><ul><li>A composite, concrete of component that has more leaves </li></ul></ul><ul><li>Natural creational pattern to create this? </li></ul>
  17. 17. Proxy <ul><li>Do something behind the scenes </li></ul><ul><li>Elements: </li></ul><ul><ul><li>An abstract interface </li></ul></ul><ul><ul><li>A real and imaginary imp </li></ul></ul><ul><li>Where have we seen this? </li></ul>
  18. 18. Aggregators <ul><li>Facade- Structural </li></ul><ul><ul><li>A simple unified interface for many other objects - delegation </li></ul></ul><ul><ul><li>Where have we seen this? </li></ul></ul><ul><li>Mediator - Behavioral </li></ul><ul><ul><li>A unified interface for other components – interaction </li></ul></ul>
  19. 19. Behavioral – Command <ul><li>A set of objects that get run/executed – decouple the content of cmds from manager </li></ul><ul><li>Elements </li></ul><ul><ul><li>An abstract command – execute </li></ul></ul><ul><ul><li>Different command imps </li></ul></ul><ul><li>Consider device management </li></ul>
  20. 20. Chain of Responsibility <ul><li>Hide the different decisions to act </li></ul><ul><li>Elements </li></ul><ul><ul><li>Base handler – chained </li></ul></ul><ul><ul><li>Derived handlers </li></ul></ul><ul><ul><li>Handlers make a decision and take an action </li></ul></ul><ul><li>Beauracracy </li></ul>
  21. 21. Iterator <ul><li>Decouple iteration from container </li></ul><ul><li>Elements: </li></ul><ul><ul><li>Pointer/Iterator </li></ul></ul><ul><ul><li>Container </li></ul></ul><ul><li>Where have we seen this? </li></ul>
  22. 22. Observer <ul><li>Decouple actions from decision to act – scalability </li></ul><ul><li>Elements: </li></ul><ul><ul><li>Facility/Subject with register & notify </li></ul></ul><ul><ul><li>Abstract observer with update </li></ul></ul><ul><ul><li>Concrete observers register </li></ul></ul><ul><li>Perfect for event handling </li></ul><ul><li>What variant of this have we seen (w/out classes)? </li></ul>
  23. 23. State <ul><li>An OO state machine </li></ul><ul><li>Elements: </li></ul><ul><ul><li>Subject with state and set_state </li></ul></ul><ul><ul><li>Abstract state with trigger </li></ul></ul><ul><ul><li>Concrete states whose triggers lead to action and setting next state </li></ul></ul><ul><li>Devices/Sockets </li></ul><ul><li>Consider a stacked approach - aborts </li></ul><ul><li>Why is this better than a table? </li></ul>
  24. 24. Strategy <ul><li>Decouple action content from action request – Imp-Interface model </li></ul><ul><li>Elements: </li></ul><ul><ul><li>Abstract interface </li></ul></ul><ul><ul><li>Implementations </li></ul></ul><ul><ul><li>Best to choose interfaces outside client (so client stays same) </li></ul></ul><ul><li>Drivers </li></ul><ul><li>Where have you “not” seen this? Where have you done this? </li></ul>

×