LINGI2252 SoftwareReuse OOP
LINGI2252 SoftwareReuse OOP
net/publication/312213177
CITATIONS READS
0 1,519
1 author:
Kim Mens
Université Catholique de Louvain - UCLouvain
239 PUBLICATIONS 2,308 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Kim Mens on 11 January 2017.
SOFTWARE REUSE *
A. SOFTWARE REUSE
SOFTWARE REUSE 3
REUSABILITY [DEFINITION]
Reusability is a general engineering principle whose importance
derives from the desire to avoid duplication and to capture
commonality in undertaking classes of inherently similar tasks.
Reusable component
WHY REUSE?
Economic justification:
Intellectual justification:
Design patterns
Software architecture
B. OBJECT-ORIENTED PROGRAMMING
OBJECT-ORIENTED PROGRAMMING 10
ABSTRACTION MECHANISMS
Encapsulation
Information hiding
Polymorphism
Code sharing
SMALLTALK’S INFLUENCE
Smalltalk is a pure object-oriented language
DISCLAIMER
THIS SESSION MAY CONTAIN TRACES OF SMALLTALK CODE
(FOR DIDACTIC PURPOSES)
OBJECT-ORIENTED PROGRAMMING 15
Hierarchies of classes
circumference
OBJECT-ORIENTED PROGRAMMING 17
e.g., with a Circle class you can create many circle objects
▸ Hierarchies of classes
POLYMORPHIC METHODS
A same message can be sent to objects of different classes
aCircle.surface aRectangle.surface
ADVANTAGES OF POLYMORPHISM
Cleaner, more maintainable code
Locality
EXAMPLE OF POLYMORPHISM
Procedural style vs. object-oriented style
Example:
surface(collection) : Real
total = 0
∀ shape ∈ collection :
if ( shape == Circle ) then
total = total + circleSurface(shape)
else if ( shape == Rectangle ) then
total = total + rectangleSurface(shape)
return total
OBJECT-ORIENTED PROGRAMMING 27
Advantages:
LATE BINDING
When sending a message, the actual receiver of a message is not
necessarily known until run-time
Rectangle {
Real bottom, top, right, left;
Real surface() : { (bottom-top)*(right-left) }
}
Sources:
http://beginnersbook.com/2013/04/java-static-dynamic-binding/
http://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java
OBJECT-ORIENTED PROGRAMMING
Source: http://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java
OBJECT-ORIENTED PROGRAMMING
Source: http://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java
OBJECT-ORIENTED PROGRAMMING 35
Because c is statically
determined to have type Collection
▸ Hierarchies of classes
HIERARCHIES OF CLASSES
Shape
colour (Colour)
Rectangle Circle
bottom top center radius
left right surface π.radius2
HIERARCHIES OF CLASSES
Classes are typically organised into hierarchical structures
HIERARCHIES OF CLASSES
Inheritance is a powerful incremental reuse mechanism Container
colour contents
Often you don’t want to rewrite everything; you just want some
small changes to what exists
▸ Hierarchies of classes
METHOD OVERRIDING
Subclasses can re-implement methods that are already implemented in
superclasses
An overridden method
▸ Hierarchies of classes
▸ Hierarchies of classes
KINDS OF INHERITANCE
Different kinds of inheritance
Single: 1 superclass
Interface
Mixin modules
OBJECT-ORIENTED PROGRAMMING 52
SINGLE INHERITANCE
Organises classes in tree structures
Units of reuse
code reuse
design reuse
MULTIPLE INHERITANCE
Sometimes it is convenient to have a Vehicle
class which has multiple parents velocity location
https://en.wikipedia.org/wiki/Multiple_inheritance
OBJECT-ORIENTED PROGRAMMING 55
or through linearisation
FlyingCar
duplicated state
harder to solve
OBJECT-ORIENTED PROGRAMMING 56
INTERFACES
Java has single inheritance
You can inherit from one class and from multiple interfaces
simultaneously
http://stackoverflow.com/questions/3556652/how-do-java-interfaces-simulate-multiple-inheritance
OBJECT-ORIENTED PROGRAMMING 57
FoodTruck
http://stackoverflow.com/questions/3556652/how-do-java-interfaces-simulate-multiple-inheritance
OBJECT-ORIENTED PROGRAMMING 58
FoodTruck Kitchen
cook cook
http://stackoverflow.com/questions/3556652/how-do-java-interfaces-simulate-multiple-inheritance
OBJECT-ORIENTED PROGRAMMING 59
MIXINS
Factoring out the increment when subclassing
CONCLUSION
OO promotes maintainability by viewing programs as collections of loosely connected
objects
Objects can be defined and manipulated in terms of the messages they understand
and ignoring the implementation details
Such components can be created and tested as independent units in isolation from the
rest of the software system
objects contain their own data as well as the methods that work on that data
Information hiding
implementation details of how it processes these messages remain hidden to external clients
Polymorphism
cleaner and more maintainable code by delegating responsibilities and implementation choices
to the objects
Code sharing
LEARNING OBJECTIVES
▸ definitions of reusability, software reuse and reusable component
POSSIBLE QUESTIONS
▸ Define and illustrate, in your own words, the notions of software reuse, reusability and reusable
components.
▸ Give two economic and two intellectual justifications for software reuse.
▸ Give (and explain) at least 3 different software reuse techniques seen throughout the course.
▸ How and why does object-oriented programming promote modularity and maintainability?
▸ Explain the object-oriented techniques of encapsulation, information hiding, polymorphism and code
sharing and their link with software reusability.
▸ Explain, using a concrete example, what polymorphism and dynamic binding is, and how it can lead to
more maintainable code.
▸ Explain on a concrete example the concepts of method overriding, self and super calls.
▸ Explain, using a concrete example, how a multiple inheritance problem could be modelled in terms of
single inheritance on classes and interfaces in Java.