DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA

  • Slides: 59
Download presentation
DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY C++ Design Patterns Joey Paquet,

DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY C++ Design Patterns Joey Paquet, Emil Patterns Vassev. C++ Design 1 2007, 2011, 2013

Outline § Introduction to Design Patterns Pattern’s Elements § Types of Design Patterns §

Outline § Introduction to Design Patterns Pattern’s Elements § Types of Design Patterns § § C++ Design Patterns Creational Patterns § Structural Patterns § Behavioral Patterns § C++ Design Patterns 2

Design Patterns : : What is a Design Pattern? “Each pattern describes a problem

Design Patterns : : What is a Design Pattern? “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. ” [1] [Christopher Alexander – late 1970 s] A Pattern Language § A Timeless Way of Building § Design patterns capture the best practices of experienced objectoriented software developers. Design patterns are solutions to general software development problems. C++ Design Patterns 3

Design Patterns : : Pattern’s Elements – Pattern Name In general, a pattern has

Design Patterns : : Pattern’s Elements – Pattern Name In general, a pattern has four essential elements. § § The pattern name The problem The solution The consequences C++ Design Patterns 4

Design Patterns : : Pattern’s Elements – The Pattern Name The pattern name is

Design Patterns : : Pattern’s Elements – The Pattern Name The pattern name is a handle we can use to describe a design problem, its solutions, and consequences in a word or two. Naming a pattern immediately increases the design vocabulary. It lets us design at a higher level of abstraction. § Having a vocabulary for patterns lets us talk about them. § It makes it easier to think about designs and to communicate them and their trade-offs to others. § C++ Design Patterns 5

Design Patterns : : Pattern’s Elements – The Problem The problem describes when to

Design Patterns : : Pattern’s Elements – The Problem The problem describes when to apply the pattern. It explains the problem and its context. § It might describe specific design problems such as how to represent algorithms as objects. § It might describe class or object structures that are symptomatic of an inflexible design. § Sometimes the problem will include a list of conditions that must be met before it makes sense to apply the pattern. § C++ Design Patterns 6

Design Patterns : : Pattern’s Elements – The Solution The solution describes the elements

Design Patterns : : Pattern’s Elements – The Solution The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations. The solution doesn't describe a particular concrete design or implementation, because a pattern is like a template that can be applied in many different situations. § Instead, the pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects in our case) solves it. § C++ Design Patterns 7

Design Patterns : : Pattern’s Elements – The Consequences The consequences are the results

Design Patterns : : Pattern’s Elements – The Consequences The consequences are the results and trade-offs of applying the pattern. The consequences for software often concern space and time trade-offs. § They may address language and implementation issues as well. § Since reuse is often a factor in object-oriented design, the consequences of a pattern include its impact on a system's flexibility, extensibility, or portability. § C++ Design Patterns 8

Design Patterns : : Types Erich Gamma, Richard Helm, Ralph Johnson and John Vlisides

Design Patterns : : Types Erich Gamma, Richard Helm, Ralph Johnson and John Vlisides in their Design Patterns [gang of four] book define 23 design patterns divided into three types: § Creational patterns - create objects for you, rather than having you instantiate objects directly. This gives your program more flexibility in deciding which objects need to be created for a given case. § Structural patterns - help you compose groups of objects into larger structures, such as complex user interfaces or accounting data. § Behavioral patterns - help you define the communication between objects in your system and how the flow is controlled in a complex program. C++ Design Patterns 9

C++ Design Patterns : : Creational Patterns and C++ I § § The creational

C++ Design Patterns : : Creational Patterns and C++ I § § The creational patterns deal with the best way to create instances of objects. In C++, the simplest ways to create an instance of an object is by using the new operator or by simply declaring the variable in the local scope: Fred* fred 1 = new Fred(); //instance of Fred class Fred fred 2 ; //instance of Fred class § § The amount of hard coding depends on how you create the object within your program. In many cases, the exact nature of the object that is created could vary with the needs of the program and abstracting the creation process into a special “creator” class can make your program more flexible and general. C++ Design Patterns 10

C++ Design Patterns : : Creational Patterns and C++ II § § § The

