Вы находитесь на странице: 1из 19

Computer Science 340

Software Design & Testing


Software Reuse

Ken Rodham 2006

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.
2

Benefits of reuse

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

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), GTK (GUI toolkit).

Forces operating against reuse

On the face of it, 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 dont know about it, I cant use it)
Maturity and Humility (overcome NIH syndrome and urge to do it myself)

If all relevant conditions are satisfied, reuse will often occur.


5

Forms of reuse

Binaries
Source code
Design patterns
Specifications
Requirements, Functional, Design

Personnel
6

Code-level reuse techniques

Composition
Class A uses Class B
Reuse a class by creating an object and calling methods on it
The most common form of code reuse (we do this all the time)

Parameterization
Write 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<Device>)

Provide options that can be customized by the client through constructor


parameters, method parameters, or property setters
E.g., a GUI ListBox class might provide the following sorting options (None,
Ascending, Descending)

Inheritance

To reuse a class,
1) Inherit its functionality
2) Customize superclass behavior by overriding its methods in the subclass
3) Extend superclass functionality by adding new variables/methods in the
subclass
7

Adapter: a reuse-related design


pattern
Client

Target
+Request()

Adapter
+Request()

Adaptee
+SpecificRequest()

...
adaptee->SpecificRequest();
...

Examples of reusable code


Reusable Array class
Bad C++ example
Good C++ example
Good Java example

Examples of reusable code


Sort(Person[]);
How could we make this sorting algorithm more
reusable?

10

Examples of reusable code


Sort(Person[]);
How could we make this sorting algorithm more
reusable?

Sort(Comparable[])
interface Comparable { int compareTo(Object o); }

11

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

12

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

13

Examples of reusable code


Java TreeSet generic class
Code

Instantiating TreeSet<SomeClass> requires that SomeClass


implement the Comparable interface so the tree can sort the elements
What if SomeClass doesnt 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<? super E> c)

14

Examples of reusable code


C++ map class template

template <typename Key, typename Value, typename


Comparator, typename Allocator > class map { }
Key and value types can be specified (of course)

By default, uses Keys < operator to sort


If you dont 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.)
15

Examples of reusable code


CLogger class

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

16

Examples of reusable code


EmailClient class
EmailClient.send method supports a generic method
for acquiring user credentials (user name & password)

This allows the EmailClient class to be reused in many


kinds of applications

17

Examples of reusable code


Generic functions/methods
C++ max function template
Java arrayToCollection generic method

18

Examples of reusable code


Inheritance is one of the primary
mechanisms for reusing code
The following lectures focus specifically
on inheritance

19