Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Science 340 Software Design & Testing Software Reuse.

Similar presentations


Presentation on theme: "1 Computer Science 340 Software Design & Testing Software Reuse."— Presentation transcript:

1 1 Computer Science 340 Software Design & Testing Software Reuse

2 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 3 Benefits of reuse Timeliness Decreased maintenance effort Reliability Efficiency Consistency Lower cost Less redundancy in the work

4 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 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 6 Forms of reuse Binaries Source code Design patterns Specifications –Requirements, Functional, Design People –Knowledge and experience

7 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)

8 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 9 Adapter: convert a class’s interface

10 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

11 Adapter 11

12 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(); }... }

13 Adapter 13

14 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

15 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();

16 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"); }

17 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 18 Decorator : augment a class’s functionality using composition

19 Decorator Pattern 19

20 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 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 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 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 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 25 Examples of reusable code Reusable Array class –C++C++ –JavaJava Reusable Manager interface –JavaJava Reusable InputStream decorators –JavaJava

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

27 Strategy Pattern 27

28 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 29 Strategy pattern : customize a class’s functionality with plug-in algorithms

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

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

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

33 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 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 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 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 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 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 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


Download ppt "1 Computer Science 340 Software Design & Testing Software Reuse."

Similar presentations


Ads by Google