Design Patterns in C++

Similar documents
Specification and Evaluation of Machine Translation Toy Systems - Criteria for laboratory assignments

Millersville University Degree Works Training User Guide

PowerTeacher Gradebook User Guide PowerSchool Student Information System

On-Line Data Analytics

DegreeWorks Advisor Reference Guide

Appendix L: Online Testing Highlights and Script

EdX Learner s Guide. Release

TeacherPlus Gradebook HTML5 Guide LEARN OUR SOFTWARE STEP BY STEP

WiggleWorks Software Manual PDF0049 (PDF) Houghton Mifflin Harcourt Publishing Company

WSU Five-Year Program Review Self-Study Cover Page

Using Task Context to Improve Programmer Productivity

Notes on The Sciences of the Artificial Adapted from a shorter document written for course (Deciding What to Design) 1

An OO Framework for building Intelligence and Learning properties in Software Agents

Implementing a tool to Support KAOS-Beta Process Model Using EPF

Objectives. Chapter 2: The Representation of Knowledge. Expert Systems: Principles and Programming, Fourth Edition

Using Blackboard.com Software to Reach Beyond the Classroom: Intermediate

ReinForest: Multi-Domain Dialogue Management Using Hierarchical Policies and Knowledge Ontology

Your School and You. Guide for Administrators

Outreach Connect User Manual

Knowledge based expert systems D H A N A N J A Y K A L B A N D E

University of Groningen. Systemen, planning, netwerken Bosman, Aart

CPS122 Lecture: Identifying Responsibilities; CRC Cards. 1. To show how to use CRC cards to identify objects and find responsibilities

INSTRUCTOR USER MANUAL/HELP SECTION

On Human Computer Interaction, HCI. Dr. Saif al Zahir Electrical and Computer Engineering Department UBC

PESIT SOUTH CAMPUS 10CS71-OBJECT-ORIENTED MODELING AND DESIGN. Faculty: Mrs.Sumana Sinha No. Of Hours: 52. Outcomes

Software Maintenance

New Features & Functionality in Q Release Version 3.2 June 2016

Getting Started Guide

Reviewing the student course evaluation request

Introduction to Moodle

Preferences...3 Basic Calculator...5 Math/Graphing Tools...5 Help...6 Run System Check...6 Sign Out...8

Vorlesung Mensch-Maschine-Interaktion

Evaluation of Usage Patterns for Web-based Educational Systems using Web Mining

Evaluation of Usage Patterns for Web-based Educational Systems using Web Mining

A Context-Driven Use Case Creation Process for Specifying Automotive Driver Assistance Systems

Using SAM Central With iread

Seminar - Organic Computing

Modeling user preferences and norms in context-aware systems

New Features & Functionality in Q Release Version 3.1 January 2016

Intel-powered Classmate PC. SMART Response* Training Foils. Version 2.0

Generating Test Cases From Use Cases

CPS122 Lecture: Identifying Responsibilities; CRC Cards. 1. To show how to use CRC cards to identify objects and find responsibilities

INTERMEDIATE ALGEBRA PRODUCT GUIDE

Achim Stein: Diachronic Corpora Aston Corpus Summer School 2011

An Introduction to Simio for Beginners

TA Certification Course Additional Information Sheet

Developing a TT-MCTAG for German with an RCG-based Parser

PROCESS USE CASES: USE CASES IDENTIFICATION

Longman English Interactive

Setting Up Tuition Controls, Criteria, Equations, and Waivers

M55205-Mastering Microsoft Project 2016

ACCESSING STUDENT ACCESS CENTER

Field Experience Management 2011 Training Guides

Minitab Tutorial (Version 17+)

MOODLE 2.0 GLOSSARY TUTORIALS

Tour. English Discoveries Online

MyUni - Turnitin Assignments

Android App Development for Beginners

Houghton Mifflin Online Assessment System Walkthrough Guide

Office of Planning and Budgets. Provost Market for Fiscal Year Resource Guide

Emporia State University Degree Works Training User Guide Advisor

Rule discovery in Web-based educational systems using Grammar-Based Genetic Programming

Storytelling Made Simple

Skyward Gradebook Online Assignments

Urban Analysis Exercise: GIS, Residential Development and Service Availability in Hillsborough County, Florida

16.1 Lesson: Putting it into practice - isikhnas

arxiv: v1 [cs.cv] 10 May 2017

Perioperative Care of Congenital Heart Diseases

