Design Patterns (Composite, Iterator)

Size: px
Start display at page:

Download "Design Patterns (Composite, Iterator)"

Transcription

1 CS 246: Software Abstraction and Specification Lecture 17 Design Patterns (Composite, Iterator) Reading: Head First Design Patterns U Waterloo CS246se (Spring 2011) p.1/30

2 Today's Agenda Design patterns: codified solutions that put design principles into practice, to improve the modularity of our code. OO Basics Favour Composition over Inheritance Model-View-Controller (MVC) Separation of Concerns Single Responsibility Principle Composite Loose Coupling Principle of Least Knowledge (Law Iterator of Demeter) Information Hiding Avoid duplicate code Encapsulate what is likely to change OO Principles Strategy Program to an Interface, not an Factory Implementation Method Liskov Substitutability PrincipleObserver Design Patterns Template Method U Waterloo CS246se (Spring 2011) p.2/30

3 Review: Object Composition A compound object represents a composition of heterogeneous, possibly recursive, component objects Law of Demeter: client code interacts with compound object Document 1 * 0..1 Section * Transcript 1 * Term Record 1 1 * Page 1..* Course Enrollment * Player * 0..1 Team * subteam 1 Course number description credits U Waterloo CS246se (Spring 2011) p.3/30

4 Composite Pattern Idea The Composite Pattern takes a different approach: gives the client access to all members in a compound object via a uniform interface. Client Code TeamMember * Player Team Client Code 2 Expr value() print() BinaryExpr Variable name value Plus Minus Multiply Divide U Waterloo CS246se (Spring 2011) p.4/30

5 Composite Pattern Problem: composite object consists of several heterogenous parts Client code is complicated by knowledge of object structure Client must change if data structure changes Solution: create a uniform interface for the object's components Interface advertises all operations that components offer Client deals only with the new uniform interface Client Code Component Operation add(component) Remove(Component) GetChild(int) * part Leaf Operation Composite Operation add(component) Remove(Component) GetChild(int) U Waterloo CS246se (Spring 2011) p.5/30

6 Example: Expressions 2 BinaryExpr left() : Expr right() : Expr print() Expression value() print() Variable name value name() value() print() Expressions: a+b a*b+c-d a Plus Minus Multiply Divide U Waterloo CS246se (Spring 2011) p.6/30

