K. Stirewalt CSE 335: Software Design Synthetic OOD concepts and reuse Lecture 4: Separation of concerns Topics: –Complex concern: Memory management –

Slides:



Advertisements
Similar presentations
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 211 Inheritance AAA.
Software Testing and Quality Assurance
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 Software Testing and Quality Assurance Lecture 28 – Testing Class Hierarchies.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes Topics: –Economics of software.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures Topics: –Elaboration of.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract.
K. Stirewalt CSE 335: Software Design Synthetic OOD concepts and reuse Lecture 3: Separation of concerns Topics: –Separation of concerns as a general principle.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 5: Mixins and reusable strategies Topics: – More on the use of mixins.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CIT241 Prerequisite Knowledge ◦ Variables ◦ Operators ◦ C++ Syntax ◦ Program Structure ◦ Classes  Basic Structure of a class  Concept of Data Hiding.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes Topics: –Economics of software.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 5: Mixins and reusable strategies Topics: – More on the use of mixins.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Huang-Ming Huang, Shih-Ling Chen, Sajeeva Pallemulle.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes Topics: –Economics of software.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Eine By: Avinash Reddy 09/29/2016.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Section 11.1 Class Variables and Methods
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Testing with OO OO has several key concepts:
Object-Oriented Programming
CISC/CMPE320 - Prof. McLeod
Chapter 11: Inheritance and Composition
Lecture 10 Concepts of Programming Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

K. Stirewalt CSE 335: Software Design Synthetic OOD concepts and reuse Lecture 4: Separation of concerns Topics: –Complex concern: Memory management – Example: Complex operations on composite structures – Problem: Memory leaks – Solution: Reference counting

K. Stirewalt CSE 335: Software Design Motivation Suppose we define a function: Expr* mkExpr(void) { Expr* l1 = new Literal(2); Expr* v = new Variable(“x”); Expr* t1 = new Add(l1,v); Expr* l2 = new Literal(2); Expr* t2 = new Multiply(l2,t1); }

K. Stirewalt CSE 335: Software Design Problem Suppose a client program executes the code:... Expr* e = mkExpr(); e->print(cout); delete e;... What happens? How could we ensure that when a composite object is deleted, its parts are also deleted?

K. Stirewalt CSE 335: Software Design Solution class Expr {... virtual ~Expr() {} }; class BinaryExpr : public Expr {... ~BinaryExpr() { delete(leftOperand); delete(rightOperand); }... };

K. Stirewalt CSE 335: Software Design Or is it? Often desirable for two or more composite objects to share the same component Example: Expr* l = new Literal(2); Expr* v = new Variable(“x”); Expr* t1 = new Add(l,v); Expr* t2 = new Multiply(l,t1); Here, object l is the child of two composite objects, t1 and t2. Will our memory-reclamation scheme work now?

K. Stirewalt CSE 335: Software Design Deleting composites that share parts Not safe for destructor of a composite class to delete its children if these objects might be children of multiple composite objects Solution: design a protocol that tracks references to objects and that deletes an object only when its reference count reaches 0. –Clients never call delete –Objects call delete on themselves! –Clients must explicitly notify objects when references to these objects are assigned or overwritten

K. Stirewalt CSE 335: Software Design Abstract ref-counting class class RCObject { public: void addRef() { ++refCount; } void removeRef() { if(--refCount <= 0) delete this; } protected: RCObject() : refCount(0) {} virtual ~RCObject() {} private: int refCount; };

K. Stirewalt CSE 335: Software Design Example Modify class Expr as follows: class Expr : public RCObject {... }; Subsequently, every class that derives from Expr will inherit reference counting infra- structure

K. Stirewalt CSE 335: Software Design Example Modify BinaryExpr as follows: class BinaryExpr : public Expr {... BinaryExpr( Expr* l, Expr* r ) : leftOperand(l), rightOperand(r) { l->addRef(); r->addRef(); }... ~BinaryExpr() { l->removeRef(); r->removeRef(); } };