Syntax Parsing 1. Grammars and parsing 2. Top-down and bottom-up parsing 3. Chart parsers 4. Bottom-up chart parsing 5. The Earley Algorithm

Computerized Adaptive Psychological Testing A Personalisation Perspective

SCT Banner Student Fee Assessment Training Workbook October 2005 Release 7.2

Test Administrator User Guide

LEGO MINDSTORMS Education EV3 Coding Activities

Creating Your Term Schedule

Test How To. Creating a New Test

On the Combined Behavior of Autonomous Resource Management Agents

GACE Computer Science Assessment Test at a Glance

AGENDA LEARNING THEORIES LEARNING THEORIES. Advanced Learning Theories 2/22/2016

The Enterprise Knowledge Portal: The Concept

Cognitive Modeling. Tower of Hanoi: Description. Tower of Hanoi: The Task. Lecture 5: Models of Problem Solving. Frank Keller.

Creating a Test in Eduphoria! Aware

Examity - Adding Examity to your Moodle Course

Student User s Guide to the Project Integration Management Simulation. Based on the PMBOK Guide - 5 th edition

Connecting Middle Grades Science and Mathematics with TI-Nspire and TI-Nspire Navigator Day 1

SECTION 12 E-Learning (CBT) Delivery Module

Introduction to Communication Essentials

Bluetooth mlearning Applications for the Classroom of the Future

An Industrial Technologist s Core Knowledge: Web-based Strategy for Defining Our Discipline

Module 12. Machine Learning. Version 2 CSE IIT, Kharagpur

Version Number 3 Date of Issue 30/06/2009 Latest Revision 11/12/2015 All Staff in NAS schools, NAS IT Dept Head of Operations - Education

Conversation Starters: Using Spatial Context to Initiate Dialogue in First Person Perspective Games

Chamilo 2.0: A Second Generation Open Source E-learning and Collaboration Platform

NAME OF ASSESSMENT: Reading Informational Texts and Argument Writing Performance Assessment

STUDENT MOODLE ORIENTATION

Lecture 10: Reinforcement Learning

The D2L eportfolio for Teacher Candidates

Creating an Online Test. **This document was revised for the use of Plano ISD teachers and staff.

Java Programming. Specialized Certificate

Experiments with SMS Translation and Stochastic Gradient Descent in Spanish Text Author Profiling

IT4305: Rapid Software Development Part 2: Structured Question Paper

Transcription:

Design Patterns in C++ Behavioural Patterns Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa March 13, 2011 G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 1 / 59

What are Behavioural Patterns Behavioural patterns describe patterns of message communications between objects Therefore, they are concerned with algorithms, rather than with structures Of course, behavioural patterns are strictly related to structural patterns Key observation: how the objects know about each other? Main goal: decouple objects from each other to allow a dynamic and flexible configurations of algorithms and methods G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 2 / 59

Outline 1 Observer 2 Chain of responsibility 3 Visitor 4 Interpret 5 Command 6 State 7 Stategy G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 3 / 59

Motivation We need to maintain consistency among (weakly-)related object When something happens to an object, other objects must be informed Typical example in GUIs The Document object must be informed when a button Print is clicked, so that the print() operation can be invoked The ViewPort object must be informed when the window is resized(), so that it can adjust the visualization of the objects We have already presented an example when presenting the Adapter pattern: an object can listen to other objects changes Participants: An object changes its state (subject) Another object wants to be informed (observer) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 4 / 59

UML Diagram Subject is the interface for something to to be observed Observer is thing that observes G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 5 / 59

Message sequence chart G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 6 / 59

Example The user resizes a window: every component of the window needs to be informed of a resize operation (viewport, scrollbars, toolbars, etc.) in this way, every object can synchronize its state with the new window size Solution: The window can install observers All components (viewport, scrollbar, etc.) can attach an observer to the main window that is informed when a resize operation is under way The observer asks for the current size of the window, and invoke methods on the objects to adjust their state (size) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 7 / 59

Consequences Abstract coupling between subject and observer all that a subject knows is that there is a list of observers, but it does not know anything about the observers themselves the observer instead must know the subjects Broadcast communication There can be many independent observers, with different purposes and hierarchies Example: resizing a window can affect the viewports inside the window, the scrollbars, etc. G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 8 / 59

