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