7 Implementing the Component Interface Because the interface of the Component Class is to be uniform, it comprises the union of all component classes. class Expression { public: // leaf-only operations virtual string name() const; // composite-only operations virtual const Expression* left() const; virtual const Expression* right() const; //shared operations virtual int value() const; virtual void print() const; private: Expression(); ; U Waterloo CS246se (Spring 2011) p.7/30

8 Uniformity vs. Safety Whether to include component-specific operations in the component interface involves a trade-off between uniformity - preserving the illusion that component objects can be treated the same way - promoted by the Composite Pattern safety - avoiding cases where the client attempts to do something meaningless, like adding components to Leaf objects - promoted by Liskov Substitutability Principle U Waterloo CS246se (Spring 2011) p.8/30

9 Implementing the Component Interface What default implementation makes sense? class Expression { public: // leaf-only operations virtual string name() const {return ""; // composite-only operations virtual const Expression* left() const {return 0; virtual const Expression* right() const {return 0; //shared operations virtual int value() const {return 0; virtual void print() const { private: Expression(); ; U Waterloo CS246se (Spring 2011) p.9/30

10 Implementing the Leaf Class The leaf classes override the behaviour of leaf-object operations. class Variable : public Expression { public: Variable (std::string, int); // redefine leaf-only operations virtual std::string name() const {return name_; // inherit default behaviour of composite-only operations // redefine shared operations virtual int value() const {return value_; virtual void print() const {std::cout << name(); private: std::string name_; int value_; ; U Waterloo CS246se (Spring 2011) p.10/30

11 Implementing the Composite The composite classes override composite-object operations. class BinaryExpr : public Expression { public: ~BinaryExpr(); // inherit default behaviour of leaf-only operations // redefine composite operations virtual Expression* left() const {return leftchild_; virtual Expression* right() const {return rightchild_; private: Expression* leftchild_; Expression* rightchild_; protected: BinaryExpr(Expression* a, Expression *b); ; U Waterloo CS246se (Spring 2011) p.11/30

12 Implementing the Composite The (concrete) composite classes specialize the composite methods that are operator specific. class Plus : public BinaryExpr { public: Plus(Expression*, Expression*); // redefine shared operations int value() const; void print() const; ; int Plus::value() const { return left()->value() + right()->value(); void Plus::print() const { left()->print(); std::cout << " + "; right()->print(); U Waterloo CS246se (Spring 2011) p.12/30

13 Another Example 0..1 Developer * Project Team * subteam TeamMember name : string name() : string salary() print() add(teammember) remove(teammember) getmember(int) * member Developer salary: float salary() print() ProjectTeam print() add(teammember) remove(teammember) getmember(int) U Waterloo CS246se (Spring 2011) p.13/30

14 Component Interface class TeamMember { public: // leaf-only operations virtual int salary() const; // component-only operations virtual void add(teammember*); virtual void remove(teammember*); virtual TeamMember* getmember(int) const; // shared operations std::string name() const; virtual void print() const; protected: TeamMember( std::string name ); private: std::string name_; ; U Waterloo CS246se (Spring 2011) p.14/30

15 Composite Pattern 2 Expression value() print() Client BinaryExpr left() : Expr right() : Expr print() Variable name value name() value() print() Plus Minus Multiply Divide Consequences: + Client deals only with the new uniform interface + New leafs and composite types are easy to add New operations are harder to add (Visitor Pattern) How can client code iterate through a composite object without knowing the composite's structure? U Waterloo CS246se (Spring 2011) p.15/30

16 Iterator Pattern Goals: (1) To encapsulate the strategy for iterating through a composite (so that it can be changed, at run-time). (2) Allow the client to iterate through a composite without exposing the composite's representation. Collection createiterator() size() getelem(int) Client Iterator first() hasnext() next() ConcreteCollection createiterator() size() getelem(int) <<create>> ConcreteIterator first() hasnext() next() U Waterloo CS246se (Spring 2011) p.16/30

17 Collection createiterator() size() getelem(int) Department createiterator() size() getelem(int) Operation() * Prof Simple Iteration <<create>> Operation to retrieve specific element Client Iterator first() hasnext() next() DepartmentIterator first() hasnext() next() Operations to retrieve all elements, systematically // client code Department* dept; DepartmentIterator* iter = dept->createiterator(); iter->first(); while ( iter->hasnext() ) { Prof* p = iter->next(); // process p U Waterloo CS246se (Spring 2011) p.17/30

18 Department Composite class is augmented with operations to support the Iterator Pattern. class Department { public: virtual int size(); virtual Prof* getelem(int); virtual Iterator* createiterator(); // other operations private: std::vector<prof*> dept_; ; Iterator* Department::createIterator() { return new DepartmentIterator(this); U Waterloo CS246se (Spring 2011) p.18/30

19 Department Iterator class DepartmentIterator { // retrieves elements in vector order private: Department* dept_; int cursor_; public: DepartmentIterator(Department* dept) : dept_(dept), cursor_(0) { virtual void first() { cursor_ = 0; virtual bool hasnext(); virtual Prof* next(); ; bool DepartmentIterator::hasNext() { return (cursor_ < dept_->size()); Prof* DepartmentIterator::next() { Prof* result = (dept_->getelem(cursor_)); cursor_ += 1; return result; U Waterloo CS246se (Spring 2011) p.19/30

20 Iteration over a Composite Object The more interesting case is when the aggregate is a composite object, in which case we need to construct an Iterator that understands and navigates the composite. Company Division A Division B Tom Tiger Team Special Ops Team Alice Ajit Sandy Ali U Waterloo CS246se (Spring 2011) p.20/30

21 Composite Iteration collection TeamMember name() salary() add (TeamMember) remove (TeamMember) createiterator() size() getchild(int) * Client Iterator first() hasnext() next() iterator Developer salary() createiterator() Team add (TeamMember) remove (TeamMember) createiterator() size() getchild(int) <<create>> <<create>> TeamIterator TeamIterator(Team) first() hasnext() next() DevIterator DevIterator(Developer) first() hasnext() next() U Waterloo CS246se (Spring 2011) p.21/30

22 Client Code Iterate through all members in the composite. TeamMember* employees;... Iterator* iter = employees->createiterator(); iter->first(); while ( iter->hasnext() ) { TeamMember* m = iter->next(); // process member m Different iterator can iterate through all leaf objects in the composite. TeamMember* employees;... Iterator* iter = employees->createdevonlyiterator(); iter->first(); while ( iter->hasnext() ) { TeamMember* m = iter->next(); RequestPayCheck(m->name(), m->salary()); U Waterloo CS246se (Spring 2011) p.22/30

23 Create Iterator Each concrete subclass in the composite knows how to create its own corresponding Iterator. Iterator* Developer::createIterator() { return new DevIterator(this); Iterator* Team::createIterator() { return new TeamIterator(this); U Waterloo CS246se (Spring 2011) p.23/30

24 Developer Iterator class DevIterator : public Iterator { private: Developer* dev_; Developer* cursor_; public: DevIterator(Developer* dev) : dev_(dev), cursor_(dev) { virtual void first() { cursor_ = dev_; virtual bool hasnext() { return (cursor_!= NULL); virtual TeamMember* next(); ; TeamMember* DevIterator::next() { if ( hasnext() ) { cursor = NULL; return dev_; return NULL; U Waterloo CS246se (Spring 2011) p.24/30

25 Team Behaviour The Composite objects contribute to iteration with operations to retrieve specific elements. class Team : public TeamMember { private: std::vector<teammember*> members_;... public:... virtual int size() { return members_->size(); virtual TeamMember* getchild(int i) { return members_->at(i); ; U Waterloo CS246se (Spring 2011) p.25/30

26 Team Iterator The composite iterator needs to iterate over the composite object, including several subobjects. We achieve this by keeping an iterator (cursor) for each collection in composite putting iterators on stack as the collections are encountered class TeamIterator : public Iterator { private: TeamMember* members_; // pointer to composite struct Iter; // < collection, cursor> std::stack<iter*> istack; // stack of iterators public: TeamIterator(TeamMember* m) : members_(m) { first(); virtual void first(); // initialize Iterator stack virtual bool hasnext(); virtual TeamMember* next(); ; U Waterloo CS246se (Spring 2011) p.26/30

27 TeamIterator::first() Initialize the iterator stack with a cursor for the whole composite. struct TeamIterator::Iter { TeamMember *collect_; int cursor_; // ranges from -1.. collect_->size() Iter(TeamMember *m) : collect_(m), cursor_(-1) { ; void TeamIterator::first() { while (!istack.empty() ) { istack.pop(); istack.push(new Iter(members_)); U Waterloo CS246se (Spring 2011) p.27/30

28 TeamIterator::next() TeamMember* TeamIterator::next() { // preorder iteration if ( hasnext() ) { // have cursors reached their limit? Iter* top = istack.top(); istack.pop(); // cursor points to member (could be Developer or Team) if (top->cursor_==-1) { top->cursor_ += 1; istack.push(top); // advance cursor to first element return top->collect_;// return member // cursor points to elem within collection member TeamMember *elem = top->collect_->getchild(top->cursor_); top->cursor_ += 1; istack.push(top); // advance cursor to next element istack.push(new Iter(elem)); // push new element on stack return next(); // recurse else return 0; U Waterloo CS246se (Spring 2011) p.28/30

29 TeamIterator::hasNext() Check if stack contains an iterator that has not retrieved all elements of its respective collection bool TeamIterator::hasNext() { while (!istack.empty() ) { Iter *top = istack.top(); if (top->collect_->size() > top->cursor_) { return true; istack.pop(); delete top; return false; U Waterloo CS246se (Spring 2011) p.29/30

30 Summary The goal of design patterns is to encapsulate change Composite Pattern: encapsulates the structure of a heterogeneous, possibly recursive data structure Iterator Pattern: encapsulates the iteration of a heterogeneous, possibly recursive data structure U Waterloo CS246se (Spring 2011) p.30/30

Design Patterns (Composite)

Design Patterns (Composite) CS 247: Software Engineering Principles Design Patterns (Composite) Reading: Freeman, Robson, Bates, Sierra, Head First Design Patterns, O'Reilly Media, Inc. 2004 Ch 9: Composite and Iterator Patterns

More information

Design Patterns (Facade, Composite)

Design Patterns (Facade, Composite) CS 247: Software Engineering Principles Design Patterns (Facade, Composite) Reading: Freeman, Robson, Bates, Sierra, Head First Design Patterns, O'Reilly Media, Inc. 2004 Ch 7 Adapter and Facade patterns

More information

Design Patterns (Iterator)

Design Patterns (Iterator) CS 247: Software Engineering Principles Design Patterns (Iterator) Reading: Freeman, Robson, Bates, Sierra, Head First Design Patterns, O'Reilly Media, Inc. 2004 Ch 9: Composite and Iterator Patterns Electronic

More information

Today's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles

Today's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles CS 247: Software Engineering Principles Reading: none Object-Oriented Design Principles Today's Agenda Object-Oriented Design Principles - characteristics, properties, and advice for making decisions that

More information

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005 1 Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/6448 - Spring Semester, 2005 2 Last Lecture Design Patterns Background and Core Concepts Examples

More information

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship CSCI 253 Object Oriented Design: Iterator Pattern George Blankenship George Blankenship 1 Creational Patterns Singleton Abstract factory Factory Method Prototype Builder Overview Structural Patterns Composite

More information

Software Eningeering. Lecture 9 Design Patterns 2

Software Eningeering. Lecture 9 Design Patterns 2 Software Eningeering Lecture 9 Design Patterns 2 Patterns covered Creational Abstract Factory, Builder, Factory Method, Prototype, Singleton Structural Adapter, Bridge, Composite, Decorator, Facade, Flyweight,

More information

Design Patterns Lecture 2

Design Patterns Lecture 2 Design Patterns Lecture 2 Josef Hallberg josef.hallberg@ltu.se 1 Patterns covered Creational Abstract Factory, Builder, Factory Method, Prototype, Singleton Structural Adapter, Bridge, Composite, Decorator,

More information

CS 246: Software Abstraction and Specification. Lecture 8. Object Composition

CS 246: Software Abstraction and Specification. Lecture 8. Object Composition CS 246: Software Abstraction and Specification Lecture 8 Object Composition Agenda: Object Composition Implementing Composite Objects "Law" of Demeter Reading: Eckel, Vol. 1 Ch. 14 Inheritance and Composition

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

SDC Design patterns GoF

SDC Design patterns GoF SDC Design patterns GoF Design Patterns The design pattern concept can be viewed as an abstraction of imitating useful parts of other software products. The design pattern is a description of communicating

More information

» Access elements of a container sequentially without exposing the underlying representation

» Access elements of a container sequentially without exposing the underlying representation Iterator Pattern Behavioural Intent» Access elements of a container sequentially without exposing the underlying representation Iterator-1 Motivation Be able to process all the elements in a container

More information

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern Think of drawing/diagramming editors ECE450 Software Engineering II Drawing/diagramming editors let users build complex diagrams out of simple components The user can group components to form larger components......which

More information

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

Design Patterns. Gang of Four* Design Patterns. Gang of Four Design Patterns. Design Pattern. CS 247: Software Engineering Principles CS 247: Software Engineering Principles Design Patterns Reading: Freeman, Robson, Bates, Sierra, Head First Design Patterns, O'Reilly Media, Inc. 2004 Ch Strategy Pattern Ch 7 Adapter and Facade patterns

More information

CS 247: Software Engineering Principles. Design Patterns

CS 247: Software Engineering Principles. Design Patterns CS 247: Software Engineering Principles Design Patterns Reading: Freeman, Robson, Bates, Sierra, Head First Design Patterns, O'Reilly Media, Inc. 2004 Ch 1 Strategy Pattern Ch 7 Adapter and Facade patterns

More information

CS 520 Theory and Practice of Software Engineering Fall 2017

CS 520 Theory and Practice of Software Engineering Fall 2017 CS 520 Theory and Practice of Software Engineering Fall 2017 OO design principles September 14, 2017 Today Code review and (re)design of an MVC application OO design principles Information hiding (and

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

20. Inheritance and Polymorphism

20. Inheritance and Polymorphism (Expression) Trees 20. Inheritance and Polymorphism Expression Trees, Inheritance, Code-Reuse, Virtual Functions, Polymorphism, Concepts of Object Oriented Programming bend fork 3 -(3-(4-5))*(3+4*5)/6

More information

Object-Oriented Oriented Programming

Object-Oriented Oriented Programming Object-Oriented Oriented Programming Composite Pattern CSIE Department, NTUT Woei-Kae Chen Catalog of Design patterns Creational patterns Abstract Factory, Builder, Factory Method, Prototype, Singleton

More information

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING Question No: 1 ( Marks: 1 ) - Please choose one Classes like TwoDimensionalShape and ThreeDimensionalShape would normally be concrete,

More information

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

COSC 3351 Software Design. Design Patterns Structural Patterns (I) COSC 3351 Software Design Design Patterns Structural Patterns (I) Spring 2008 Purpose Creational Structural Behavioral Scope Class Factory Method Adaptor(class) Interpreter Template Method Object Abstract

More information

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995 CS560 Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995 Classification of GoF Design Pattern Creational Structural Behavioural Factory Method Adapter Interpreter Abstract Factory Bridge

More information

Design Pattern and Software Architecture: IV. Design Pattern

Design Pattern and Software Architecture: IV. Design Pattern Design Pattern and Software Architecture: IV. Design Pattern AG Softwaretechnik Raum E 3.165 Tele.. 60-3321 hg@upb.de IV. Design Pattern IV.1 Introduction IV.2 Example: WYSIWYG Editor Lexi IV.3 Creational

More information

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1 Object-Oriented Languages and Object-Oriented Design Ghezzi&Jazayeri: OO Languages 1 What is an OO language? In Ada and Modula 2 one can define objects encapsulate a data structure and relevant operations

More information

An Introduction to Patterns

An Introduction to Patterns An Introduction to Patterns Robert B. France Colorado State University Robert B. France 1 What is a Pattern? - 1 Work on software development patterns stemmed from work on patterns from building architecture

More information

CS 520 Theory and Practice of Software Engineering Fall 2018

CS 520 Theory and Practice of Software Engineering Fall 2018 Today CS 520 Theory and Practice of Software Engineering Fall 2018 Object Oriented (OO) Design Principles September 13, 2018 Code review and (re)design of an MVC application (and encapsulation) Polymorphism

More information

A Case Study of Gang of Four (GoF) Patterns : Part 7

A Case Study of Gang of Four (GoF) Patterns : Part 7 A Case Study of Gang of Four (GoF) Patterns : Part 7 d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University

More information

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/60 ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns Professor Jia Wang Department of Electrical

More information

CS 320 Introduction to Software Engineering Spring March 06, 2017

CS 320 Introduction to Software Engineering Spring March 06, 2017 CS 320 Introduction to Software Engineering Spring 2017 March 06, 2017 Recap: types of Polymorphism Recap: types of Polymorphism Ad-hoc polymorphism (e.g., operator overloading) a + b String vs. int, double,

More information

Design Patterns. Lecture 10: OOP, autumn 2003

Design Patterns. Lecture 10: OOP, autumn 2003 Design Patterns Lecture 10: OOP, autumn 2003 What are patterns? Many recurring problems are solved in similar ways This wisdom is collected into patterns design patterns - about software design Other kinds

More information

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003 What are patterns? Design Patterns Lecture 10: OOP, autumn 2003 Many recurring problems are solved in similar ways This wisdom is collected into patterns design patterns - about software design Other kinds

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Design Patterns Reid Holmes

Design Patterns Reid Holmes Material and some slide content from: - Head First Design Patterns Book - GoF Design Patterns Book Design Patterns Reid Holmes GoF design patterns $ %!!!! $ "! # & Pattern vocabulary Shared vocabulary

More information

CS 520 Theory and Practice of Software Engineering Fall 2018

CS 520 Theory and Practice of Software Engineering Fall 2018 Today CS 520 Theory and Practice of Software Engineering Fall 208 Object Oriented Design Patterns Recap: Object oriented design principles Design problems & potential solutions Design patterns: What is

More information

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques 1 CPSC2620 Advanced Programming Spring 2003 Instructor: Dr. Shahadat Hossain 2 Today s Agenda Administrative Matters Course Information Introduction to Programming Techniques 3 Course Assessment Lectures:

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

18. Inheritance and Polymorphism

18. Inheritance and Polymorphism (Expression) Trees -(3-(4-5))*(3+4*5)/6 18. Inheritance and Polymorphism fork fork / root 6 Expression Trees, Inheritance, Code-Reuse, Virtual Functions, Polymorphism, Concepts of Object Oriented Programming

More information

Object Oriented Programming

Object Oriented Programming OOP Object Oriented Programming Object 2 Object 1 Object 3 For : COP 3330. Object oriented Programming (Using C++) Object 4 http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Objects: State (fields),

More information

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science Containers: Stack Jordi Cortadella and Jordi Petit Department of Computer Science The Stack ADT A stack is a list of objects in which insertions and deletions can only be performed at the top of the list.

More information

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT Containers: Stack The Stack ADT A stack is a list of objects in which insertions and deletions can only be performed at the top of the list. Also known as LIFO Last In, First Out) push insert an element

More information

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011 Design Patterns Lecture 2 Manuel Mastrofini Systems Engineering and Web Services University of Rome Tor Vergata June 2011 Structural patterns Part 2 Decorator Intent: It attaches additional responsibilities

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

The Strategy Pattern Design Principle: Design Principle: Design Principle:

The Strategy Pattern Design Principle: Design Principle: Design Principle: Strategy Pattern The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Design

More information

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar Design Patterns MSc in Communications Software Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

More information

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns? What is a Pattern? Lecture 40: Design Patterns CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki "Each pattern describes a problem which occurs over and over again in our environment, and then describes

More information

Design Patterns. Definition of a Design Pattern

Design Patterns. Definition of a Design Pattern Design Patterns Barbara Russo Definition of a Design Pattern A Pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem,

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

Design Patterns. (and anti-patterns)

Design Patterns. (and anti-patterns) Design Patterns (and anti-patterns) Design Patterns The Gang of Four defined the most common object-oriented patterns used in software. These are only the named ones Lots more variations exist Design Patterns

More information

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.) Design Pattern CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.) A. Design Pattern Design patterns represent the best practices used by experienced

More information

Design patterns. Jef De Smedt Beta VZW

Design patterns. Jef De Smedt Beta VZW Design patterns Jef De Smedt Beta VZW Who Beta VZW www.betavzw.org Association founded in 1993 Computer training for the unemployed Computer training for employees (Cevora/Cefora) 9:00-12:30 13:00-16:00

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

CPSC 310: Sample Final Exam Study Questions 2014S1 (These are in addition to the Study Questions listed at the end of some lectures)

CPSC 310: Sample Final Exam Study Questions 2014S1 (These are in addition to the Study Questions listed at the end of some lectures) CPSC 310: Sample Final Exam Study Questions 2014S1 (These are in addition to the Study Questions listed at the end of some lectures) 1. Select the best functional requirement from the list of requirements

More information

C++ Modern and Lucid C++ for Professional Programmers

C++ Modern and Lucid C++ for Professional Programmers Informatik C++ Modern and Lucid C++ for Professional Programmers part 13 Prof. Peter Sommerlad Institutsleiter IFS Institute for Software Rapperswil, HS 2015 Inheritance and dynamic Polymorphism base classes,

More information

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017 OOP components For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading

More information

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism Review from Lecture 22 Added parent pointers to the TreeNode to implement increment and decrement operations on tree

More information

Software Construction

Software Construction Lecture 7: Type Hierarchy, Iteration Abstraction Software Construction in Java for HSE Moscow Tom Verhoeff Eindhoven University of Technology Department of Mathematics & Computer Science Software Engineering

More information

CS246 Software Abstraction and Specification Final Examination

CS246 Software Abstraction and Specification Final Examination CS246 Software Abstraction and Specification ination Spring 2007 Date: 04-Aug-2007 Time: 4.00 6.30pm Permitted Aids: None 14 pages Student Name: UW Student ID: Instructions: (Read carefully before the

More information

Principles of Software Design. Software Engineering Alessio Gambi Saarland University

Principles of Software Design. Software Engineering Alessio Gambi Saarland University Principles of Software Design Software Engineering Alessio Gambi Saarland University The Challenge Software may live much longer than expected Software must be continuously adapted to a changing environment

More information

CSE 70 Final Exam Fall 2009

CSE 70 Final Exam Fall 2009 Signature cs70f Name Student ID CSE 70 Final Exam Fall 2009 Page 1 (10 points) Page 2 (16 points) Page 3 (22 points) Page 4 (13 points) Page 5 (15 points) Page 6 (20 points) Page 7 (9 points) Page 8 (15

More information

CS304 Object Oriented Programming

CS304 Object Oriented Programming 1 CS304 Object Oriented Programming 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes?

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every

More information

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns Lecture 26: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 6448 - Spring Semester, 2003 Last Lecture Design Patterns Background and Core Concepts Examples Singleton,

More information

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function CS 247: Software Engineering Principles Representation Invariant, Abstraction Function Refresher: Interface Specifications An interface specification is a contract between a module's provider and the client

More information

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413 Type Inference Systems CS412/CS413 Introduction to Compilers Tim Teitelbaum Type inference systems define types for all legal programs in a language Type inference systems are to type-checking: As regular

More information

2.1 Design Patterns and Architecture (continued)

2.1 Design Patterns and Architecture (continued) MBSE - 2.1 Design Patterns and Architecture 1 2.1 Design Patterns and Architecture (continued) 1. Introduction 2. Model Construction 2.1 Design Patterns and Architecture 2.2 State Machines 2.3 Timed Automata

More information

Representation Invariant, Abstraction Function

Representation Invariant, Abstraction Function CS 247: Software Engineering Principles Representation Invariant, Abstraction Function Reading: Barbara Liskov and John Guttag, Program Development in Java: Abstraction, Specification, and Object Oriented

More information

22. Subtyping, Inheritance and Polymorphism

22. Subtyping, Inheritance and Polymorphism 741 Last Week: ression Trees 742 22. Subtyping, Inheritance and Polymorphism ression Trees, Separation of Concerns and Modularisation, Type Hierarchies, Virtual Functions, Dynamic Binding, Code Reuse,

More information

2.1 Design Patterns and Architecture (continued)

2.1 Design Patterns and Architecture (continued) MBSE - 2.1 Design Patterns and Architecture 1 2.1 Design Patterns and Architecture (continued) 1. Introduction 2. Model Construction 2.1 Design Patterns and Architecture 2.2 State Machines 2.3 Abstract

More information

CSCI-1200 Data Structures Fall 2017 Lecture 25 C++ Inheritance and Polymorphism

CSCI-1200 Data Structures Fall 2017 Lecture 25 C++ Inheritance and Polymorphism SI-1200 ata Structures Fall 2017 Lecture 25 ++ Inheritance and Polymorphism Review from Lecture 24 (& Lab 12!) Finish hash table implementation: Iterators, find, insert, and erase asic data structures

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar Design Patterns MSc in Communications Software Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

More information

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CprE 288 Introduction to Embedded Systems Exam 1 Review.  1 CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements

More information

The Composite Design Pattern

The Composite Design Pattern Dr. Michael Eichberg Software Technology Group Department of Computer Science Technische Universität Darmstadt Introduction to Software Engineering The Composite Design Pattern For details see Gamma et

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1 COMP6771 Advanced C++ Programming Week 11 Object Oriented Programming 2016 www.cse.unsw.edu.au/ cs6771 2 Covariants and Contravariants Let us assume that Class B is a subtype of class A. Covariants:

More information

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism Review from Lecture 23 cs2set operations: insert, destroy, printing, erase Tree height calculation illustrates the use of recursion

More information

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns EPL 603 TOPICS IN SOFTWARE ENGINEERING Lab 6: Design Patterns Links to Design Pattern Material 1 http://www.oodesign.com/ http://www.vincehuston.org/dp/patterns_quiz.html Types of Design Patterns 2 Creational

More information

Composite Pattern. IV.4 Structural Pattern

Composite Pattern. IV.4 Structural Pattern IV.4 Structural Pattern Motivation: Compose objects to realize new functionality Flexible structures that can be changed at run-time Problems: Fixed class for every composition is required at compile-time

More information

1 Software Architecture

1 Software Architecture Some buzzwords and acronyms for today Software architecture Design pattern Separation of concerns Single responsibility principle Keep it simple, stupid (KISS) Don t repeat yourself (DRY) Don t talk to

More information

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich CSCD01 Engineering Large Software Systems Design Patterns Joe Bettridge Winter 2018 With thanks to Anya Tafliovich Design Patterns Design patterns take the problems consistently found in software, and

More information

An Introduction to Object Orientation

An Introduction to Object Orientation An Introduction to Object Orientation Rushikesh K Joshi Indian Institute of Technology Bombay rkj@cse.iitb.ac.in A talk given at Islampur Abstractions in Programming Control Abstractions Functions, function

More information

Details of Class Definition

Details of Class Definition Schedule(2/2) Feb. 25th 13:00 Outline of UML: Static Modeling (details of class definition) 14:30 Outline of UML: Dynamic Modeling (state machine, communication diagram, sequence diagram) March. 4th 13:00

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -

More information

Maintainable Software. Software Engineering Andreas Zeller, Saarland University

Maintainable Software. Software Engineering Andreas Zeller, Saarland University Maintainable Software Software Engineering Andreas Zeller, Saarland University The Challenge Software may live much longer than expected Software must be continuously adapted to a changing environment

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Review Questions for Final Exam

Review Questions for Final Exam CS 102 / ECE 206 Spring 11 Review Questions for Final Exam The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR, chs.

More information

STRICT_VARIANT. A simpler variant in C++ Chris Beck

STRICT_VARIANT. A simpler variant in C++ Chris Beck STRICT_VARIANT A simpler variant in C++ Chris Beck https://github.com/cbeck88/strict_variant What is a variant? A variant is a heterogenous container. std::vector many objects of one type std::variant

More information

LECTURE 18. Control Flow

LECTURE 18. Control Flow LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a

More information

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

class Polynomial { public: Polynomial(const string& N = no name, const vector<int>& C = vector<int>());... }; Default Arguments 1 When declaring a C++ function, you may optionally specify a default value for function parameters by listing initializations for them in the declaration: class Polynomial { public:

More information

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees Binary Tree (Chapter 0. Starting Out with C++: From Control structures through Objects, Tony Gaddis) Le Thanh Huong School of Information and Communication Technology Hanoi University of Technology 11.1

More information

COURSE 2 DESIGN PATTERNS

COURSE 2 DESIGN PATTERNS COURSE 2 DESIGN PATTERNS CONTENT Fundamental principles of OOP Encapsulation Inheritance Abstractisation Polymorphism [Exception Handling] Fundamental Patterns Inheritance Delegation Interface Abstract

More information

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming 1. What is object-oriented programming (OOP)? OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object

More information