Exercise Session 1 Adriana Ispas Today’S Exercise Session

Exercise Session 1 Adriana Ispas Today’S Exercise Session

Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 1 Adriana Ispas Today’s Exercise Session Pattern of the Day The Visitor Pattern Quizzes What is the Result? Class Initialisation Instance Initialisation Assignment I Calculator Component Languages in Depth series: Java Programming 2 The Visitor Pattern Creates an external class to act on data in other classes Useful if Fair number of instances of a small number of classes Need to perform some operation that involves all or most of them Languages in Depth series: Java Programming 3 Motivation Similar code for drawing each object class Different drawing methods But all use utility functions duplicated in each class Closely related functions scattered throughout several different classes Solution Visitor class contains all the related draw methods and visits each object Languages in Depth series: Java Programming 4 Visiting Languages in Depth series: Java Programming 5 Example: Vacation Days Report Languages in Depth series: Java Programming 6 Example: Visitor to Compute the Sum No indication what the Visitor does with each class in either the client classes or the abstract Visitor class Write different visitors that do different things Languages in Depth series: Java Programming 7 Example: Visiting the Classes Languages in Depth series: Java Programming 8 Example: Iterate 1. We move through a loop of all the Employees. 2. The Visitor calls each Employee’s accept method. 3. That instance of Employee calls the Visitor’s visit method. 4. The Visitor fetches the vacation days and adds them into the total. 5. The main program prints out the total when the loop is complete Languages in Depth series: Java Programming 9 Double Dispatching Visitor calls polymorphic accept method of a given object Accept method calls polymorphic visit method of the Visitor Thus May add more operations on any class that has an accept method New Visitor class can carry out operations using the data available in these classes Languages in Depth series: Java Programming 10 Traversing a Series of Classes The calling program that passes the class instances to the Visitor must know about all the existing instances of classes to be visited Commonly used for abstract syntax trees Languages in Depth series: Java Programming 11 Consequences Useful when Need to obtain data from a collection of unrelated classes and utilize it to compute a global calculation But . Data needs to be made available through public methods Easy to add new operations Visitor contains the code instead of each individual class But . When new class needs to be visited need to update visitors accordingly Languages in Depth series: Java Programming 12 Quiz 1: What is the result? true true 6 5 6 References f1, z, and f3 refer to the same instance final assures that a reference variable cannot be referred to a different object . But final doesn’t keep the object’s state from changing Languages in Depth series: Java Programming 13 Quiz 2: Class Initialization Years left: -2004 Languages in Depth series: Java Programming 14 Object Creation Review 1. new allocates memory for a new instance 2. static fields are initialized at the defaults 3. static fields are initialized at chosen values 4. Non static fields are initialized at the defaults 5. Non static fields are initialized at chosen values 6. The constructor is invoked Languages in Depth series: Java Programming 15 Quiz 2: Class Initialization … 1. Initialization of the class EuropeanFootballChampionship triggered by the VM's call to its main method 2. Static fields are set to their default values • INSTANCE is set to null • YEAR is set to 0 3. Static field initializers are executed in order of appearance • INSTANCE computed by invoking the constructor • YEAR is still 0 • elapsedYears is – 2004 • … Languages in Depth series: Java Programming 16 Quiz 2: Class Initialization …… It is possible to observe a final static field before it is initialized to the chosen value Fix a class initialization cycle Reorder the static field initializers so that each initializer appears before any initializers that depend on it Languages in Depth series: Java Programming 17 Quiz 3: Instance Initialization [4,2]:null Languages in Depth series: Java Programming 18 Quiz 3: Instance Initialization … It is possible to observe the value of a final instance field before its value has been assigned Problem Constructor calls a method overridden in its subclass . The method runs before the instance has been initialized, when its declared fields still have their default values Avoid this problem Never call overridable methods from constructors Lazy initialization vs. eager initialization . Initialize field when first used vs. when instance created Languages in Depth series: Java Programming 19 Quiz 3: Instance Initialization …… Languages in Depth series: Java Programming 20 Assignment 1 Calculator Component Languages in Depth series: Java Programming 21 Questions? Languages in Depth series: Java Programming 22 Exercise Session 2 Pattern of the Day The Template Method Pattern Quizzes Assignment I Correction Assignment II Hand-out Languages in Depth series: Java Programming 23.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    23 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us