• Keine Ergebnisse gefunden

Advanced Aspects of Object-Oriented Programming (SS 2012) Practice Sheet 5 (Hints and Comments)

N/A
N/A
Protected

Academic year: 2022

Aktie "Advanced Aspects of Object-Oriented Programming (SS 2012) Practice Sheet 5 (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 2012) Practice Sheet 5 (Hints and Comments)

Exercise 1 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 subclassing.

Exercise 2 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:

public static void main(String... argv) { Student s = new Student("Max Mustermann");

Assistant a = new Assistant(s); // copy constructor, i.e. copy all information that remain valid // from the existing student object to the new assistant object

// invalidate s, remove s from all collections, ...

Professor p = new Professor (a); // same as for assistant }

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 3 StoJas Extension

a) Syntax:Extend the syntax to include theforstatement.

Statement = ...

| for ( VarId = Exp; Exp; Statement ) { Statement } ;

(2)

Context Conditions: ForVarId = Expthe context conditions used for normal assignments have to be fulfilled, especially the loop variableVarId has to be declared before the loop; the second expression (the termination expression) has to be of type boolean; the first statement is an arbitrary statement, usually this should be a statement that changes the loop variable, but we do not force it to be of that kind.

Static Semantics:No changes needed.

Dynamic Semantics: We add a new rule for the new statement. This rule expresses the semantics of the for statement as the semantics of an equivalent while statement.

S : x=e1; while (e2) { s2; s1 }→SQ S: for ( x=e1; e2; s1 ) { s2 }; →SQ

b) Syntax:For the static methods we use a similar syntax as for the super calls and we extend the method declarations with the keyword static.

MethodDecl = ...

| static TypeId MethodId(TypeId p) { Statement } Statement = ...

| VarId = ClassId@MethodId( Exp ) ;

Context Conditions:The statement of the method body is not allowed to use the variablethis(neither reading nor writing).

Static Semantics: The setMethodin the static semantics can now contains both static and non-static methods.

The rest of the semantics does not change.

Dynamic Semantics:Same as the super call, as it is a statically bound call as well.

S[p:=S(e), res:=init(rtyp(C@m))] : body(C@m)→SQ S: x=C@m(e); →S[x :=SQ(res), $:=SQ($)]

c) Syntax:Include static field declarations and statements to read and write the static fields.

FieldDecl = ...

| static TypeId FieldId ; Statement = ...

| VarId = ClassId@FieldId ;

| ClassId@FieldId = Exp ;

Context Conditions:Anything that holds for non-static fields has to hold for static fields as well. (Type compati- bility in assignments, etc.)

To implement the static fields, there are two possibilities. A) Model them as a part of the state, B) use a class-object and consider them to be the instance variables of the class object. We look at a version A here, for a solution using class objects you may refer to Exercise 4 of the year 2008.

Static Semantics:The state space of the program is enlarged. The state now contains not only instance variables

<c,o,f>and the store but also the static fields. So first of all, we define the set of static fields, it is similar to the instance variable set, but as static fields belong to classes and not to instances, there is no object reference.

StatFields = { <c,f> | f is a static field of class c } The state now is defined as

S tate=(VarId∪ {$} ∪ {ClassId@FieldId})→(Value∪S tore) Dynamic Semantics:We introduce two new rules for read and write access to the static fields.

S: x=C@f; →S[x :=S(C@f)] S: C@f=e; →S[C@f :=S(e)]

When a static field changes, this change remains valid when returning from a method call. Same as a change in the store. Therefor we have to copy the changes originating of an execution of a method body to the calling state.

The final states of the call rules therefor change to:

S[x :=SQ(res), $ :=SQ($), C@f :=SQ(C@f) for all C@f]

For static local variables (not supported by Java) the extension would look similar, just useC@m@vfor the variables and make sure, that they do not get reinitialized at each method call.

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 &lt;?&gt;), because overloading is resolved at compile time and at compile time the parameter t has type List &lt; T &gt; , 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

a) Write a generic static method flip which takes an object of class Pair (see the slides of the lecture) and flips the elements of the given Pair object. Hint: In order to flip