Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Kathrin Geilmann
University of Kaiserslautern Department of Computer Science Software Technology Group
Advanced Aspects of Object-Oriented Programming (SS 2010) Practice Sheet 4
Date of Issue: 29.04.10Deadline: 05.05.10 (until 10 a.m. as PDF via E-Mail)
Exercise 1 Tiny Web Server
You can find the sources of a simple Java-based web server on the lecture’s web page.Important notice: The sources are meant to illustrate/practice concepts of the lecture. Students will refactor/redesign/extend parts of the software system, so currently existing deficiencies are intentional.
a) Download the ZIP file, unzip it, and start the server viaant clean compile run. Check if everything works by requesting the URLhttp://localhost:8080/public_html/HelloWorld.htmlusing a web browser.
b) Analyse the classesSimpleWebServerandLoggableClass. Introduce an appropriate interface and refactor the existing code of these two classes so forwarding/delegation is used instead of inheritance.
Exercise 2 University Administration System - Reloaded
The delegation pattern can be used to simulate inheritance, but it is a more general design pattern (seehttp://en.
wikipedia.org/wiki/Delegation_(programming)). Note, the meaning of the terms delegation and forwarding varies in the literature, each author has a slightly different notion of them.
Until now the UAS used inheritance to model the different persons at a university, look at the listing below to see a different implementation, which uses delegation to establish the link between a person and the role, it currently has at the university. In this implementationPerson-objects delegate some calls to theirRole-object. Figure 1 shows the architecture of the implementation.
package p e r s o n s ; c l a s s P e r s o n {
p u b l i c S t r i n g name ; p u b l i c R o l e r o l e ;
p u b l i c P e r s o n ( S t r i n g name ) {}
p u b l i c v o i d a s s i g n R o l e ( R o l e r ) { r o l e = r ; } p u b l i c v o i d p r i n t ( ) {
i f ( r o l e == n u l l)
S y s t e m . o u t . p r i n t l n ( " Notmuchknowna b o u t" + name ) ; e l s e
r o l e . p r i n t (t h i s) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g . . . a r g v ) { P e r s o n p = new P e r s o n ( "MaxMustermann " ) ;
/ / Max s t a r t s h i s c a r e e r p . a s s i g n R o l e (new S t u d e n t ( ) ) ;
/ / Max g r a d u a t e s and s t a r t s w o r k i n g a t t h e u n i v e r s i t y p . a s s i g n R o l e (new A s s i s t a n t ( ) ) ;
/ / and f i n a l l y he manages t o become a p r o f e s s o r p . a s s i g n R o l e (new P r o f e s s o r ( ) ) ;
} }
i n t e r f a c e R o l e {
p u b l i c v o i d p r i n t ( P e r s o n p ) ; }
c l a s s P r o f e s s o r i m p l e m e n t s R o l e { S t r i n g room ;
S t r i n g i n s t i t u t e ;
p u b l i c v o i d p r i n t ( P e r s o n p ) {
S y s t e m . o u t . p r i n t ( " P r o f e s s o r" + p . name + " ’ so f f i c ei si nroom" + room ) ; }
}
Role
Professor role
Assistant Student Person
Figure 1: Architecture of the Delegation-based UAS
c l a s s S t u d e n t i m p l e m e n t s R o l e { i n t reg_num ;
p u b l i c v o i d p r i n t ( P e r s o n p ) {
S y s t e m . o u t . p r i n t ( p . name + "h a st h er e g i s t r a t i o nnumber" + reg_num ) ; }
}
c l a s s A s s i s t a n t i m p l e m e n t s R o l e { b o o l e a n p h D S t u d e n t ;
p u b l i c v o i d p r i n t ( P e r s o n p ) {
S y s t e m . o u t . p r i n t ( p . name + "i saPhDs t u d e n t :" + p h D S t u d e n t ) ; }
}
a) Can you think of advantages and disadvantages of the delegation based implementation compared to an inheritance based implementation ? How does the scenario of the main method looks like in a inheritance based system?
b) Formulate a general guideline, when to favor inheritance over delegation and vice versa.
Exercise 3 Super-Calls
Write a program, that would behave differently under the assumption of dynamically boundsuper-calls than it does with statically boundsuper-calls.