1 Computer Science 340 Software Design & Testing Software Reuse.

Slides:



Advertisements
Similar presentations
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Object-oriented Programming Concepts
Feb. 23, 2004CS WPI1 CS 509 Design of Software Systems Lecture #5 Monday, Feb. 23, 2004.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Reuse Activities Selecting Design Patterns and Components
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
OOP Languages: Java vs C++
Object Oriented Software Development
Software Engineering Muhammad Fahad Khan
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 18 Slide 1 Software Reuse.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Ceg860 (Prasad)L6MR1 Modularity Extendibility Reusability.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
1 Computer Science 340 Software Design & Testing Inheritance.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Frameworks CompSci 230 S Software Construction.
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Object Oriented Programming
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Coming up: Inheritance
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
From Use Cases to Implementation 1. Structural and Behavioral Aspects of Collaborations  Two aspects of Collaborations Structural – specifies the static.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
From Use Cases to Implementation 1. Mapping Requirements Directly to Design and Code  For many, if not most, of our requirements it is relatively easy.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Examples (D. Schmidt et al)
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Chapter 11 Object-Oriented Design
Behavioral Design Patterns
Object Oriented Analysis and Design
Object Oriented Design Patterns - Structural Patterns
Advanced Programming Behnam Hatami Fall 2017.
More About Inheritance & Interfaces
CS 325: Software Engineering
Presentation transcript:

1 Computer Science 340 Software Design & Testing Software Reuse

2 The quest for software reuse Reuse is an important goal of software design Software developers are notorious for “reinventing the wheel” by repeating work that has been done many times in the past. For years people have touted the virtue of the reuse achieved by hardware engineers, who frequently use off-the-shelf (OTS) components in their designs. The hope has been that similar reuse could be achieved through properly designed software “components”.

3 Benefits of reuse Timeliness Decreased maintenance effort Reliability Efficiency Consistency Lower cost Less redundancy in the work

4 Examples of successful reuse OS APIs are used by all programs that run on the OS (e.g., the Windows scroll bar) Built-in libraries provided by programming languages are reused by all programs written in that language (e.g., C++ STL) Third-party libraries that perform generally-useful functions, such as: OpenSSL (secure communication), zlib (compression), boost, Qt (useful C++ libraries) Application Frameworks: GUI Frameworks (WPF), Web App Frameworks (RoR), Enterprise App Frameworks (Java EE), etc.

5 Forces operating against reuse At first glance, the lack of reuse in software makes little sense, but there are reasons for it. What conditions must exist to make a class reusable? –Generality (customizable to the present need) –Performant (general without sacrificing performance) –Available across programming languages and operating systems –Easily obtainable (inexpensive or free, reasonable licensing, downloadable on web) –Well documented –Well supported and maintained –Source code available –Awareness (if I don’t know about it, I can’t use it) –Maturity and Humility (overcome NIH syndrome and the urge to do it myself) If all relevant conditions are satisfied, reuse will often occur.

6 Forms of reuse Binaries Source code Design patterns Specifications –Requirements, Functional, Design People –Knowledge and experience

7 Techniques for reusing / customizing class behavior Composition –Class A “uses” Class B to do its work –Reuse a class by creating an object and calling methods on it –Create “wrappers” to add additional functionality, if needed –The most common form of code reuse (we do this all the time) Inheritance –Inherit superclass functionality –Customize superclass behavior by overriding its methods Totally replace or augment with pre/post processing –Extend superclass functionality by adding new variables/methods Parameterization –Design a class so its behavior/functionality can be customized to the current application –Generic Types E.g., C++ templates, Java and C# generics (e.g., ArrayList ) –Provide “options” that can be customized by the client through constructor parameters, method parameters, or properties E.g., a GUI ListBox class might provide the following sorting options (None, Ascending, Descending)

Adapter Pattern You have a class that does what you need, but the interface it supports is not the interface expected by the rest of the application. You can write an "adapter" class to translate between the interface expected by clients, and the interface provided by the class. Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interface 8

9 Adapter: convert a class’s interface

Adapter Pattern Why not just modify the class to support the interface required by its clients? 1) You don't have the source code for the third-party class 2) You have the source code, but you don't want to couple the class to the interface expected by clients (i.e., in general there is a different interface that you prefer for the class, but for integration purposes you must support the mandated interface). 10

Adapter 11

