Sysprog 10

24
C/C++ For Linux Session 10 C++ - Session 5

Transcript of Sysprog 10

Page 1: Sysprog 10

C/C++ For Linux

Session 10C++ - Session 5

Page 2: Sysprog 10

Outline

● STL Continued– Functors, algorithms, strings, streams

● Very brief intro to boost● Some design patterns

Page 3: Sysprog 10

Functor

● Abstraction of anything callable● Operator()● Templatized by argument types● Generator, unary, binary

Page 4: Sysprog 10

Adapters

● An interface adapter● binder● mem_fun● ptr_fun

Page 5: Sysprog 10

Predicates

● A functor with a bool return value● Why?

– Containers - Utilities

– algorithms

Page 6: Sysprog 10

Common Algorithms

● find/search/equal_range● replace/remove_if● foreach● sort

Page 7: Sysprog 10

Strings

● Abstracts pointer manipulation● Concat +● Construct with char *● c_str()● Find, replace● iterator

Page 8: Sysprog 10

Streams

● Flow of characters● Operators <<,>>● Stream manipulation● sstream● iostream, cout/cin● Fstream● Your streams?

Page 9: Sysprog 10

Boost

● www.boost.org● Smart pointers● foreach● Bind● Format● any

Page 10: Sysprog 10

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/

Page 11: Sysprog 10

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

Page 12: Sysprog 10

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

Page 13: Sysprog 10

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

Page 14: Sysprog 10

Other Creational

● Prototype: New based on a copy – new operator

– copy constructor

– Consider with a factory method

● Singleton: – GetInstance

– Static methods

Page 15: Sysprog 10

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?

Page 16: Sysprog 10

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?

Page 17: Sysprog 10

Proxy

● Do something behind the scenes● Elements:

– An abstract interface

– A real and imaginary imp

● Where have we seen this?

Page 18: Sysprog 10

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

Page 19: Sysprog 10

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

Page 20: Sysprog 10

Chain of Responsibility

● Hide the different decisions to act● Elements

– Base handler – chained

– Derived handlers

– Handlers make a decision and take an action

● Beauracracy

Page 21: Sysprog 10

Iterator

● Decouple iteration from container● Elements:

– Pointer/Iterator

– Container

● Where have we seen this?

Page 22: Sysprog 10

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)?

Page 23: Sysprog 10

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?

Page 24: Sysprog 10

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?