
OO Pattern Examples Douglas C. Schmidt Case Studies Using Patterns The following slides describ e several case studies using C++ & patterns to build highly extensible software Object-Oriented Design Case Studies with The examples include Patterns & C++ 1. Expression Tree Douglas C. Schmidt { e.g., Adapter, Factory, Bridge 2. System Sort Professor Department of EECS { e.g., Facade, Adapter, Iterator, Singleton, Factory Metho d, Strategy, [email protected] Vanderbilt University Bridge www.dre.vanderbilt.edu/schmidt/ 615 343-8197 3. Sort Veri er { e.g., Strategy, Factory Metho d, Facade, Iterator, Singleton Vanderbilt University 1 OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt Case Study: Expression Tree Evaluator Expression Tree Diagram BINARY The following inheritance & dynamic binding example constructs * NODES expression trees { Expression trees consist of no des containing op erators & op erands _ Op erators have di erent precedence levels, di erent asso ciativities, + & di erent arities, e.g., Multiplication takes precedence over addition UNARY The multiplication op erator has two arguments, whereas unary NODE 5 3 44 minus op erator has only one 5 3 Op erands are integers, doubles, variables, etc. We'll just handle integers in this example . INTEGER NODES Vanderbilt University 2 Vanderbilt University 3 V OO V OO anderbilt anderbilt MEMORY LAYOUT Here's Exp { { P P binary_ unary_ attern attern num_ use_ tag_ op_ The T ression rees Return P Generate T e.g. Universit Universit Examples Examples raverse erfo the evaluation , ma Memo in-o rm y y memo trees y the semantic rder, be & ode co \value" Node p Tree \evaluated" ry ry rint RELATIONSHIPS Exp step ost-o p la La y CLASS 1 the out 1|2 ression analysis ma y of rder, out exp y of the erfo p Node a Tree ression via p of struct exp re-o T di erent rm Algo ression ree rder, va tree Tree rious Behavio rithmic level-o traversals tree Node perations, op rder object r V ersion e.g. Douglas Douglas , C. C. Schmidt Schmidt 6 4 o OO Pattern Examples Do OO Pattern Examples D Algorithmic Version Print Tree Function A typical algorithmic implementation use a switch A typical algorithmic metho d for implementing statement & a recursive function to build & evaluate expression trees involves using a struct/union to a tree, e.g., represent data structure, e.g., void print_tree Tree_Node *root { typedef struct Tree_Node Tree_Node; switch root->tag_ { struct Tree_Node { case NUM: printf "d", root->num_; enum { NUM, UNARY, BINARY } tag_; break; short use_; /* reference count */ case UNARY: union { printf "s", root->op_[0]; char op_[2]; print_tree root->unary_; int num_; printf ""; break; } o; case BINARY: define num_ o.num_ printf ""; define op_ o.op_ print_tree root->binary_.l_ ; union { printf "s", root->op_[0]; Tree_Node *unary_; root->binary_.r_ ; print_tree struct { Tree_Node *l_, *r_; } binary_; printf ""; break; } c; default: define unary_ c.unary_ printf "error, unknown type\n"; define binary_ c.binary_ } }; } Vanderbilt University Vanderbilt University OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt Limitations with Algorithmic Approach More Limitations with Algorithmic Approach Problems or limitations with the typical algorithmic approach include The program organization makes it dicult to extend, e.g., { Little or no use of encapsulation { Any small changes will ripple through the entire design & implementation Incomplete mo deling of the application domain, which results in e.g., see the \ternary" extension b elow { Easy to make mistakes switching on typ e tags . 1. Tight coupling between no des & edges in union representation 2. Complexity b eing in algorithms rather than the data structures Solution wastes space by making worst-case assumptions wrt structs & { e.g., switch statements are used to select between various typ es of unions no des in the expression trees { Compare with binary search! { This is not essential, but typically o ccurs 3. Data structures are \passive" & functions do most pro cessing work { Note that this problem b ecomes worse the bigger the size of the explicitly largest item b ecomes! Vanderbilt University 8 Vanderbilt University 9 OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt OO Alternative Expression Tree Diagram BINARY Contrast previous algorithmic approach with an object-oriented * NODES decomp osition for the same problem: { Start with OO mo deling of the \expression tree" application domain, _ e.g., go back to original picture + { Discover several classes involved: class No de: base class that describ es expression tree vertices: UNARY class Int No de: used for implicitly converting int to Tree no de NODE class Unary No de: handles unary op erators, e.g., -10, +10, !a 55 33 44 class Binary No de: handles binary op erators, e.g., a + b, 10 - 30 class Tree: \glue" co de that describ es expression-tree edges, i.e., relations between No des INTEGER { Note, these classes mo del entities in the application domain NODES i.e., no des & edges vertices & arcs Vanderbilt University 10 Vanderbilt University 11 OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt Relationships Between Tree & No de Classes Design Patterns in the Expression Tree Program Binary Unary Int Node Node Node Factory 1 1 { Centralize the assembly of resources necessary to create an object e.g., decouple Node sub class initialization from use Bridge { Decouple an abstraction from its implementation so that the two can Ternary Node vary indep endently Node e.g., printing contents of a subtree and managing memory 2 1 Adapter 1 has-a 3 Tree { Convert the interface of a class into another interface clients exp ect e.g., make Tree conform C++ iostreams Vanderbilt University 12 Vanderbilt University 13 OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt C++ No de Interface C++ Tree Interface include "Node.h" class Tree; // Forward declaration // Bridge class that describes the Tree edges and // acts as a Factory. // Describes the Tree vertices class Tree { class Node { public: friend class Tree; // Factory operations protected: // Only visible to derived classes Tree int; Node : use_ 1 {} Tree const string &, Tree &; Tree const string &, Tree &, Tree &; /* pure */ virtual void print std::ostream & const = 0; Tree const Tree &t; void operator= const Tree &t; // Important to make destructor virtual! ~Tree ; virtual ~Node ; void print std::ostream & const; private: private: int use_; // Reference counter. Node *node_; // pointer to a rooted subtree }; Vanderbilt University 14 Vanderbilt University 15 OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt C++ Int No de Interface C++ Unary No de Interface include "Node.h" include "Node.h" class Int_Node : public Node { class Unary_Node : public Node { public: public: Int_Node int k; Unary_Node const string &op, const Tree &t; virtual void print std::ostream &stream const; virtual void print std::ostream &stream const; private: private: int num_; // operand value. string operation_; }; Tree operand_; }; Vanderbilt University 16 Vanderbilt University 17 o D OO Pattern Examples Douglas C. Schmidt C++ Binary No de Interface Node include "Node.h" of ) ) ) op tag ) ) ) op tag ersion PART PART PART PART PART PART left V right left_ PART middle right_ class Binary_Node : public Node { Node PART Node operator middle_ (Tree (Tree operator_ (Tree (Tree (Tree (Tree public: ) ) ) ) Binary_Node const string &op, sub classes Node PART C++ PART PART Ternary PART left right left_ PART const Tree &t1, Node PART right_ Node Node operator Binary r (Tree operator_ (Tree (Tree (Tree const Tree &t2; fo ) ) virtual void print std::ostream &s const; di erent PART PART private: out PART r Node PART operand Node operator operand_ (Tree operator_ (Tree y fo const string operation_; Unary Node La Tree left_; outs y ptr use y ry vptr num vptr use_ Tree right_; PART Tree num_ Node node_ PART Node Node la Int_Node }; ry Examples Universit Memo attern P Memo anderbilt Vanderbilt University 18 OO V OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt C++ Int No de Implementations C++ Unary No de Implementations include "Int_Node.h" include "Unary_Node.h" Int_Node::Int_Nod e int k: num_ k { } Unary_Node::Unary _N ode const string &op, const Tree &t1 : operation_ op, operand_ t1 { } void Int_Node::print std::ostream &stream const { stream << this->num_; void Unary_Node::print std::ostream &stream const { } stream << "" << this->operation_ << << this->operand_ // recursive call! << ""; } Vanderbilt University 20 Vanderbilt University 21 OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt C++ Binary No de Implementation Initializing the No de Sub classes include "Binary_Node.h" Problem Binary_Node::Bina ry _No de const string &op, { How to ensure the No de sub classes are initialized prop erly const Tree &t1, Forces const Tree &t2: operation_ op, left_ t1, right_ t2 {} { There are di erent typ es of No de sub classes e.g., take di erent numb er & typ e of arguments void Binary_Node::prin t std::ostream &stream const { { We want to centralize initialization in one place b ecause it is likely to stream << "" << this->left_ // recursive call change . << " " << this->operation_ Solution << " " << this->right_ // recursive call << ""; { Use a Factory pattern to initialize the No de sub classes } Vanderbilt University 22 Vanderbilt University 23 OO Pattern Examples Douglas C. Schmidt OO Pattern Examples Douglas C. Schmidt The Factory Pattern Structure of the Factory Pattern Factory Intent { Centralize the assembly of resources necessary to create an object make_product() Decouple object creation from object use by lo calizing creation knowledge creates This pattern resolves the following forces: Product product = ... { Decouple initialization of the Node sub classes from their subsequent return product use { Makes it easier to change or add new No de sub classes later on e.g., Ternary no des .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages37 Page
-
File Size-