CompSci 307
Spring 2019
Software Design and Implementation

Java Reflection

Reflection is an amazing feature of many modern programming languages that lets you dynamically create objects and call methods based on String values instead of having to know their exact names when the code is compiled. This flexibility lets you more easily hold their values in data structures or data files instead of hardcoding them directly within your code. It also makes it easier to provide names for Lambdas and refer to them dynamically instead of having to hardcode their relationship to the code that uses them.

Getting Started

The project contains updated copies of two previous examples, Roulette and Browser, so you do not have to worry about what state your version currently is in.

For this lab exercise, work together with someone else in the class:

Work in pairs to implement the changes described below to example code we have discussed this semester. Create a commit for each step below as your programming pair completes it.

Roulette Factory

  1. Create a Factory class that hides the creation of Bet subclasses.
    • For this step just move the List<Bet> of hard coded subclasses to the Factory class (so you are just extracting the existing code from one class to another).
    • Also move the promptForBet() method to the Factory class because it should also handle printing out the menu of choices rather than the Game class.
    • Finally update the Game class to have a Factory instance variable instead of the List<Bet> and call its promptForBet() method instead.
  2. Change the Factory class to use reflection instead of a hardcoded list of concrete Bet subclasses.
    • For this step change the List<Bet> to a List<String>, where the strings represent the complete name of each Bet subclass (so you can focus just on implementing reflection).
    • Create a new method makeBet() that gets a Class object representing the Bet subclass named in the given string using the forName() method, then gets its Constructor object, and calls the newInstance() method (and catches its many exceptions) instead of using the instance directly in the previous list.
    • Note, you can use either the given default constructor for each Bet subclass or create some logic to provide the description and odds for each subclass — whichever makes more sense for your project.
  3. Change the Factory class to get the class names from a properties file instead of a hardcoded list.
    • For this step create a properties file and, in the Factory class constructor, read it using a ResourceBundle object to determine which subclasses are possible to create so that nothing is hardcoded in your Factory class.
    • Note, the reflection code itself should likely not need to change because you are using the properties file to populate the List instance variable(s).
    • Note, the properties file should map either a general bet name to the complete Bet subclass name (for the default constructor) or the complete Bet subclass name to its odds and description, on one line separated by a comma for easy parsing by your program (for the two argument constructor) — whichever makes more sense for your project.

Browser Flexible GUI

  1. Change the makeButton() method so that it that takes a String parameter instead of a Lambda (so two separate String parameters)
    • For this step, in each call to makeButton(), replace each Lambda that calls a single method with no parameters to a just the name of that method (so it is still essentially hardcoded, but now using Strings instead of Lambdas).
    • Change the call to setOnAction(), within the makeButton() method, to make your own Lambda (instead of the one previously passed in) that uses the given string parameter to get a Method object and then call its invoke() method (and catches its many exceptions).
  2. Change the makeButton() method so that it that only takes one String parameter (i.e., no extra parameter telling it what action to perform)
    • For this step create a separate properties file that maps the button's key name (the same one used in the original properties file) to the name of the method to invoke when it is pressed.
    • Create a second ResourceBundle instance variable and, in the BrowserView class constructor, read the new properties file so that the method name to call is not hardcoded in your BrowserView class.
    • After loading the both properties files, you should be able to use the one given string to look up both the text for the button to display (in the original resource file) and the action for the button to perform (in the new resource file). Again, this reduces the assumptions and what is hardcoded in your program.
  3. Create a data structure to represent the buttons in the Browser's panels
    • For this step create a List<List<String>> instance variable that holds hardcoded values for the buttons' key names used in the properties files, with a separate List for each row of buttons (i.e., the first list will hold the button keys "BackCommand", "NextCommand", "HomeCommand", and "GoCommand" and the second list will hold the button keys "AddFavoriteCommand" and "SetHomeCommand").
    • Change the calls to the methods makePreferencesPanel() and makeNavigationPanel() in the BrowserView class constructor to take a List<String>.
    • Change the code in each method to use a loop to create the buttons from the given list rather than hardcoding their order and number.
    • Note, in this example, you will need to change the order of the second panel so the non-button components are explicitly created at the end for both panels (which is also why two separate makeXXXPanel() methods are still needed). But it should not be too big a step to see how to make a more comprehensive data structure that allows you the flexibility to create more than just buttons.

Note, it is typically likely better overall design to use the Command pattern, which uses an abstraction (substitutable subclasses) rather than methods, because that is more flexible and recognizable than this simple example. But in either case, reflection can be used to make it data driven rather than hardcoded.

Your Projects

If you have completed all of the practice steps, try updating some of your own code with some of these features.

Submission

At the end of class, use Gitlab's Merge Request to submit your pair's refactored code to the lab's organization repository. Make sure the NetIDs of everyone in the group are in the title of your Merge Request.