1 CSC 330 Object-Oriented Software Design REUSABILITY
Overview 2 Reuse concepts Impediments to reuse Reuse case studies Objects and reuse Reuse during the design and implementation phases Reuse and maintenance Portability Techniques for achieving portability
Reuse Concepts 3 Two types of reuse Accidental reuse» First, product is built» Then, parts put into part database for reuse Planned reuse» First, reusable parts are constructed» Then, products are built using these parts
Why reuse? 4 Minor Reason It is expensive to design, implement, test, and document software Only 15% of new code serves an original purpose (average) Reuse of parts saves» Design costs» Implementation costs» Testing costs» Documentation costs Major Reason Maintenance Maintenance consumes two-thirds of software cost
Raytheon Missile Systems Division 5 Data-processing software Planned reuse of Designs» 6 code templates COBOL code» 3200 reusable modules Reuse rate 60% (1976 1982)
Toshiba Fuchu Works, Tokyo 6 Industrial process control systems Accidental reuse of Specifications Designs Modules Contracts Manuals Standards Reuse rate (1985) 33% design 48% code
NASA Software 7 Ground support system for unmanned spacecraft control Management permitted (but did not encourage) accidental reuse Accidental reuse of Modules Reuse rate (1982) 28% reused unchanged 10% reused with minor changes
GTE Data Services 8 Data-processing software Strongly encouraged by management Cash incentives when module was accepted for reuse Cash incentive when module was reused Accidental reuse of Modules Reuse rate (1988) 15% reused code, $1.5 million saved (est. 1989) 20% reused code (est. 1993) 50% reused code
Hewlett-Packard 9 Implemented in many divisions of the company Software Technology Division Accidental reuse of resource planning software 4.1 faults per KLOC of new code, 0.9 for reused code Overall fault rate decreased 51% Productivity increased 57% Cost $1 million, savings $4.1 million (1983 92)
Hewlett-Packard (contd) 10 San Diego Technical Graphics (STG) Planned reuse of firmware for printers Cost $2.6 million, savings $5.6 million (1987 94) 24% reduction in faults 40% increase in productivity Cost of developing reusable firmware 11% more Cost of reusing it 19% of developing from scratch
European Space Agency 11 Ariane 5 rocket blew up 37 seconds after lift-off Cost: $500 million Reason: attempt to convert 64-but integer into 16- bit unsigned integer, without Ada exception handler On-board computers crashed, so did rocket Conversion was unnecessary Computations on the inertial reference system can stop 9 seconds before lift-off But, if there is a subsequent hold in countdown, it takes several hours to reset the inertial reference system Computations therefore continue 50 seconds into flight
European Space Agency (contd) 12 Cause of problem Ten years before, it was mathematically proven that overflow was impossible on the Ariane 4 Because of performance constraints, conversions that could not lead to overflow were left unprotected Software was used, unchanged and untested, on Ariane 5 But, the assumptions for the Ariane 4 no longer held Lesson Software developed in one context needs to be retested when integrated into another context
Size of Reused Components 13 NASA Most reused components were small Toshiba Many large components were reused GTE Many large components were reused Reason A strong managerial commitment for reuse is needed
Reuse During Design and Implementation 14 Design reuse Library or toolkit Framework Design pattern Software architecture
Library or Toolkit 15 Set of reusable routines Examples: Scientific software GUI class library or toolkit The user is responsible for the control logic (white in figure)
Application Framework 16 Control logic of the design Hot spots (white in figure) Faster than reusing toolkit More of design is reused The logic is usually harder to design than the operations
Design Pattern 17 A solution to a general design problem In the form of a set of interacting classes The classes need to be customized (white in figure)
Widget Generator 18 Tool that uses the set of classes created by the widget generator
Abstract Factory Pattern 19 Abstract classes and abstract (virtual) methods The interfaces between client and program and generator are abstract The application program is uncoupled from the specific operating system
Software Architecture 20 Encompasses a wide variety of design issues, including: Organization in terms of components How those components interact
Reuse of Software Architecture 21 Architecture reuse can lead to large-scale reuse One mechanism: Software product lines Case study: Hewlett-Packard printers (1995 to 1998)» Person-hours to develop firmware decreased by a factor of 4» Time to develop firmware decreased by factor of 3» Reuse has increased to over 70% of components
Reuse and Maintenance 22 Reuse impacts maintenance more than development Assumptions 30% of entire product reused unchanged 10% reused changed Savings during maintenance are nearly 18% Savings during development are about 9.3%
Objects and productivity 23 Reuse achieved Not via modules with functional cohesion, but via objects (informational cohesion) [classes] In general Software costs have decreased Overall quality has improved Large products are essentially collection of smaller products
Difficulties and Problems 24 Learning curve Particularly noticeable with GUI Problems with inheritance New subclass does not affect its superclass But, any change to a superclass affects all its subclasses Subclasses low in the inheritance tree can be huge (inherited attributes) Polymorphism and dynamic binding Maintenance problems were already discussed
Incompatibilities 25 Hardware (disk, tape, characters, parity) Operating system (JCL, virtual memory) Numerical software (word size, Ada) Compiler FORTRAN Pascal COBOL C Ada C++ Java
Why Portability? 26 Difficulties hampering portability One-off software Hardware incompatibility Lifetimes of software, hardware Multiple copy software Portability saves money!
Portability strategies 27 Portable system software Isolate implementation-dependent pieces» UNIX kernel, device-drivers Levels of abstraction» Graphics
Object-Oriented Principles 28 OOP Encapsulation (class) -- Information Hiding -- Separation of Interface and Implementation -- Standardization -- Access Control mechanisms (private /public) Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Virtual functions -- Abstract Base Classes
Why Object-oriented paradigm? 29 Separation of interface and implementation: Any implementation change should not affect user interface. To ease design process: Separation of Design definition, Implementation, Usage through data encapsulation. Software reuse : To allow (1) sharing, (2) upgrades (3) modification using inheritance and polymorphism. Parameterized classes in templates.
From problem to OO solution 30 Identify the objects ( nouns ) in the problem. Define and implement classes to represent the objects. Class name Data members : public, private, (protected) Function members» Service functions (usually public)» Utility functions (usually private)» Predicate functions. Example: warnings, status indicators, error indicators, etc. Write a driver that carries out the interaction among the objects.
Basic syntax : class definition 31 class class_name { public: list of class attributes (variables, types, constants, and so on) that may be accessed by name from outside the class. list of prototypes for each member function that may be accessed by name from outside the class. private: list of class attributes (variables, types, constants, and so on) that are intended to be hidden for reference from outside the class. list of prototypes for each member function intended to be hidden from outside of the class. };
32 Object-Oriented Principles Using inheritance to promote software reusability Use multiple inheritance to derive a class from several base classes
Inheritance 33 Software reusability. Create new (derived) class from existing (base) class or classes. New derived class is to inherit the data members and member functions of a previous defined base class. New derived class can have its own members (attributes and behaviors).
Inheritance 34 In addition, new class can embellish the members (attributes and behaviors) of base class. Derived class can be large, but more specific. Derived class can also be a base class for future derived class. Single inheritance: derived from one base class Multiple inheritance: derived from multiple base classes.
Example Inheritance 35 Inheritance forms tree-hierarchical structure Examples: Shape TwoDimensionalShape ThreeDimensionalShape circle square triangle sphere cube tetrahedron How to define a derived class? This is public inheritance. class TwoDimensionalShape : public Shape { };
Multiple Inheritance 36
Definition of Class Base1 37 class Base1 { public: Base1(int x) { value = x; } int getdata( ) const { return value; } protected: int value; };
Definition of Class Base2 38 class Base2 { public: Base2(char c) { letter = c; } char getdata( ) const {return letter; } protected: char letter; };
Definition of Class Derived 39 // multiple inheritance class Derived : public Base1, public Base2 { friend ostream &operator << (ostream & const Derived &); public: Derived(int, char, double); double getreal( ) const; private: double real; };
Member Function Definitions for Class Derived 40 Derived::Derived(int i, char c, float f) : Base1(i), Base2(c) { real = f; } double Derived::getReal( )const { return real; } ostream &operator<<(ostream &output, const Derived &d) { output << Interger: << d.value << endl << Character: << d.letter << endl << Real number: << d.real; return output; }
Driver for Multiple Inheritance Example 41 int main( ) { Base1 b1( 10 ), *base1ptr = 0; Base2 b2( z ), *base2ptr = 0; Derived d(7, A, 3.5 ); cout<< Object b1 contains integer < b1.getdata() << \nobject b2 contains char << b2.getdata( ) << \nobject d contains:\n << d << \n\n ; cout<< Data members of Derived can be << accessed individually: << \n Integer: << d.base1::getdata( ) << \n Character: << d.base2::getdata( ) << \nreal number: << d.getreal( ) << \n\n ;
Driver for Multiple Inheritance Example 42 } cout << Derived can be treated as an << object of either base class:\n ; base1ptr = &d; cout << base1ptr->getdata( ) yields << base1ptr->getdata( ) << \n ; base2ptr = &d; cout << base2ptr->getdata( ) yields << base2ptr->getdata( ) << endl; return 0;
43 Points to Remember Software Engineering Observations
Software Engineering Observations 44 A derived class cannot directly access private members of its base class.
Software Engineering Observations 45 Suppose we create an object of a derived class where both the base class and the derived class contain objects of other classes. When an object of that derived class is created, first the constructors for the base class member objects execute, then the base-class constructor executes, then the constructors for the derived class member objects execute, then the derived class constructor executes. Destructors are called in the reverse of the order in which their corresponding constructors are called.
Software Engineering Observations 46 In inheritance, base-class constructors are called in the order in which inheritance is specified in the derived-class definition. The order in which the base-class constructors are specified in the derived-class member initializer list does not affect the order of construction.
Software Engineering Observations 47 Creating a derived class does not affect it s base class s source code or object code; the integrity of a base class is preserved by inheritance.
Software Engineering Observations 48 In an object-oriented system, classes are often closely related. Factor out common attributes and behavior and place these in a base class. Then use inheritance to form derived classes.
Software Engineering Observations 49 A derived class contains the attributes and behaviors of its base class. A derived class can also contain additional attributes and behaviors. With inheritance, the base class can be compiled independent of the derived class. Only the derived class s incremental attributes and behaviors need to be compiled to be able to combine these with the base class to form a derived class.
Software Engineering Observations 50 Modifications to a base class do not require derived classes to change as long as the public and protected interfaces to the base class remain unchanged. Derived classes may, however, need to be recompiled.
Software Reusability 51 Focus on use of classes (object oriented programs) make possible general classes that are applicable to other projects Class libraries exist and may be searched for classes which you can use or adapt for your project