11 Generic Programming and Patterns

download 11 Generic Programming and Patterns

of 27

Transcript of 11 Generic Programming and Patterns

  • 8/13/2019 11 Generic Programming and Patterns

    1/27

    Recapitulation Templates The Standard Template Library Evaluation

    11. Generic Programming and Design Patterns

    8. Juli 2011

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 1 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    2/27

    Recapitulation Templates The Standard Template Library Evaluation

    Outline

    Recapitulation

    Template Programming

    An Overview over the STL

    Design Patterns (skipped)

    Evaluation/feedback

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 2 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    3/27

    Recapitulation Templates The Standard Template Library Evaluation

    11.1.Recapitulation

    Important Concepts (1/2)

    von-Neumann architecture, and, in particular, the linearised memory.

    Definition of an algorithm.

    Interplay of compiler, linker, interpreted programs, and machine language.

    Idea of a variable and its instantiation.

    Overview of built-in data typesplease note the default-value paradigm in contextwith default constructors.

    Termssyntaxandsemantic.

    Comments.

    Assignment statements (equal sign) in combination with case distinction.

    An idea of the rounding error (normalisation process).

    Strict vs. non-strict boolean logic.

    Different variants of branches and loops.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 3 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    4/27

    Recapitulation Templates The Standard Template Library Evaluation

    Important Concepts (2/2)

    Scopes.

    Notion of a function and the three call paradigms.

    Keyword const in different context.

    Recursion and its different types.

    Enums.

    Split-up of an application into header and implementation files. Pointers, arrays, and strings.

    Concept of 0-terminated strings.

    Structures (n-tupels).

    Dynamic memory management.

    Encapsulation.

    Methods instead of functions on structs.

    Constructor and destructor concept.

    Inheritance.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 4 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    5/27

    Recapitulation Templates The Standard Template Library Evaluation

    11.2.Templates

    v o i d swap(i n t & a , i n t & b ) {a = a + b ;b = ab ;a = ab ;

    }

    i n t a , b ;double c , d ;s w a p ( a , b ) ;swap ( c , d ) ;

    We need aswaproutine for doubles as well.

    As we are not allowed to introduce a com-mon superclass of int and double (thiswould be the C# way), we have to do it withoverloading. Analyse your solution. Is therea bad smell in it?

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 5 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    6/27

    Recapitulation Templates The Standard Template Library Evaluation

    A Solution with Overloading

    v o i d swap(i n t & a , i n t & b ) {a = a + b ;b = ab ;a = ab ;

    }

    v o i d swap( double& a , double& b ) {a = a + b ;b = ab ;a = ab ;

    }

    This is duplicated code (bad).

    For each built-in type, we have to write yet another swap routine (worse).

    Now that we are able to overload the comparison operator, we even might have towrite the swap for each user-defined class, e.g. if we are using our classes withina sorted list (worst case).

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 6 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    7/27

    Recapitulation Templates The Standard Template Library Evaluation

    Templates

    t e m p l a t e v o i d swap ( T& a , T& b ) {T c ( a ) ; a=b ; b=c ;

    }

    i n t a , b ;

    s w a p ( a , b ) ; / / o r swap( a , b ) ;

    Templates are a technique to write code for an yet undefined type.

    We instantiate them implicitly whenever we use them.

    Templates have to be written into headers only (its still cut-n-paste).

    The requirements on the datatype (we need a copy-constructor or there has to bea comparison operator) are given implicitly.

    We can resolve disambiguities explicitly.

    Template error messages are difficult to understand (youll see later on).

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 7 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    8/27

    Recapitulation Templates The Standard Template Library Evaluation

    Templates Imply Copy n Paste

    t e m p l a t e v o i d swap ( T& a , T& b ) {T c ( a ) ; a=b ; b=c ;

    }i n t a , b ; double c , d ;s w a p ( a , b ) ;

    swap ( c , d ) ;

    becomes internally

    v o i d swap ( double& a , double& b ) {double c ( a ) ; a=b ; b=c ;

    }v o i d swap ( i n t & a , i n t & b ) {

    i n t c ( a ) ; a=b ; b=c ;}i n t a , b ; double c , d ;s w a p ( a , b ) ;swap ( c , d ) ;

    and maps to overloading, i.e. templates blow up your code.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 8 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    9/27

    Recapitulation Templates The Standard Template Library Evaluation

    Generic Classes

    t e m p l a t e c l as s O r b i t {

    p r i v a t e :T m y Ce l es t ia l Bo d y ;

    p u b l i c :

    O r b i t ( C e l e s t i a l B o d y bo dy ) ;v o i d se tTimeSte p ( double ta u ) ;v o i d co mp u te Ne xtPo sit io n ( ) {

    . . .

    . . . = m y C e l e s t i a l B o d y . g et Ma ss ( ) . . . ;

    . . .}

    }

    O r b i t myOrbit (mySun ) ;

    Whole classes can be defined with respect to an yet undefined data type.

    You can throw in any type (class) that has a getMass()operation.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 9 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    10/27

    Recapitulation Templates The Standard Template Library Evaluation

    Generic Classes vs. Interfaces

    t e m p l a t e c l as s O r b i t {

    p r i v a t e :T m y Ce l es t ia l Bo d y ;

    p u b l i c :

    O r b i t ( C e l e s t i a l B o d y bo dy ) ;v o i d se tTimeSte p ( double ta u ) ;v o i d co mp u te Ne xtPo sit io n ( ) {

    . . .

    . . . = m y C e l e s t i a l B o d y . g et Ma ss ( ) . . . ;

    . . .}

    }

    From an OO point of view, it would be better to make Orbitexpect animplementation of an interface with the operation getMass(). However,

    then it would not work with built-in types, and

    we often dont want hundreds of classes inherit from the same interface.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 10 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    11/27

    Recapitulation Templates The Standard Template Library Evaluation

    Technical Classes

    t e m p l a t e cl as s OrderedSequence {

    p r i v a t e :T sequenceElements ;

    p u b l i c :

    v o i d a d dE n t ry ( T n ew En tr y ) {i n s e r t and do bu bb le s o r t

    }}

    Such a class should work for any (built-in) type that has a comparison operator.

    And theres lots of these technical classes (containers in particular).

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 11 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    12/27

    Recapitulation Templates The Standard Template Library Evaluation

    11.3.The Standard Template Library

    namespace st d {t e m p l a t e v o i d swap ( T& a , T& b ) {

    T c ( a ) ; a=b ; b=c ;}

    }

    C++ comes along with a whole bunch of different technical classes

    contained in the namespace std (Standard).

    They are all contained realised as templates (Template),

    and are available with each C++ installation (Library).

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 12 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    13/27

    Recapitulation Templates The Standard Template Library Evaluation

    The Most Famous Generic Stuff

    Strings (yes we can also handle something besides chars).

    Containers

    Iterators

    Standard algorithms (skipped)

    Math

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 13 of 26

    R it l ti T l t Th St d d T l t Lib E l ti

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    14/27

    Recapitulation Templates The Standard Template Library Evaluation

    Strings

    # i n c l u d e

    . . .

    s td : : s t r i n g s0 = T hi s i s a ;s td : : s t r i n g s1 ( t e s t ) ;s td : : s t r i n g s3 = s0 + + s 1 ;

    size(): int

    clear(): void

    empty(): bool

    operator[]: char

    append(string)

    insert(string)

    replace(string,string)

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 14 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    15/27

    Recapitulation Templates The Standard Template Library Evaluation

    ContainersVector

    Acontaineris a data structure storing other objects. Our single linked list, e.g., was acontainer for integers. The STL containers all are templates, i.e. they work with any

    datatype.

    # i n c l u d e

    . . .

    s t d : : v e c t o r myVector ;

    myVector . push back ( 1 . 0 ) ;myVector . push back ( 2 . 0 ) ;myVector . push back ( 3 . 0 ) ;

    i f ( myVector . empty ( ) ) { . . .i f ( m y V e c t o r [ 2 ] = = 4 . 3 ) { . . .

    myVector . c le a r ( ) ;

    The vector represents an array that can grow/shrink on-demand. Almost noone in C++uses pure arrays anymore if theres no need to do so. The dynamic memorymanagement (due to new/delete) and a proper copying mechanism are hidden.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 15 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    16/27

    Recapitulation Templates The Standard Template Library Evaluation

    ContainersList

    A vector allows arbitrary elements access and

    grows and shrinks (automatically), however inserting and removing data is expensive as it induces copying.

    # i n c l u d e < l i s t >

    . . .

    s td : : l i s t myList ;

    m y L i s t . p us h b ac k ( 1 . 0 ) ;m y L i s t . p us h b ac k ( 2 . 0 ) ;m y L i s t . p us h b ac k ( 3 . 0 ) ;

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 16 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    17/27

    Recapitulation Templates The Standard Template Library Evaluation

    ContainersStack and Queue

    A stack is a container where you can only add/remove elements at the end (LIFO).

    A queue is a container where you can remove elements only in the order youveinserted them (FIFO).

    vectorbasically gives you everything you need, but the stack and the queue classare faster.

    # i n c l u d e

    . . .

    s t d : : s t ac k myStack ;st d : : queue myQueue;

    myStack . push back ( 1 . 0 ) ; myQueue. push back ( 1 . 0 ) ;myStack . push back ( 2 . 0 ) ; myQueue. push back ( 2 . 0 ) ;myStack . push back ( 3 . 0 ) ; myQueue. push back ( 3 . 0 ) ;

    mySta ck . b ack () ; / / g iv es you a 3. 0 myQueue. f r o n t ( ) ; / / g iv es you a 1 . 0

    myStack . push back ( ) ;myQueue . pop back ( ) ;

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 17 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    18/27

    Recapitulation Templates The Standard Template Library Evaluation

    Associative ContainersMaps

    # i n c l u d e

    . . .

    std : : map myMap;

    myMap [ 1 ] = 0 . 3 ;myMap [ 2 ] = 1 . 2 2 ;myMap[ 16 ] = 5;myMap[4 ] = 9 . 9 9 ;

    i f (myMap. f i n d ( 1 ) ! =myMap. end ( ) ) { . . .

    Access similar to vector, but

    datastructure does not hold list but mappings fromkeys tovalues.

    Theres also variants of this map such as a hash map. Question: Why is it dangerous to have a double as key.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 18 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    19/27

    Recapitulation Templates The Standard Template Library Evaluation

  • 8/13/2019 11 Generic Programming and Patterns

    20/27

    p p p y

    Iterators

    If you wanna traverse an array or a std::vector, you might use a simple for loop. However, most containers neither have an[ . ]operator nor do they provide

    constant-time single element access (see our single linked list). The STL hence introduces the concept of an iterator.

    s t d : : v e c t o r myVector ;

    s t d : : v e c t o r: : i t e r a t o r p = myV ect or . b eg in ( ) ;w h i l e ( p != myVector . end ( ) ) {

    i f ( p = = 0 . 4 ) { . . .p ++;

    }

    Iterators have a signature similar to a pointer traversing an array, but they are classesand hide the implementation of the container.

    s td : : l i s t myVector ;

    s td : : l i s t : : i t e r a t o r p = myV ect or . b eg in ( ) ;w h i l e ( p != myVector . end ( ) ) {

    i f ( p = = 0 . 4 ) { . . .p ++;

    }

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 19 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    21/27

    Scientific computing

    We already used functions such as std::sinor std::sqrt.

    All of them are defined in . Theres also a library for complex numbers (complex),

    and some tools for ranges:

    # i n c l u d e

    double maxOfDouble = s t d : : n u m e r i c l i m i t s: : m a x ( ) ;i n t h ow Ma ny Di gi ts = s t d : : n u m e r i c l i m i t s: : d i g i t s ( ) ;b o o l i sI tS i g n ed = s t d : : n u m er ic l im i ts: : i s s i g n e d ( ) ;double accuracy = s t d : : n um er ic l i m it s: : e p s i l o n ( ) ;

    The classnumeric_limitsdoes exist for each built-in datatype, and it providesexclusively static operations.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 20 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    22/27

    11.4.Evaluation

    Hail to the international students in Munich

    This all leads to ...

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 21 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    23/27

    ... Constructive Criticism

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 22 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    24/27

    About the slides

    The slides are far from complete (and without bugs), however, they shall not andcannot replace any book. Their purpose is to guide you through books, the internet,tutorials, and to highlight some things that are important and are typcially not discussedin (introductionary) literature.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 23 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    25/27

    Literature

    Martin Fowler:Refactoring

    Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides.Design Patterns:Elements of Reusable Software

    Randall Hyde:Write Great Code I: Understanding the machine

    Randall Hyde:Write Great Code II: Thinking Low-level, Writing High-level

    Scott Meyers.Effective C++

    Scott Meyers.More Effective C++

    Walter Savitch.Absolute C++

    Bjarne Stroustrup.The C++ Programming Language

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 24 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    26/27

    Feedback

    I appreciate feedback written down as email if its positive (please CC then the rightpersons, i.e. representatives, professors, the president, ... ;-)). However, perhaps there

    are also things/ideas how we could do something better. For such remarks, it is oftenuseful to discussed them in the group:

    Speed and amount of content as well as level of difficulty.

    Correlation to current research, other courses, and day-to-day business.

    Structure, explanations, and examples.

    Quality of slides, preparation of lecturer, and poise. Would you recommend this lecture because of the content/because of the

    lecturer?

    Are there missing topics that should be discussed?

    Inspired by the Vorlesungsumfrage of TUM.

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 25 of 26

    Recapitulation Templates The Standard Template Library Evaluation

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 11 Generic Programming and Patterns

    27/27

    Finally ...

    Temple Church, London

    Thank you for your participation!

    Tobias [email protected]

    11. Generic Programming and Design Patterns

    Einfuhrung in die ProgrammierungIntroduction to C/C++,Tobias Weinzierl page 26 of 26

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl