Software reuse means to “borrow” existing code. How can you reuse an existing class to make a new one? Such reuse is really an adaptation -- using the.

Slides:



Advertisements
Similar presentations
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - First In First Out The first (most distant)
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Abstraction, Inheritance, and Polymorphism in Java.
Inheritance using Java
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Program Errors Syntax errors Logic errors
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Course: Object Oriented Programming - Abstract Data Types Unit1: IntroductionSlide Number 1 Introduction Course: Object Oriented Programming Abstract Data.
Software Design Patterns (2) four commonly used design patterns using php (singleton, factory, adapter & decorator)
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - Fast In First Out The first (most distant) item.
Example Consider the following class specification for a class that stores a bunch of characters. /* class invariant *this bunch contains one or more char.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Coming up: Inheritance
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Example of Aggregation 1) aggregation - the class contains other structure(s) 2) specialization - the new class is a special case of the data structure.
Unit Testing Part 2: Drivers and Stubs
CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Inheritance, Polymorphism and Abstract Classes. Student Management System All students are CUNY Students CUNY Students are Queens College students, or.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
There are three possible lifetime patterns for Java data. Static Data The data is created when the program begins execution and is retained until the program.
A table is a useful way to capture a discrete function. Days Per Month MonthLength 1) Jan 31 2) Feb 28 3) Mar ) Dec 31 Cost Per Burger QuantityItem.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Sections 3.4 Formal Specification
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
03/10/14 Inheritance-2.
Inheritance and Encapsulation
Week 3 - Friday CS221.
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Type Abstraction SWE Spring 2009.
Chapter 10: An Array Instance Variable
Subtyping Rules David Evans cs205: engineering software BlackBear
Data Abstraction David Evans cs205: engineering software
Lecture 4: Data Abstraction CS201j: Engineering Software
EECE 310: Software Engineering
مظفر بگ محمدی دانشگاه ایلام
Adapter Design Pattern
C++ Programming CLASS This pointer Static Class Friend Class
Chapter 8 Inheritance Part 2.
Data Structures: Abstract Data Types (ADTs)
Type Abstraction SWE Spring 2013.
Abstract Data Types ADT: A set of objects and a set of operations on those objects. examples: integers: +, - , *, … Collection, insert, remove, … set:
CIS 110: Introduction to computer programming
Presentation transcript:

Software reuse means to “borrow” existing code. How can you reuse an existing class to make a new one? Such reuse is really an adaptation -- using the adapter design pattern Example: Create a SetOf3Doublets class by adapting a SimpleStringOfSet class. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Domain set of String Invariant (for every SetOfDoublets) this ->size() == 3 and exists( e : this-> includes(e) | e.length() == 2 ) Constructor public SetOfDoublets(String s1, String s2, String s3) pre: s1.length()==2 and s2.length()==2 and s3.length==2 and s1!=s2 and s1!=s3 and s2!=s3 post: this == set{ s1, s2, s3 } Query Methods public boolean isIn(String s) post: result == this-> includes( s ) public boolean equals(SetOf3Doublets s) post: result == ( this = s ) “ = “ denotes set equality Update Methods public void replace( String oldS String newS) pre: newS.length()==2 and isIn(oldS) (throws IllegalStateException) post: this == SetOf3Doublets ADT Specifications The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

SetOf3Doublets «constructor» + SetOf3Doublets(String s1, String s2, String s3) «query» + boolean isIn(String s) + String equals(SetOf3Doublets s) «update» + void replace(String oldS, String newS) How would you reuse SimpleSetOfString to design SetOf3Doublets? SimpleSetOfString «constructor» + SimpleSetOfString() «query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s) «update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Solution 1: Use Inheritance SetOf3Doublets «constructor» + SetOf3Doublets(String s1, String s2, String s3) «query» + boolean isIn(String s) + String equals(SetOf3Doublets s) «update» + void replace(String oldS, String newS) SimpleSetOfString «constructor» + SimpleSetOfString() «query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s) «update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

public class SetOf3Doublets extends SimpleSetOfString { public SetOf3Doublets(String s1, String s2, String s3) { super(); add(s1); add(s2); add(s3); } public void replace(String oldS, String newS) { remove(oldS); add(newS); } This is an example of the adapter design pattern, because SetOf3Doublets is adapting SimpleSetOfString via inheritance. Methods that must be coded: SetOf3Doublets replace Methods that are reused without change: isIn equals The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Potential Problem with the inheritance version of the adapter design pattern SetOf3Doublets «constructor» + SetOf3Doublets(String s1, String s2, String s3) «query» + boolean isIn(String s) + String equals(SetOf3Doublets s) «update» + void replace(String oldS, String newS) SimpleSetOfString «constructor» + SimpleSetOfString() «query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s) «update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss) Unwanted members are inherited: size add remove union intersection inheriting size not harmful use of add, remove, and union might violate invariant The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Solution 2: Proxy Design Pattern SetOf3Doublets - SimpleSetOfString theSet «constructor» + SetOf3Doublets(String s1, String s2, String s3) «query» + boolean isIn(String s) + String equals(SetOf3Doublets s) «update» + void replace(String oldS, String newS) SimpleSetOfString «constructor» + SimpleSetOfString() «query» + int size() + boolean isIn(String s) + String equals(SimpleSetOfString s) «update» + void add(String s) + void remove(String s) + void union(SimpleSetOfString ss) + void intersection(SimpleSetOfString ss) theSet is a “proxy” that allows SetOf3Doublets to access SimpleSetOfString members. The proxy design pattern uses aggregation to accomplish the adapter design pattern. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

public class SetOf3Doublets { private SimpleSetOfString theSet; public SetOf3Doublets(String s1, String s2, String s3) { theSet = new SimpleSetOfString(); theSet.add(s1); theSet.add(s2); theSet.add(s3); } public void replace(String oldS, String newS) { theSet.remove(oldS); theSet.add(newS); } public boolean isIn(String s) { return theSet.isIn(s); } public boolean equals(SetOf3Doublets ss) { return theSet.equals(ss); } Proxy disadvantages: 1) must “reimplement” methods (see isIn & equals ) 2) slightly messier notation “ theSet.” Proxy advantage: provides maximum control over class members