Consequences Abstract coupling between subject and observer all that a subject knows is that there is a list of observers, but it does not know anything about the observers themselves the observer instead must know the subjects Broadcast communication There can be many independent observers, with different purposes and hierarchies Example: resizing a window can affect the viewports inside the window, the scrollbars, etc. Unexpected updates A seemingly innocuous operation on the subject may cause a cascade of updates on the observers and their dependent objects, many of them may not care about any update This simple protocol does not tell the observer what change happened to the subject (a resize? a move?) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 8 / 59

Update problems Pull model the subject sends nothing the observer askes for details about the changes equivalent to what we have already seen Push model the subject sends the observer detailed information about the change (whether it wants it or not) the observer can understand if he is interested in the change by analysing this additional parameter Specifying events By complicating the protocol, it is possible to register to specific aspects onresize() onmove(),... more efficient, but more complex interface G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 9 / 59

Extensions It is possible to efficiently and effectively use templates for extending as much as possible to usage of the observer pattern the first extension we consider is to have an observer that wants to observe different subjects however, in the standard patterns, only one subject is possible we could have different pointers inside the ConcreteObserver class, however the update takes no parameter to understand which subject has changed, we need to pass a parameter to the update we could pass a simple integer, 0 meaning the first subject, 1 the second subject, and so on however, the subject must know its number for the specific observer; and the observer has to implement a switch case not very scalable My solution is to use one more class, that connects subject with observer G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 10 / 59

UML diagram See the code in observer example G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 11 / 59

Outline 1 Observer 2 Chain of responsibility 3 Visitor 4 Interpret 5 Command 6 State 7 Stategy G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 12 / 59

Motivation Consider a context-sensitive help for a GUI the user can click on any part of the interface and obtain help on it The help that is actually provided depends on which part of the interface (button, menu, etc.) the context (where the button is) Example: a button in a dialog box a button in the main window If no help can be found for that part, a more general help page is shown G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 13 / 59

Motivation Consider a context-sensitive help for a GUI the user can click on any part of the interface and obtain help on it The help that is actually provided depends on which part of the interface (button, menu, etc.) the context (where the button is) Example: a button in a dialog box a button in the main window If no help can be found for that part, a more general help page is shown The help should be organized hierarchically From more general to more specific The object that provides the help is not known to the object that initiates the request for help the button does not know which help object will handle the request, as this depends on the context G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 13 / 59

Goals and requirements Decouple senders and receivers Gives multiple objects a chance to handle the request Chain: build a list of receivers pass the request to the first receiver if the request cannot be handled, pass it to the next receiver in the chain Consequences Reduced coupling: the sender does not care which object handles the request Added flexibility in assigning responsibility: different responsibility can be distributed to different objects Receipt is not guaranteed: there is not guarantee that eventually some object will handle the request G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 14 / 59

UML diagram G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 15 / 59

Example Instance G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 16 / 59

Notes Applicability More than one object can handle a request, and the handler is not known a priori you want to issue a request to one of several objects without specifying the receiver explicitly the set of objects that can handle a request should be specified dynamically Implementation Connecting successors: the Handler class itself usually maintains a link to the successor. Also, it automatically forwards allow requests by default if there is a successor. Representing requests: usually represented in the method call itself (i.e. handlehelp()). However, we could think of one or more parameters to encode the specific request. to simplify the passage of parameters, we could also encode them into an object that is passed along the chain G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 17 / 59

Outline 1 Observer 2 Chain of responsibility 3 Visitor 4 Interpret 5 Command 6 State 7 Stategy G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 18 / 59

Motivation Consider a compiler that internally represents a program as an abstract syntax tree the compiler will take as input a text file containing the program the parser component will read the file and build the syntax tree then it performs syntax checking on the tree for example, it checks that all used variables have actually been defined, and that the type corresponds it will also need to generate code optionally, it can need to print the program in a nice formatted way In general, on a complex structure, you may need to define several distinct operations The structure may consists of different types of nodes (see the Composite pattern) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 19 / 59

Naive approach Let s define a method for each operation in the node itself G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 20 / 59

Naive approach Let s define a method for each operation in the node itself Not correct. Why? G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 20 / 59

The problem Does not scale: what if we want to implement one more operation to visit the node? We need to change all the Node classes Also, we are doing many things to do in a single class The Node class should care about the structure, and to provide a generic interface to all types of nodes Node typically implements a Composite pattern What we need to do Decouple visiting from Nodes. Solution: use a different class to encapsulate the various visiting operations G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 21 / 59

The Visitors These classes take care of visiting the Nodes, and doing the appropriate operations each concrete visitor implements a different kind of visit G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 22 / 59

