Introduction Summary References
SOFTWARE FRAMEWORKS
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 22, 2015
Introduction Summary References
OUTLINE I
1 INTRODUCTION
2 SUMMARY
3 REFERENCES
Introduction Summary References
INTRODUCTION
Software frameworks are one of the primary means forlarge scale software reuse, and rapid applicationdevelopment.In this chapter we will examine the technologies that makesoftware frameworks possible.The Tension between Reuse and Specialization.Reuse of Code, Reuse of Concept.High and Low Level Abstractions.Some Example Frameworks.
Introduction Summary References
TENSION BETWEEN REUSE AND SPECIALIZATION
A truly general purpose tool cannot contain featuresspecific to any one application.Solving most problems requires application specificfeatures.How do you bridge the gap between general purpose andapplication independent tools and an application that willsolve a real problem?
Introduction Summary References
REUSE OF CODE, REUSE OF CONCEPT
The solution comes from the two ways we have been usinginheritance from the beginning of the book.Reuse of code.Certain methods, call foundation methods, are defined in ageneral purpose class.These provide functionality that is common to manyapplications.Reuse of concept.Other methods, called specialization methods, are definedin a parent class, but overridden in child classes.These provide the means to specialize an application to anew situation.
Introduction Summary References
A SIMPLE EXAMPLE, A GUI SYSTEM
A simple example might be a framework for userinterfaces:
Class Window: methods.foundation: setTitle, setWindowSize, moveWindowTo,addMenu, repaintspecialization: mouseDown, keyPress, paint
class Childwindow: methodspaint, mouseDown, keyPress
The foundation method are applicable to any type ofwindow.The deferred specialization methods are appropriate onlyto one type of application.
Introduction Summary References
DEFERRED METHODS
By working with deferred methods, the application classviews the application in one way, and the child class inanother.
Introduction Summary References
REUSE OF HIGH LEVEL ABSTRACTIONS
Software frameworks provide a different type of reuse:Conventional libraries of procedures provide a means forreuse of low level abstractions (I/O libraries, mathfunctions, and so on).Software frameworks provide a means for reuse of highlevel abstractions, and still allow them to be specialized fornew situations.
Introduction Summary References
AN EXAMPLE OF A LOW LEVEL ABSTRACTIONSORTING EMPLOYEE RECORDS
Suppose we want to sort employee records. We could write thefollowing.
EXAMPLE
class Employee {public :
s t r i n g name ;i n t sa la ry ;i n t s ta r t i ngYe a r ;
}
void s o r t ( Employee ∗ data [ ] , i n t n ) {for ( i n t i = 1 ; i < n ; i ++) {
i n t j = i −1;while ( j >= 0 &&
v [ j +1]−> s ta r t i ngYea r < v [ j ]−>s ta r t i ngYe a r ) {/ / swap elementsEmployee ∗ temp = v [ j ] ;v [ j ] = v [ j + 1 ] ;v [ j +1] = temp ;j = j − 1;
}}
}
But what happens if we want to change it?
Introduction Summary References
TYPES OF REUSE
We can reuse the idea of a merge sort, but cannot reusethe binary without modifications to the original sourcecode.Might want to sort on salary, instead of starting year.Might want to do comparisons of string (e.g., name), notintegers.Might want to sort a different type of structure.To create an object-oriented software framework, we mustfirst ask ourselves what are the likely sources of change?
Introduction Summary References
A SORTING FRAMEWORK
EXAMPLE
class I n s e r t i o n S o r t e r {public :
void s o r t ( ) {i n t n = s ize ( ) ;for ( i n t i = 1 ; i < n ; i ++) {
i n t j = i − 1;while ( j >= 0 && lessThan ( j +1 , j ) ) {
swap ( j , j +1 ) ;j = j − 1;
}}
}
private :v i r t u a l i n t s ize ( ) = 0 ; / / abs t r ac t methodsv i r t u a l boolean lessThan ( i n t i , i n t j ) = 0 ;v i r t u a l void swap ( i n t i , i n t j ) = 0 ;
}
The part that is common in made into a foundation method, thepart that changes are made into deferred methods.
Introduction Summary References
SPECIALIZING THE SORTING FRAMEWORK
To apply the framework to a new problem, we subclass andoverride the deferred methods:
EXAMPLE
class EmployeeSorter : public I n s e r t i o n S o r t e r {public :
EmployeeSorter ( Employee ∗ d [ ] , i n t n ){ data = d ; sze = n ; }
private :Employee ∗ data [ ] ;i n t sze = n ;
v i r t u a l i n t s ize ( ) { return sze ; }
v i r t u a l bool lessThan ( i n t i , i n t j ){ return data [ i ]−>s ta r t i n gY e a r < data [ j ]−>s t a r t i ngYe a r ; }
v i r t u a l void swap ( i n t i , i n t j ) {Employee ∗ temp = v [ i ] ;v [ i ] = v [ j ] ;v [ j ] = temp ;
}}
We can now reuse the high level algorithm without making anychange to the original source code!
Introduction Summary References
NOT ONE CLASS
The impression that a framework is always just one class iswrong.Often, a framework is a collection of many classes.For example, a typical GUI framework might have.Window classes.Button or scroll bar classes.Text box classes.All can be specialized by the combination of foundationmethods for overall structure, and deferred methods forspecialization.
Introduction Summary References
FLEXIBILITY AND RIGIDITY
A framework can be tremendously helpful in allowing aprogrammer to rapidly create new application, but onlywhen the application fits into the general structureenvisioned by the creator of the framework.If an application falls outside that framework, then it can bevery difficult to overcome the framework.For example, if the designer of the framework has notencapsulated the right sources of variation in a method, orhas forgotten to declare a method as virtual, then it can bevery difficult to work with.
Introduction Summary References
AN EXAMPLE APITHE JAVA APPLET API
The Java Applet API is one simple example of a softwareframework.
init() Invoked when the applet is initializedstart() Invoked to start the applicationpaint(Graphics) Invoked to repaint the windowstop() Invoked when the applet is halteddestroy() Invoked when the applet is terminated
Lots of
other classes for constructing buttons and menus, and so on.
Introduction Summary References
SIMULATION FRAMEWORK
Here is a framework like we might use in Chapter 7 (thebilliards game).
EXAMPLE
Graphica lObject = ob jec t(∗ data f i e l d s ∗)
l i n k : Graph ica lObjec t ;reg ion : Rect ;
(∗ i n i t i a l i z a t i o n f u n c t i o n ∗)procedure setRegion ( l e f t , top , r i g h t , bottom : i n t e g e r ) ;
(∗ opera t ions t h a t g raph i ca l ob jec ts perform ∗)procedure draw ;procedure update ;f u n c t i o n i n t e r s e c t ( anObj : Graph ica lObjec t ) : boolean ;procedure h i tBy ( anObj : Graphica lObject ) ;
end ;
Introduction Summary References
A GENERALIZED EVENT DRIVEN SIMULATION
FRAMEWORK I
A generalized discrete event-driven simulation can be formedbased around the class Event:
EXAMPLE
class Event {public :
Event ( unsigned i n t t ) : t ime ( t ) { }
const unsigned i n t t ime ;v i r t u a l void processEvent ( ) = 0 ;
} ;
class eventComparison {public :
bool operator ( ) ( event ∗ l e f t , event ∗ r i g h t ){ return l e f t −>t ime > r i g h t −>t ime ; }
} ;
Introduction Summary References
A GENERALIZED EVENT DRIVEN SIMULATION
FRAMEWORK II
An event is an action that will take place at a specific time.
Discrete event driven simulations were the type ofapplication that helped drive the design of the firstobject-oriented programming language, Simula. (Early1960’s).
Introduction Summary References
THE SIMULATION CLASS I
CODE
class Simula t ion {public :
S imu la t ion ( ) : eventQueue ( ) , currentTime ( 0 ) { }
void scheduleEvent ( event ∗ newEvent ) { eventQueue . push ( newEvent ) ; }void run ( ) ;unsigned i n t currentTime ;
protected :p r i o r i t y_queue <vector , eventComparison > eventQueue ;
} ;
void Simula t ion : : run ( ) {/ / execute events u n t i l event queue becomes emptywhile ( ! eventQueue . empty ( ) ) {
event ∗ nextEvent = eventQueue . top ( ) ;eventQueue . pop ( ) ;t ime = nextEvent−>t ime ;nextEvent−>processEvent ( ) ;delete nextEvent ;
}}
Introduction Summary References
THE SIMULATION CLASS II
The book continues with the development of a simulationbased on this framework.
Introduction Summary References
SUMMARY
A framework is a way of organizing classes so as to solvea class of related problemsThe framework balances software reuse and the ability tospecialize a tool to a new applicationThe framework achieves this by combining inheritance ofcode and inheritance of concept (overriding).Frameworks can be developed for any application whereyou can extract and generalize the ways in which code willchange.Frameworks are great if your new application fits thescheme of the designed, but very inflexible if it does not.
Introduction Summary References
REFERENCES
Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.An Introduction to Object Oriented Programming, TimothyBudd.This presentation is developed with Beamer:
Darmstadt, spruce.