Skip to topic | Skip to bottom
Minfo.SeTD3y1314r1.3 - 20 Oct 2013 - 21:35 - PhilippeCollettopic end

Start of topic | Skip to actions

Lab 3: Genericity and Inheritance

Generic Methods for Filtering & String Handling

Start with the correction of the previous lab: (add the lib directory with the mockito jar).

A new package "common" appears. It contains a Utils class with generic methods to :

  • represent functions as objects (a predicate, a function2 that applies b to a and returns the result)
  • filter out from a iterable structure by a predicate (filter)
  • reduce, which applies a function (2 params) from left to right in a list (see its application in mkstring)
  • mkstring that concatenates elements with a given separator (using "reduce")

Work to do:

  • Implement the missing code inside the filter function
  • Use "filter" to modify the getProperties method in "AbstractCreature" so that static fields are not taken into account. You can use a new Predicate with the following apply code:
public boolean apply(Field input) {
   return !Modifier.isStatic(input.getModifiers());
  • replace the "mkString" method in toString of "AbstractCreature" by the Utils version. This should work flawlessly while being generic!
  • refactor the code of creatureAround() so to use a filter function

Inheritance and refactoring

In the first lab, you were asked final questions about the code structure of the first version of the simulator, questions like:

  • What to do with some of the utility functions?
  • What to do with the fact that logic of the Environment and the visualization are kept together?

Use the refactoring support in Eclipse (right-click and get into submenus on elements you want to refactor) to get the following inheritance hierarchy for creatures: creatures.png

Then do another refactoring to abstract and separate the simulation from its visualisation. You should be close to the following organization (proceed step by step and with eclipse refactoring as much as possible) : simuvisu.png

TIP There is plenty of help available on the web. Remember also the Vogella's tutorial in our resources, there is something on refactoring:

At home

If you want to complete this lab at home, think about testing the generic string methods of the first exercise:

  • What kind of things would you test? generic parameters ? specific forms of strings ?
  • Then build a test suite that covers all relevant aspects

-- PhilippeCollet - 20 Oct 2013
to top

I Attachment sort Action Size Date Who Comment manage 17.4 K 20 Oct 2013 - 20:21 PhilippeCollet  
creatures.png manage 102.4 K 20 Oct 2013 - 21:33 PhilippeCollet  
simuvisu.png manage 107.9 K 20 Oct 2013 - 21:33 PhilippeCollet  

You are here: Minfo > SeTD3y1314

to top

Copyright © 1999-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding WIKIDeptinfo? Send feedback