12 public class AddressBook { List personList; public int getSize() {...} public int addPerson(Person p) {...} public Person getPerson(int index) {...}... } public interface TableModel { public int getRowCount(); public int getColumnCount(); public String getColumnName(int columnIndex); public Object getValueAt(int rowIndex, int columnIndex); … } public class AddressBookTableAdapter implements TableModel { AddressBook ab; public AddressBookTableAdapter( AddressBook ab ){ this.ab = ab; } public int getRowCount() { return ab.getSize(); } public int getColumnCount() { return 3; } public String getColumnName(int columnIndex) { switch (columnIndex) { case 0: return "First Name”; case 1: return "Last Name”; case 2: return " Address"; } public Object getValueAt(int rowIndex, int columnIndex) { Person p = ab.getPerson(rowIndex); switch (columnIndex) { case 0: return p.getFirstName(); case 1: return p.getLastName(); case 2: return p.get Address(); }... }

Adapter 13

Decorator Pattern Sometimes we need to be able to add functionality to an object in a flexible and dynamic way –Don’t want to change the base functionality, just add to it –Window  Window with scrollbars Can be done with inheritance and composition 14

Decorator with Inheritance 15 class A { void run() { System.out.println("A.run"); } class B extends A { void run() { System.out.println("B.run pre-processing"); super.run(); System.out.println("B.run post-processing"); } class C extends B { void run() { System.out.println("C.run pre-processing"); super.run(); System.out.println("C.run post-processing"); } // // Client // A obj = new C(); obj.run();

Decorator with Composition 16 interface Runner { void run(); } class A implements Runner { void run() { System.out.println("A.run"); } class B implements Runner { Runner delegate; B(Runner delegate) { this.delegate = delegate; } void run() { System.out.println("B.run pre-processing"); delegate.run(); System.out.println("B.run post-processing"); }

Decorator with Composition cont. 17 class C implements Runner { Runner delegate; C(Runner delegate) { this.delegate = delegate; } void run() { System.out.println("C.run pre-processing"); delegate.run(); System.out.println("C.run post-processing"); } // // Client // Runner obj = new C(new B(newA())); obj.run();

18 Decorator : augment a class’s functionality using composition

Decorator Pattern 19

20 // the Window abstract class public abstract class Window { public abstract void draw(); // draws the Window public abstract String getDescription(); // returns a description of the Window } // extension of a simple Window without any scrollbars class SimpleWindow extends Window { public void draw() { // draw window } public String getDescription() { return "simple window"; }

21 // abstract decorator class - note that it extends Window abstract class WindowDecorator extends Window { protected Window decoratedWindow; // the Window being decorated public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; } public void draw() { decoratedWindow.draw(); //delegation } public String getDescription() { return decoratedWindow.getDescription(); //delegation }

22 // the first concrete decorator which adds vertical scrollbar functionality class VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); public void draw() { super.draw(); drawVerticalScrollBar(); } private void drawVerticalScrollBar() { // draw the vertical scrollbar public String getDescription() { return super.getDescription() + ", including vertical scrollbars"; }

23 // the second concrete decorator which adds horizontal scrollbar functionality class HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); public void draw() { super.draw(); drawHorizontalScrollBar(); } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar public String getDescription() { return super.getDescription() + ", including horizontal scrollbars"; }

24 public class DecoratedWindowTest { public static void main(String[] args) { // create a decorated Window with horizontal and vertical scrollbars Window decoratedWindow = new HorizontalScrollBarDecorator ( new VerticalScrollBarDecorator(new SimpleWindow())); // print the Window's description System.out.println(decoratedWindow.getDescription()); }

25 Examples of reusable code Reusable Array class –C++C++ –JavaJava Reusable Manager interface –JavaJava Reusable InputStream decorators –JavaJava

26 Examples of reusable code Generic functions/methods –C++ max function template max –Java arrayToCollection generic method arrayToCollection

Strategy Pattern 27

28 Strategy pattern : customize a class’s functionality with plug-in algorithms Class C implements some generally useful functionality C does most of the work, but some aspects of its behavior can be customized by clients Clients customize C’s behavior by passing in “algorithms” that are invoked by C at appropriate times The “algorithms” passed in by clients are called “strategies” C invokes the client-provided strategies as needed to perform its work A “strategy” is an object that implements an algorithm This is an alternative to using inheritance to customize a class’s behavior

29 Strategy pattern : customize a class’s functionality with plug-in algorithms

30 Strategy pattern : customize a class’s functionality with plug-in algorithms The following examples of reusable code use the Strategy pattern

31 Strategy pattern : customize a class’s functionality with plug-in algorithms Examples: –Input validationInput validation –SorterSorter

32 Examples of reusable code Sort(Person[]); –How could we make this sorting algorithm more reusable?

33 Examples of reusable code Sort(Person[]); –How could we make this sorting algorithm more reusable? Sort(Comparable[]) –interface Comparable { int compareTo(Object o); }

34 Examples of reusable code Sort(Person[]); –How could we make this sorting algorithm more reusable? Sort(Comparable[]) –interface Comparable { int compareTo(Object o); } Sort(Object[], Comparator) –interface Comparator { int compare(Object o1, Object o2); }

35 Examples of reusable code Sort(Person[]); –How could we make this sorting algorithm more reusable? Sort(Comparable[]) –interface Comparable { int compareTo(Object o); } Sort(Object[], Comparator) –interface Comparator { int compare(Object o1, Object o2); } Anybody remember qsort from 240? –Very reusable

36 Examples of reusable code Java TreeSet generic class –CodeCode –Instantiating TreeSet requires that SomeClass implement the Comparable interface so the tree can sort the elements TreeSet Comparable –What if SomeClass doesn’t implement Comparable ? –Or, what if we need a sort order different from the one SomeClass implements? –Can we still use TreeSet ? –Yes. TreeSet has a constructor that lets you pass in a comparator object that defines the sort order to be used. –public TreeSet(Comparator c)Comparator

37 Examples of reusable code C++ map class template template class map { … } Key and value types can be specified (of course) By default, uses Key’s < operator to sort If you don’t like that, Comparator class can be specified to customize sorting –Overloads bool operator ()(Key a, Key b) Memory allocator class can be specified to customize memory management –Implements a standard memory allocator interface ( allocate block, deallocate block, etc.)

38 Examples of reusable code CLogger classCLogger Logging class that provides customizable thread synchronization behavior If multiple threads will be logging messages, thread synchronization will be needed If only one thread will be logging messages, no thread synchronization is needed In order to avoid unnecessary overhead due to thread synchronization, the CLogger class allows the client to specify the desired thread synchronization behavior

39 Examples of reusable code Client class Client Client.send method supports a generic method for acquiring user credentials (user name & password) This allows the Client class to be reused in many kinds of applications