K. Stirewalt CSE 335: Software Design Example (continued) Now we write a function that creates an expression: Expr* createExpression(void) { Expr* l = new Literal(2); Expr* v = new Variable(“x”); Expr* t1 = new Add(l,v); Expr* t2 = new Multiply(l,t1); return t2; } Requires client to increment refCount of root object (pointed to here by t2 before it goes out of scope)

K. Stirewalt CSE 335: Software Design Step 1 Expr* createExpression(void) { Expr* l = new Literal(2); Expr* v = new Variable(“x”); Expr* t1 = new Add(l,v); Expr* t2 = new Multiply(l,t1); return t2; } l : Literal refCount=0

K. Stirewalt CSE 335: Software Design Step 2 Expr* createExpression(void) { Expr* l = new Literal(2); Expr* v = new Variable(“x”); Expr* t1 = new Add(l,v); Expr* t2 = new Multiply(l,t1); return t2; } l : Literal refCount=0 v : Variable refCount=0

K. Stirewalt CSE 335: Software Design Step 3 Expr* createExpression(void) { Expr* l = new Literal(2); Expr* v = new Variable(“x”); Expr* t1 = new Add(l,v); Expr* t2 = new Multiply(l,t1); return t2; } l : Literal refCount=1 v : Variable refCount=1 t1 : Add refCount=0

K. Stirewalt CSE 335: Software Design Step 4 Expr* createExpression(void) { Expr* l = new Literal(2); Expr* v = new Variable(“x”); Expr* t1 = new Add(l,v); Expr* t2 = new Multiply(l,t1); return t2; } l : Literal refCount=2 v : Variable refCount=1 t1 : Add refCount=1 t2 : Multiply refCount=0

K. Stirewalt CSE 335: Software Design Example (continued) Client code then uses as follows:... Expr* e = createExpression(); e->addRef(); // new line e->print(cout); e->removeRef(); // rather than delete(e)...

K. Stirewalt CSE 335: Software Design Adding reference to returned object e->addRef(); e->print(cout); e->removeRef(); l : Literal refCount=2 v : Variable refCount=1 t1 : Add refCount=1 t2 : Multiply refCount=1 e

K. Stirewalt CSE 335: Software Design Printing expression... e->addRef(); e->print(cout); e->removeRef(); l : Literal refCount=2 v : Variable refCount=1 t1 : Add refCount=1 t2 : Multiply refCount=1 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 1 e->addRef(); e->print(cout); e->removeRef(); l : Literal refCount=1 v : Variable refCount=1 t1 : Add refCount=1 t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 2 e->addRef(); e->print(cout); e->removeRef(); l : Literal refCount=1 v : Variable refCount=1 t1 : Add refCount=0 t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 3 e->addRef(); e->print(cout); e->removeRef(); l : Literal refCount=0 v : Variable refCount=1 t1 : Add refCount=0 t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 4 e->addRef(); e->print(cout); e->removeRef(); l : Literal refCount=0 v : Variable refCount=1 t1 : Add refCount=0 t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 5 e->addRef(); e->print(cout); e->removeRef(); v : Variable refCount=0 t1 : Add refCount=0 t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 6 e->addRef(); e->print(cout); e->removeRef(); v : Variable refCount=0 t1 : Add refCount=0 t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 7 e->addRef(); e->print(cout); e->removeRef(); t1 : Add refCount=0 t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Removing ref: Step 8 e->addRef(); e->print(cout); e->removeRef(); t2 : Multiply refCount=0 e

K. Stirewalt CSE 335: Software Design Conceptual (but flawed) alternative implementation of createExpression() Expr* createExpression(void) { Expr* l = new Literal(2); l->addRef(); Expr* v = new Variable(“x”); v->addRef(); Expr* t1 = new Add(l,v); t1->addRef(); Expr* t2 = new Multiply(l,t1); t2->addRef(); l->removeRef(); v->removeRef(); t1->removeRef(); t2->removeRef(); // What happens here??? return t2; }

K. Stirewalt CSE 335: Software Design “Mixin” classes RCObject is an example of a “mixin” class –So-called because designed to encapsulate orthogonal functionality that may be “mixed into” classes in an existing hierarchy –Mixin classes are always abstract –May use multiple inheritance to incorporate functionality in a mixin class if base class does not need it or if base class needs functionality from multiple mixin classes