C++ Design Patterns : : Creational Patterns and C++ II § § § The Factory Pattern provides a simple decision making class that returns one of several possible subclasses of an abstract base class depending on the data that are provided. The Abstract Factory Pattern provides an interface to create and return one of several families of related objects. The Builder Pattern separates the construction of a complex object from its representation. The Prototype Pattern starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances. The Singleton Pattern is a class of which there can be no more than one instance. It provides a single global point of access to that instance. C++ Design Patterns 11

The Factory Pattern : : How does it Work? The Factory pattern returns an

The Factory Pattern : : How does it Work? The Factory pattern returns an instance of one of several possible classes depending on the data provided to it. § § § Here, x is a base class and classes xy and xz are derived from it. The Factory is a class that decides which of these subclasses to return depending on the arguments you give it. The get. Class() method passes in some value abc, and returns some instance of the class x. Which one it returns doesn't matter to the programmer since they all have the same methods, but different implementations. C++ Design Patterns 12

The Factory Pattern : : The Shape Factory class Shape. Factory { public: //

The Factory Pattern : : The Shape Factory class Shape. Factory { public: // Static method to create objects // Change is required only in this function static Shape* Create(string type){ if ( type == "circle" ) return new Circle(); if ( type == "square" ) return new Square(); return NULL; } private: //Constructor is made private to prevent instantiation Shape. Factory(){}; }; C++ Design Patterns 13

The Factory Pattern : : The Shape and Subclasses //Superclass of all object that

The Factory Pattern : : The Shape and Subclasses //Superclass of all object that the Factory can create class Shape { public: //common interface to all subtypes virtual void draw() = 0; }; class Circle : public Shape { public: void draw() { cout << "I am circle" << endl; } }; class Square : public Shape { public: void draw() { cout << "I am square" << endl; } }; C++ Design Patterns 14

The Factory Pattern : : The Driver client int main() { // Give me

The Factory Pattern : : The Driver client int main() { // Give me a circle Shape* shape 1 = Shape. Factory: : Create("circle"); // Give me a square Shape* shape 2 = Shape. Factory: : Create("square"); shape 1 ->draw(); // will call appropriate draw() shape 2 ->draw(); // as it is defined as virtual delete shape 1; delete shape 2; } C++ Design Patterns 15

The Factory Pattern : : When to Use a Factory Pattern You should consider

The Factory Pattern : : When to Use a Factory Pattern You should consider using a Factory pattern when: § A class can’t anticipate the kind of objects it must create. § A class uses hierarchy of classes to specify which objects it creates. § You want to localize the knowledge of which class gets created. How to recognize the Factory pattern? § The base class is abstract and the pattern must return a complete working class. § The base class contains default methods and is only subclassed for cases where the default methods are insufficient. § Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different. C++ Design Patterns 16

The Abstract Factory Pattern : : How does it Work? The Abstract Factory pattern

The Abstract Factory Pattern : : How does it Work? The Abstract Factory pattern is one level of abstraction higher than the factory pattern. This pattern returns one of several related classes, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories. One classic application of the abstract factory is the case where your system needs to support multiple “look-and-feel” user interfaces, such as Windows, Linux or Macintosh: § You tell the factory that you want your program to look like Windows and it returns a GUI factory which returns Windows-like objects. § When you request specific objects such as buttons, check boxes and windows, the GUI factory returns Windows instances of these visual interface components. C++ Design Patterns 17

The Abstract Factory Pattern : : A Widget Factory // Superclass of all objects

The Abstract Factory Pattern : : A Widget Factory // Superclass of all objects to be created by all Factories class Widget { public: //common interface to all created objects virtual void draw() = 0; }; //Supeclass of all Factories class Widget. Factory { public: //All specific kinds of objects that can be created virtual Widget* create_button() = 0; virtual Widget* create_menu() = 0; }; C++ Design Patterns 18

The Abstract Factory Pattern : : The Motif Widget Factory //Factory generating Motif Widgets

The Abstract Factory Pattern : : The Motif Widget Factory //Factory generating Motif Widgets class Motif. Widget. Factory : public Widget. Factory { public: Widget* create_button() { return new Motif. Button; } Widget* create_menu() { return new Motif. Menu; } }; //Different specific kinds of Motif objects class Motif. Button : public Widget { public: void draw() { cout << "Motif. Buttonn"; } }; class Motif. Menu : public Widget { public: void draw() { cout << "Motif. Menun"; } }; C++ Design Patterns 19

The Abstract Factory Pattern : : The Windows Widget Factory //Factory generating Windows Widgets

The Abstract Factory Pattern : : The Windows Widget Factory //Factory generating Windows Widgets class Windows. Widget. Factory : public Widget. Factory { public: Widget* create_button() { return new Windows. Button; } Widget* create_menu() { return new Windows. Menu; } }; //Different specific kinds of Windows objects class Windows. Button : public Widget { public: void draw() { cout << "Windows. Buttonn"; } }; class Windows. Menu : public Widget { public: void draw() { cout << "Windows. Menun"; } }; C++ Design Patterns 20

The Abstract Factory Pattern : : The Driver client //uncomment the following to generate

The Abstract Factory Pattern : : The Driver client //uncomment the following to generate Motif widgets #define MOTIF //uncomment the following to generate Windows widgets //#define WINDOWS int main(int argc, char *argv[]) { Widget. Factory *widget_factory; #ifdef MOTIF widget_factory = new Motif. Widget. Factory; #else // WINDOWS widget_factory = new Windows. Widget. Factory; #endif Widget* w[2] = {widget_factory->create_button(), widget_factory->create_menu()}; w[0]->draw(); //call appropriate draw() method w[1]->draw(); //of subclass of Widget object } C++ Design Patterns 21

The Abstract Factory Pattern : : Consequences of Abstract Factory § § § One

The Abstract Factory Pattern : : Consequences of Abstract Factory § § § One of the main purposes of the Abstract Factory is that it isolates the concrete classes that are generated. The actual class names of these classes are hidden in the factory and need not be known at the client level at all. Because of the isolation of classes, you can change or interchange these product class families freely. Since you generate only one kind of concrete class, this system keeps you for inadvertently using classes from different families of products. While all of the classes that the Abstract Factory generates have the same base class, there is nothing to prevent some derived classes from having additional methods that differ from the methods of other classes. C++ Design Patterns 22

The Builder Pattern : : How does it Work? - I The Builder Pattern

The Builder Pattern : : How does it Work? - I The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. § § Builder - specifies an abstract interface for creating parts of a Product object. Concrete. Builder - constructs and assembles parts of the product by implementing the Builder interface. Also, it defines and keeps track of the representation it creates and provides an interface for retrieving the product. Director - constructs an object using the Builder interface. Product - represents the complex object under construction. C++ Design Patterns 23

The Builder Pattern : : How does it Work? - II The client creates

The Builder Pattern : : How does it Work? - II The client creates the Director object and configures it with the desired Builder object. § Director notifies the builder whenever a part of the product should be built. § Builder handles requests from the director and adds parts to the product. § The client retrieves the product from the builder. § The following interaction diagram illustrates how Builder and Director cooperate with a client. C++ Design Patterns 24

The Builder Pattern : : Example: Pizza Builder [5] //Superclass of all kinds of

The Builder Pattern : : Example: Pizza Builder [5] //Superclass of all kinds of objects to be created //Generically called “Product” class Pizza { public: //Set all parts of the Product, and use the Product void set. Dough(const string& dough){ m_dough = dough; } void set. Sauce(const string& sauce){ m_sauce = sauce; } void set. Topping(const string& topping){ m_topping = topping; } void open() const{ cout << "Pizza with " << m_dough << " dough, " << m_sauce << " sauce and " << m_topping << " topping. Mmm. " << endl; } private: //Parts of the Product string m_dough; string m_sauce; string m_topping; }; C++ Design Patterns 25

The Builder Pattern : : Example: Pizza Builder [5] class Pizza. Builder { public:

The Builder Pattern : : Example: Pizza Builder [5] class Pizza. Builder { public: //get the built Pizza from the Builder Pizza* get. Pizza(){ return m_pizza; } //build a generic empty Pizza void create. New. Pizza. Product(){ m_pizza = new Pizza; } //create each part of the Product according to subtypes virtual void build. Dough() = 0; virtual void build. Sauce() = 0; virtual void build. Topping() = 0; protected: //Product built by Pizza Builder Pizza* m_pizza; }; C++ Design Patterns 26

The Builder Pattern : : Example: Pizza Builder [5] class Hawaiian. Pizza. Builder :

The Builder Pattern : : Example: Pizza Builder [5] class Hawaiian. Pizza. Builder : public Pizza. Builder //Concrete Builder 1 { public: virtual void build. Dough() //Build different parts of the pizza {m_pizza->set. Dough("cross"); } //The construction process could be virtual void build. Sauce() //more complex in a real-life example {m_pizza->set. Sauce("mild"); } //The construction of the pizza part virtual void build. Topping() //depends on the type of pizza. {m_pizza->set. Topping("ham+pineapple"); } }; class Spicy. Pizza. Builder : public Pizza. Builder //Concrete Builder 2 { public: virtual void build. Dough() //The construction process may vary {m_pizza->set. Dough("pan baked"); } //across different Concrete Builders. virtual void build. Sauce() {m_pizza->set. Sauce("hot"); } virtual void build. Topping() {m_pizza->set. Topping("pepperoni+salami"); } }; //There could be other Concrete Builders added for other kinds of pizza the Chef //designs later C++ Design Patterns 27

The Builder Pattern : : Example: Pizza Builder [5] class Cook //Director { public:

The Builder Pattern : : Example: Pizza Builder [5] class Cook //Director { public: void set. Pizza. Builder(Pizza. Builder* pb) //Use a concrete builder { //for building a specific m_pizza. Builder = pb; //kind of Pizza. } Pizza* get. Pizza() //get the constructed Pizza { return m_pizza. Builder->get. Pizza(); } void construct. Pizza() //Creational process to create { //a pizza using the builder. m_pizza. Builder->create. New. Pizza. Product(); m_pizza. Builder->build. Dough(); m_pizza. Builder->build. Sauce(); m_pizza. Builder->build. Topping(); } private: Pizza. Builder* m_pizza. Builder; }; C++ Design Patterns 28

The Builder Pattern : : Example: Pizza Builder [5] //Pizza Builder Client int main()

The Builder Pattern : : Example: Pizza Builder [5] //Pizza Builder Client int main() { Cook cook; //Create the Director Pizza. Builder* hawaiian. Pizza. Builder = //Create the Concrete Builder new Hawaiian. Pizza. Builder; Pizza. Builder* spicy. Pizza. Builder = new Spicy. Pizza. Builder; cook. set. Pizza. Builder(hawaiian. Pizza. Builder); //Tell the Director which Builder to use cook. construct. Pizza(); //Tell the Director to construct the Product Pizza* hawaiian = cook. get. Pizza(); //Client gets the Product hawaiian->open(); //Client uses the Product cook. set. Pizza. Builder(spicy. Pizza. Builder); //same for another kind of product cook. construct. Pizza(); Pizza* spicy = cook. get. Pizza(); spicy->open(); delete hawaiian. Pizza. Builder; delete spicy. Pizza. Builder; delete hawaiian; delete spicy; } C++ Design Patterns 29

The Builder Pattern : : Applicability of Builder Pattern Use the Builder pattern when:

The Builder Pattern : : Applicability of Builder Pattern Use the Builder pattern when: The algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled. § The construction process must allow different representations for the object that is constructed. § C++ Design Patterns 30

The Builder Pattern : : Consequences of Builder Pattern A Builder lets you vary

The Builder Pattern : : Consequences of Builder Pattern A Builder lets you vary the internal representation of the product it builds. It also hides the details of how the product is assembled. § Each specific builder is independent of the others and of the rest of the program. This improves modularity and makes the addition of other builders relatively simple. § Because each builder constructs the final product step-by-step, depending on the data, you have more control over each final product that a Builder constructs. § A Builder pattern is somewhat like an Abstract Factory pattern in that both return classes made up of a number of methods and objects. The main difference is that while the Abstract Factory returns a family of related classes, the Builder constructs a complex object step by step depending on the data presented to it. § C++ Design Patterns 31

The Singleton Pattern : : Definition & Applicability - I Sometimes it is appropriate

The Singleton Pattern : : Definition & Applicability - I Sometimes it is appropriate to have exactly one instance of a class: § window managers, § print spoolers, § filesystems. Typically, those types of objects known as singletons, are accessed by disparate objects throughout a software system, and therefore require a global point of access. The Singleton pattern addresses all the concerns above. With the Singleton design pattern you can: § Ensure that only one instance of a class is created. § Provide a global point of access to the object. § Allow multiple instances in the future without affecting a singleton class' clients. C++ Design Patterns 32

The Singleton Pattern : : Definition & Applicability - II § § § The

The Singleton Pattern : : Definition & Applicability - II § § § The Singleton pattern ensures a class has only one instance, and provides a global point of access to it. The class itself is responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. Singletons maintain a static reference to the sole singleton instance and return a reference to that instance from a static instance() method. C++ Design Patterns 33

The Singleton Pattern : : The Classic Singleton - I The Singleton class maintains

The Singleton Pattern : : The Classic Singleton - I The Singleton class maintains a static reference to the sinle singleton instance (s_instance) and returns that reference from the static Instance() method. class Singleton. Class { private: int m_value; //static instance, allows static instance() method to access the instance static Singleton. Class *s_instance; //private constructor prevents external instantiation Singleton. Class(){ m_value = 0; } public: int get_value(){ return m_value; } void increment_value(){ m_value++; } //static method to create/access the singleton instance static Singleton. Class *Instance() { if (!s_instance) s_instance = new Singleton. Class(); return s_instance; } }; C++ Design Patterns 34

The Singleton Pattern : : The Classic Singleton - II // Allocating and initializing

The Singleton Pattern : : The Classic Singleton - II // Allocating and initializing Singleton. Class's // static data member. The pointer is being // allocated - not the object itself. Singleton. Class *Singleton. Class: : s_instance = 0; //two free functions manipulating the instance void foo(void) { Singleton. Class: : instance()->increment_value(); cout << "foo: global_ptr is " << Singleton. Class: : instance()->get_value() << 'n'; } void bar(void) { Singleton. Class: : instance()->increment_value(); cout << "bar: global_ptr is " << Singleton. Class: : instance()->get_value() << 'n'; } int main(int argc, char *argv[]) { cout << "main: global_ptr is " << Singleton. Class: : instance()->get_value() << 'n'; foo(); bar(); } C++ Design Patterns 35

The Singleton Pattern : : The Classic Singleton - III § § § The

The Singleton Pattern : : The Classic Singleton - III § § § The Singleton class employs a technique known as lazy instantiation to create the singleton; as a result, the singleton instance is not created until the Instance() method is called for the first time. This technique ensures that singleton instances are created only when needed. The Singleton class implements a protected constructor so clients cannot instantiate Singleton instances. Protected constructors can be called by subclasses. Solutions: We can make the Singleton constructor private so that only Singleton’s methods call it; C++ Design Patterns 36

The Singleton Pattern : : Consequences of the Singleton Pattern § § It can

The Singleton Pattern : : Consequences of the Singleton Pattern § § It can be difficult to subclass a Singleton, since this can only work if the base Singleton class has not yet been instantiated. We can easily change a Singleton to allow a small number of instances where this is allowable and meaningful. We can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change. The Singleton pattern permits refinement of operations and representation. The Singleton class may be subclassed, and it is easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time. C++ Design Patterns 37

Design Patterns : : Structural Patterns § § Structural patterns describe how classes and

Design Patterns : : Structural Patterns § § Structural patterns describe how classes and objects can be combined to form larger structures. The difference between class patterns and object patterns is that class patterns describe how inheritance can be used to provide more useful program interfaces. Object patterns, on the other hand, describe how objects can be composed into larger structures using object composition, or the inclusion of objects within other objects. Some of the Structural patterns are: § Adapter § Composite § Proxy § Flyweight § Façade § Bridge § Decorator C++ Design Patterns 38

Design Patterns : : Structural Patterns - II § The Adapter pattern can be

Design Patterns : : Structural Patterns - II § The Adapter pattern can be used to make one class interface match another to make programming easier. § The Composite pattern is a composition of objects, each of which may be either simple or itself a composite object. § The Proxy pattern is frequently a simple object that takes the place of a more complex object that may be invoked later, for example when the program runs in a network environment. C++ Design Patterns 39

Design Patterns : : Structural Patterns III § The Flyweight pattern is a pattern

Design Patterns : : Structural Patterns III § The Flyweight pattern is a pattern for sharing objects, where each instance does not contain its own state, but stores it externally. This allows efficient sharing of objects to save space, when there are many instances, but only a few different types. § The Façade pattern is used to make a single class represent an entire subsystem. § The Bridge pattern separates an object’s interface from its implementation, so you can vary them separately. § The Decorator pattern can be used to add responsibilities to objects dynamically. C++ Design Patterns 40

The Adapter Pattern : : Definition & Applicability - I Motivation Like any adapter

The Adapter Pattern : : Definition & Applicability - I Motivation Like any adapter in the real world it is used to be an interface, a bridge between two objects that have the same functionality, but that are to be used in a different manner, i. e. have a different specification to their interfaces. In real world we have adapters for power supplies, adapters for camera memory cards, and so on. The same concept applies to software components. You may have some class expecting some type of object and you have an object offering the same features, but exposing a different interface. You don't want to change existing classes, so you want to create an adapter. Intent § § Convert the interface of a class into another interface that clients expect. Adapter lets classes work together, that could not otherwise because of incompatible interfaces. C++ Design Patterns 41

The Adapter Pattern : : Definition & Applicability - II The classes/objects participating in

The Adapter Pattern : : Definition & Applicability - II The classes/objects participating in adapter pattern: Target - defines the domain-specific interface that Client uses. Adapter - adapts the interface Adaptee to the Target interface. Adaptee - defines an existing interface that needs adapting. Client - collaborates with objects conforming to the Target interface. C++ Design Patterns 42

The Adapter Pattern : : Definition & Applicability - III The adapter pattern is

The Adapter Pattern : : Definition & Applicability - III The adapter pattern is used: § § When you have a class (Target) that invokes methods defined in an interface and you have a another class (Adapter) that doesn't implement the interface but implements the operations that should be invoked from the first class through the interface. You can change none of the existing code. The adapter will implement the interface and will be the bridge between the two classes. When you write a class (Target) for a generic use relying on some general interfaces and you have some implemented classes, not implementing the interface, that needs to be invoked by the Target class. C++ Design Patterns 43

The Adapter Pattern : : Example - I /** * The Square. Peg class.

The Adapter Pattern : : Example - I /** * The Square. Peg class. * This is the Target class. */ public class Square. Peg { public void insert(String str) { cout << "Square. Peg insert(): " << str; } } /** * The Round. Peg class. * This is the Adaptee class. */ public class Round. Peg { public void insert. Into. Hole(String msg) { cout << “Round. Peg insert. Into. Hole(): " << msg; } } If a client only understands the Square. Peg interface for inserting pegs using the insert() method, how can it insert round pegs, which are pegs, but that are inserted differently, using the insert. Into. Hole() method? C++ Design Patterns 44

The Adapter Pattern : : Example - II Solution: Design a Round. To. Square.

The Adapter Pattern : : Example - II Solution: Design a Round. To. Square. Peg adapter that enables to insert. Into. Hole() a Round. Peg object connected to the adapter to be inserted as a Square. Peg, using insert(). /** * The Round. To. Square. Peg. Adapter class. * This is the Adapter class. * It adapts a Round. Peg to a Square. Peg. * Its interface is that of a Square. Peg. */ public class Round. To. Square. Peg. Adapter: public Square. Peg { private Round. Peg round. Peg; public Round. To. Square. Peg. Adapter(Round. Peg peg) { //the round. Peg is plugged into the adapter this. round. Peg = peg; } public void insert(String str) { //the round. Peg can now be inserted in the same manner as a square. Peg! round. Peg. insert. Into. Hole(str); } } C++ Design Patterns 45

The Adapter Pattern : : Example - III // Test program for Pegs. main(int

The Adapter Pattern : : Example - III // Test program for Pegs. main(int argc, char *argv[]) { // Create some pegs. Round. Peg round. Peg = new Round. Peg(); Square. Peg square. Peg = new Square. Peg(); // Do an insert using the square peg. square. Peg. insert("Inserting square peg. . . "); // Now we'd like to do an insert using the round peg. // But this client only understands the insert() // method of pegs, not a insert. Into. Hole() method. // The solution: create an adapter that adapts // a square peg to a round peg! Peg. Adapter adapter = new Peg. Adapter(round. Peg); adapter. insert("Inserting round peg. . . "); Square. Peg *sq_pegs[2] = {new Square. Peg(), new Round. To. Square. Peg. Adapter(*round. Peg)}; sq_pegs[0]->insert. Into. Square. Hole("Inserting square peg [0]. . . n"); sq_pegs[1]->insert. Into. Square. Hole("Inserting square peg [1]. . . n"); } C++ Design Patterns 46

The Adapter Pattern : : Definition & Applicability - VII More: two-way adapter! Solution:

The Adapter Pattern : : Definition & Applicability - VII More: two-way adapter! Solution: multiple inheritance! public class Square. Peg { public virtual void insert. Square. Peg(String str) { cout << "Square. Peg insert. Square. Peg(): " << str; } } public class Round. Peg { public virtual void insert. Round. Peg(String msg) { cout << “Round. Peg insert. Round. Peg(): " << msg; } } class Two. Way. Peg. Adapter: public Square. Peg, Round. Peg { private: Round. Peg round. Peg; Square. Peg square. Peg; public: Two. Way. Peg. Adapter(Round. Peg peg) { //a round. Peg is plugged into the adapter this. round. Peg = peg; } Two. Way. Peg. Adapter(Square. Peg peg) { //a square. Peg is plugged into the adapter this. square. Peg = peg; } void virtual insert. Round. Peg(String str) { square. Peg. insert. Square. Peg(str ); } void virtual insert. Square. Peg(String str) { round. Peg. insert. Round. Peg(str ); } } C++ Design Patterns 47

The Adapter Pattern : : Definition & Applicability - VIII int main(int argc, char

The Adapter Pattern : : Definition & Applicability - VIII int main(int argc, char *argv[]) { Round. Peg *round. Peg = new Round. Peg(); Square. Peg *square. Peg = new Square. Peg(); Square. Peg *square. Peg. Array[2] = {square. Peg, new Two. Way. Peg. Adapter(round. Peg )}; square. Peg. Array[0]->insert. Square. Peg("Inserting Square Peg in square. Peg. Array[0]. . . n"); square. Peg. Array[1]->insert. Square. Peg("Inserting Square Peg in square. Peg. Array[1]. . . n"); Round. Peg *round. Peg. Array[2] = { round. Peg, new Two. Way. Peg. Adapter(square. Peg)}; round. Peg. Array[0]->insert. Round. Peg("Inserting Round Peg in round. Peg. Array[0]. . . n"); round. Peg. Array[1]->insert. Round. Peg("Inserting Round Peg in round. Peg. Array[1]. . . n"); }; C++ Design Patterns 48

Design Patterns : : Behavioral Patterns § § Observer pattern Define a one-to-many dependency

Design Patterns : : Behavioral Patterns § § Observer pattern Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Null Object Provide an object as a surrogate for the lack of an object of a given type. The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators. Memento Capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed. Observer Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. C++ Design Patterns 49

The Observer Pattern : : Definition & Applicability - I Motivation The cases when

The Observer Pattern : : Definition & Applicability - I Motivation The cases when certain objects need to be informed about the changes occured in other objects are frequent. To have a good design means to decouple as much as possible and to reduce the dependencies. The Observer Design Pattern can be used whenever a subject has to be observed by one or more observers. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. This pattern is a cornerstone of the Model-View-Controller architectural design, where the Model implements the mechanics of the program, and the Views are implemented as Observers that are as much uncoupled as possible to the Model components. C++ Design Patterns 50

The Observer Pattern : : Definition & Applicability - II C++ Design Patterns 51

The Observer Pattern : : Definition & Applicability - II C++ Design Patterns 51

The Observer Pattern : : Definition & Applicability - III The participants classes in

The Observer Pattern : : Definition & Applicability - III The participants classes in the Observer pattern are: Observable - interface or abstract class defining the operations for attaching and de-attaching observers to the client. In the GOF book this class/interface is known as Subject. Concrete. Observable - concrete Observable class. It maintain the state of the observed object and when a change in its state occurs it notifies the attached Observers. Observer - interface or abstract class defining the operations to be used to notify the Observer object. Concrete. Observer. A, Concrete. Observer. B - concrete Observer implementations. C++ Design Patterns 52

The Observer Pattern : : Definition & Applicability - IV Behavior § § §

The Observer Pattern : : Definition & Applicability - IV Behavior § § § The client class instantiates the Concrete. Observable object. Then it instantiate and attaches the concrete observers to it using the methods defined in the Observable interface. Each time the state of the subject it's changing it notifies all the attached Observers using the methods defined in the Observer interface. When a new Observer is added to the application, all we need to do is to instantiate it in the client class and to add attach it to the Observable object. The classes already created will remain unchanged. C++ Design Patterns 53

The Observer Pattern : : Example - I // Subject class, also known as

The Observer Pattern : : Example - I // Subject class, also known as “Observable” // class Observable { public: virtual ~Observable() {}; virtual void attach(Observer *); virtual void detach(Observer *); virtual void notify(); protected: Observable() {}; private: list<Observer*> _observers; }; C++ Design Patterns 54

The Observer Pattern : : Example - II // A Sub-class of Observable: a

The Observer Pattern : : Example - II // A Sub-class of Observable: a Clock Timer // class Clock. Timer : public Observable { public: Clock. Timer(); int Get. Hour(){return hour}; int Get. Minute(){return minute}; int Get. Second(){return second}; void tick(){ // update internal time-keeping state //. . . // The Observable object notifies all its registered observers notify(); }; private: int hour; int minute; int second; }; In green are the changes to be applied to the class to be made an observable class. C++ Design Patterns 55

The Observer Pattern : : Example - III //Observer Class // class Observer {

The Observer Pattern : : Example - III //Observer Class // class Observer { public: virtual ~Observer(); virtual void Update(Subject* the. Change. Subject) = 0; protected: Observer(); }; The Observer class is a virtual class C++ Design Patterns 56

The Observer Pattern : : Example - IV // A specific Observer to observe

The Observer Pattern : : Example - IV // A specific Observer to observe Clock. Timers: Digital. Clock // class Digital. Clock: public Observer { public: Digital. Clock(Clock. Timer* s){ //Upon instantiation, attaches itself to a Clock. Timer _observable = s; _observable->attach(this); } ~Digital. Clock(){ //Upon destruction, detaches itself from its Clock. Timer _observable->detach(this); }; void Update(Subject* the. Changed. Subject){ //if the notification concerns my own subject, redraw my clock’s reading if(the. Changed. Subject == _observable) draw(); }; void draw(){ int hour = _subject->Get. Hour(); int minute = _subject->Get. Minute(); int second = _subject->Get. Second(); cout << hour << ': ' << minute << ': ' << second << endl; }; private: Clock. Timer *_observable; }; C++ Design Patterns 57

The Observer Pattern : : Example - V int main(void) { //Create a Clock.

The Observer Pattern : : Example - V int main(void) { //Create a Clock. Timer to be observed Clock. Timer *timer = new Clock. Timer; //Create a Digital. Clock that is connected to the Clock. Timer Digital. Clock *digital. Clock = new Digital. Clock(timer); //Advancing the Clock. Timer updates the Digital. Clock //as Tick() calls Update() after it changed its state while(true){ timer->tick(); } } C++ Design Patterns 58

Resources [1] Christopher Alexander, Sara Ishikawa, Murray Silverstein, Max Jacobson, Ingrid Fiksdahl-King, and Shlomo

Resources [1] Christopher Alexander, Sara Ishikawa, Murray Silverstein, Max Jacobson, Ingrid Fiksdahl-King, and Shlomo Angel. A Pattern Language. Oxford University Press, New York, 1977. [2] Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns – Elements of Reusable Object-Oriented Software, Adisson. Wesley, 1995. [3] James W. Cooper, The Design Patterns – Java Companion Elements of Reusable Object-Oriented Software, Adisson-Wesley, 1998. [4] James O. Coplien, Advanced C++ Programming Styles and Idioms, Addison -Wesley, Reading, MA. , 1992. [5] www. oodesign. com. Object-oriented design patterns. 2009. [6] C++ Programming Wiki. Book http: //en. wikibooks. org/wiki/C++_Programming/Code/Design_Patterns/Cre ational_Patterns C++ Design Patterns 59