The Nodes to be visited Now the Node class is much simples it only need to provide a hook for allowing visitors to visit it G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 23 / 59

Generic UML diagram G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 24 / 59

Message Sequence Chart G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 25 / 59

Applicability Use the Visitor pattern when an object structure contains many classes of objects with different interfaces, and you want to perform operations on the elements of the structure many different operations needs to be performed on objects in a structure, and you want to avoid putting such operations on the objects (decoupling) the classes defining the object structure rarely or never change G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 26 / 59

Consequences Visitor makes adding new operations easier Adding a new ConcreteElement is hard similar to an Iterator, however the Iterator visits elements of the same type, while visitor traverses structure of objects of different types Accumulating State: since the visitor is an object, while visiting it can accumulate state, or cross-check the structure G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 27 / 59

Implementation techniques The visitor is able to understand the type of the element it is visiting using the technique called double dispatch. Single dispatch: the operation to be invoked depends on the type of the object (or of the pointer), and on the parameter list in object oriented slang, we say that it depends on the message type (the method) and on the receiver (the object) type Double Dispatch: The operation that is invoked depends on the message type (the method) and on two receivers accept() is a double-dispatch operation, because the final method that is called depends both on the visitor type and the element type the technique used for the template observer is quite similar: which operation is invoked depends on the message type (update), on the receiver (the observer) and on the subject (parameter of the update) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 28 / 59

Who performs the visit? Different techniques The object structure the Visitor A separate object (an Iterator) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 29 / 59

Outline 1 Observer 2 Chain of responsibility 3 Visitor 4 Interpret 5 Command 6 State 7 Stategy G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 30 / 59

Motivation Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language In many cases it is useful to define a small language to define things that need to be expressed easily Examples where a simple language may be useful Configuration files for creating objects List of complex parameters Rules to configure filters, etc. If the language is complex (for example, a scripting or programming language), it is better to use classical tools like parser generators However, when we want to implement a simple thing, then it may be useful to do it by hand in C In the following example, we will assume to build a simple interpreter for regular expressions G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 31 / 59

Example Grammar rules expression ::= literal alternation sequence repetition ( expression ) alternation ::= expression expression sequence ::= expression & expression repetition ::= expression * literal :: = a b c... { a b c... }* Expression is the starting rule Literal is a terminal symbol G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 32 / 59

Abstract Syntax Tree To implement the previous grammar, we prepare a class for each rule each class derives from an abstract class at the end of the parsing we must obtain an abstract syntax tree that will represent the expression G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 33 / 59

UML representation G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 34 / 59

How the interpret works The abstract syntax tree must be built by a parser (not part of this pattern) once the tree is built, we can use it in our program. For example, we could pass the interpret a sequence of characters, and it will tell us it the sequence respects the regular expression we would also pretty-print the expression, or transform it into another representation (for example a finite state machine) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 35 / 59

UML representation A general UML representation is the following G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 36 / 59

Participants AbstractExpression (RegularExpression) it represents the abstract interface for the node in the tree TerminalExpression (LiteralExpression) Represents the leaf of the tree, cannot be further expanded NonTerminalExpression (SequenceExpression, AlternationExpression, etc.) this class represents a rule in the grammar it is also an intermediate node in the tree, can contain children Context Contains global information useful for the interpret Client builds the abstract syntax tree via a parser calls the interpreter operation to carry on the interpretation of the language G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 37 / 59

Consequences It s easy to change and extend the grammar appropriate classes can be written, existing classes appropriately modified Easy to implement the grammar Classes are easy to write and often their generation can be automated by a parser generator Complex grammars are hard to maintain When the number of rules is large, you need a lot of classes also, not very efficient to execute Adding new ways to interpret expressions Since you have the tree, you can do many things with it by using a Visitor pattern, you can easily add new operations without modifying the classes G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 38 / 59

Example of parser In the code G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 39 / 59

Outline 1 Observer 2 Chain of responsibility 3 Visitor 4 Interpret 5 Command 6 State 7 Stategy G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 40 / 59

Motivation Sometimes it is necessary to issue requests to objects without knowing anything about the operation being requested, or the receiver of the request Example: when pressing a button, something happens that is not related or implemented to the Button class In many cases, exactly the same operation can be performed by a menu item, or by a button in a toolbar We want to encapsulate commands into objects This patterns is the OO equivalent of C callbacks Other uses Undo/redo of commands Composing commands (macros) G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 41 / 59

UML example G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 42 / 59

Implementing macros G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 43 / 59

General UML structure G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 44 / 59

