• Keine Ergebnisse gefunden

Advanced Aspects of Object-Oriented Programming (SS 2011) Practice Sheet 4 (Hints and Comments)

N/A
N/A
Protected

Academic year: 2022

Aktie "Advanced Aspects of Object-Oriented Programming (SS 2011) Practice Sheet 4 (Hints and Comments)"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

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 2011) Practice Sheet 4 (Hints and Comments)

Exercise 1 Questions Exercise 2 Super-Calls

p u b l i c c l a s s A {

p u b l i c v o i d m( ) { S y s t e m . o u t . p r i n t l n ( ’ ’A ’ ’ ) ; } }

p u b l i c c l a s s B {

p u b l i c v o i d m( ) { s u p e r.m ( ) ; } }

p u b l i c c l a s s C {

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 ) { new C ( ) . m ( ) ;

} }

With static binding new C().m() results in a call to the method m of class A. With dynamic binding it would end in an endless recursion, because the supertype of the current this would be B, so the call would again be dispatched to m in B.

Exercise 3 Tiny Web Server

a)

b) See enclosed source. To be able to compile the complete webserver without changing the whole source, the old code was marked as deprecated but remained in the implementation. It can be removed if all classes use the delegation pattern instead of subtyping.

Exercise 4 University Administration System - Reloaded

a) Advantages:

• Easy to change the current role.

• Easy to extend with new roles, as that does not affect any of the non-role specific parts, whereas subtypes accidentally could modify the behavior of the role independent parts unintended.

• ...

Disadvantages:

• No code for free, everything has to be written for each role

• Guarantees of the type system cannot be used anymore. E.g. no static guarantee anymore, that professors don’t register for exams (every one is just a person)

• ...

The given scenario would do something like:

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 ) {

S t u d e n t s = new S t u d e n t ( "MaxMustermann " ) ;

A s s i s t a n t a = new A s s i s t a n t ( s ) ; / / c o p y c o n s t r u c t o r , i . e . c o p y a l l i n f o r m a t i o n t h a t r e m a i n v a l i d / / f r o m t h e e x i s t i n g s t u d e n t o b j e c t t o t h e new a s s i s t a n t o b j e c t

/ / i n v a l i d a t e s , r e m o v e s f r o m a l l c o l l e c t i o n s , . . . P r o f e s s o r p = new P r o f e s s o r ( a ) ; / / same a s f o r a s s i s t a n t }

(2)

b) If the set of types/roles can be given at compile time and it will not change for an object during its lifecycle, it is usually a good idea to use subtyping and, if appropriate, inheritance. If the role of an object is likely to change, it may be better to take a delegation approach. In general, the decision depends on a) the language support, b) the used libraries (some favor delegation, some are designed with subtyping in mind) c) the knowledge of the programmer, etc. Bigger software projects usually use both, e.g. parts of the software that have strong dependencies between them may use subtyping and parts that are not integrated so much may use delegation or forwarding.

Exercise 5 StoJas Extension

See the solution to sheet 5.

2

Referenzen

ÄHNLICHE DOKUMENTE

the concepts of classification, generalization and aggregation, are provided but also modularity (keeping logically related things together}, information hiding

Advanced Aspects of Object-Oriented Programming. Summer 2015

Advanced Aspects of Object-Oriented Programming. Summer

c) The output is overloadedMethod (Collection <?>), because overloading is resolved at compile time and at compile time the parameter t has type List < T > , such that

Refine the behavior of the getChar() method such that it returns the next character of the string if the position is not at the end and that it returns -1 if the reader is at the end

b) If the set of types / roles can be given at compile time and it will not change for an object during its lifecycle, it is usually a good idea to use subtyping and, if

b) Implement a StringReader. First add an interface equivalent to the BlankReader including the specification of the behavior. Second add an implementation of StringReader that

b) If the set of types / roles can be given at compile time and it will not change for an object during its lifecycle, it is usually a good idea to use subtyping and, if