Undo/Redo It is not always possible The operation should be reversible Need to add and undo() operation in the Command abstract class The command may need to carry additional state of the receiver inside We need an history list (how far should we go with the history?) Using prototype We could use a Prototype pattern to create copies of commands, customize with the internal state of the receiver, and then save the copy on the history G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 45 / 59

Differences with callbacks Commands are objects, not just functions They can carry state information on the receivers They can carry information on the history itself The Invoker only needs to know the general interface of the command (execute()), not the specific internal information (i.e. parameters, etc.) which are decided at creation time G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 46 / 59

Outline 1 Observer 2 Chain of responsibility 3 Visitor 4 Interpret 5 Command 6 State 7 Stategy G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 47 / 59

The State pattern Allow an object to alter its behaviour when its internal state changes. The object will appear to change its class. This pattern is useful to implement simple state machines The idea is to implement each state with a different class, and each event with a different method Consider a library to implement the TCP protocol A TCPConnection can be in one of several different states For example, the connection can be void, established, closing, etc. the response to a request of open depends on the current state of the connection: only if the connection is not yet established we can open it this behaviour can be implemented as follows: An abstract class TCPState that implements a method for each possible request the derived classes represent the possible states only some of them will respond to a certain request G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 48 / 59

Example G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 49 / 59

Applicability Use the State pattern in one of the following cases an object behaviour depends on its state, that will change at run-time operations have large, multi-part, conditional statements that depend on the object state. This state is usually represented by one or more enumerated constants G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 50 / 59

UML diagram G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 51 / 59

Participants Context (TCPConnection) defines the interface of interest to clients maintains an instance of a ConcreteState subclass that defines the current state through a pointer to the abstract State class State (TCPState) defines and interface for encapsulating the behaviour associated with a particular state of the Context ConcreteState subclasses each subclass implements a behaviour associated with a state of the Context G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 52 / 59

Consequences Localizes state-specific behaviour and partitions behaviour for different states. All behaviour associate with a particular state is concentrate into a single class (ConcreteState). new states and transitions can be easily added the pattern then avoid large if/then/else conditional instructions however, distributing information in state classes may not be appropriate for complex behaviour, because it increases the amount of interaction and dependencies between classes it makes state transitions explicit. a transition is a change in the state object, therefore is quite visible G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 53 / 59

Outline 1 Observer 2 Chain of responsibility 3 Visitor 4 Interpret 5 Command 6 State 7 Stategy G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 54 / 59

Strategy Define a family of algorithms, encapsulate each one, and make then interchangeable. Strategy lets the algorithms vary independently from clients that use it In general, it is useful to delegate an algorithm to a function, instead of embedding it into the normal code we make the algorithm general and reusable we can easily change the algorithm by substituting the function In object oriented programming, objects can be used instead of functions An example: many algorithms exist for breaking a stream of text into lines hard-wiring them into the class that uses them is undesirable, because it goes against the single-responsibility principle therefore, we could define an hierarchy of function objects that behave like functions G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 55 / 59

UML diagram G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 56 / 59

Structure SimpleCompositor implements a simple strategy that determines linebreaks one at a time TeXCompositor implements the TeX algorithm for finding linebreaks. This strategy tries to optimize linebreaks globally, that is one paragraph at a time ArrayCompositor implements a strategy that selects breaks so that each row has a fixed number of items. It s useful for breaking a collection of icons into rows, for example G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 57 / 59

Structure SimpleCompositor implements a simple strategy that determines linebreaks one at a time TeXCompositor implements the TeX algorithm for finding linebreaks. This strategy tries to optimize linebreaks globally, that is one paragraph at a time ArrayCompositor implements a strategy that selects breaks so that each row has a fixed number of items. It s useful for breaking a collection of icons into rows, for example A Composition maintains a reference to a Compositor object we can change strategy both at compile time and at run-time Why using classes instead of functions? Objects can carry state, while functions can t G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 57 / 59

In C++ In C++ you can define a method without name, through the operator() class MyFunctor {... public: MyFunctor(); double operator() {...}... }; MyFunctor a;... double result = a(); // equivalent to // result = a.operator(); You can also pass parameters to the operator, and overload it G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 58 / 59

Strategy as template parameters Using this technique, a class can be easily parametrised through a template instead than by inheritance template <class Functor> class Context { Functor f; public:... void operation() { f(); } }; G. Lipari (Scuola Superiore Sant Anna) Behavioural patterns March 13, 2011 59 / 59