COMP 310
Spring 2018
|
HW03:
Composition-based Ballworld
|
 |
Deprecated Web Page! This content has either been moved to Canvas or has been removed from the course. Please inform the staff right away about any links that led to this page.
Assignment Instructions
Replicate the
composition-based Ballworld demo.
- At least 5 strategies, not including the switcher or composite
strategies.
- "Straight" strategy does NOT qualify as one of the 5 strategies!
- You are not required to replicate the strategies in the demo.
Use your creativity to make your own!
- All buttons and text fields should have tool tips.
- Should support arbitrary combinations of update strategies,
including combining with previous combinations.
- Other than concrete strategy classes, code should be completely
independent of any concrete strategy behaviors except for default values
in the event of an error. This also technically exempts
SwitcherStrategy and
MultiStrategy because neither has concrete behaviors--though it
should be pointed out that the assignment can be completed without
defining either class!
- Students need not replicate the component layout exactly, but it
must be in an intuitive, convenient layout, i.e. not everything in a
line.
- Create UML diagrams. To keep the diagrams manageable, it
is recommended that, at least, separate diagrams for the MVC and
strategy structures be created.
Recommended Development Process:
- If you haven't done so already, branch your HW02 codebase to a new trunk
at the top level of your repository called "HW03".
- Finish the work started in lab to convert your codebase to a proper MVC
architecture. Make sure your system works
completely at this stage!
- Modify your ABall class to be concrete
(change its name!) and take an IUpdateStrategy
in its constructor.
- Implement a StraightStrategy.
- Test your code by temporarily commenting out your load ball code and
hard-coding it to load a ball with a
StraightStrategy. You should have bouncing straight
balls.
- Modify your dynamic class loading code so that it will instantiate and
return a strategy.
- Modify your load ball code to instantiate and load a ball with a given
strategy object.
- Implement at least one other strategy, a simple one.
- Test that you can type in a strategy name and it will create a ball with
that strategy.
- Modify your GUI so that it has the drop lists and additional buttons.
- Add the supplied IStrategyFac utility
methods to your model.
- Implement the "Add to Lists" and "Make
Ball" buttons by adding their event listeners and connecting them to
appropriate (perhaps new) methods on your adapters.
Implement those adapter methods by calling the supplied utility methods on
the model.
- Test that you can add and create balls with your existing strategies.
- Implement the MultiStrategy.
- Implement the "Combine!" button
functionality.
- Test that you can arbitrarily combine strategies in your balls.
- Implement the Switcher ball buttons and
their behaviors on the model. How many
SwitcherStrategy instances are ever in existence?
- Enjoy!
YOUR HW03 SVN REPOSITORY MUST BE AT THE ROOT OF YOUR SVN FOLDERS AND
BE CALLED "HW03"!
Composition-based Ballworld considerations:
GUI:
-
JComboBox is a generic
parameterized class, so you will need to declare it like this:
JComboBox<TDropListItem> where
TDropListItem (you can use whatever name you
prefer instead) is the type of entity stored in the
JComboBox. You will need to thus parameterize your GUI
class as MyGUI<TDropListItem>.
The Controller will use IStrategyFac as the
generic type when constructing the GUI because that is the type of item to
be stored in the JComboBox.
-
JComboBox.addItem(x) will add the
item x to the end of the list while
JComboBox.insertAt(x, 0) will add the item
x to the beginning of the list.
-
Be sure to retrieve the selected item (the factory!), not
the selected index or text.
-
Tool-tips: Use the
setToolTipText method to set the pop-up tool tip (or set property in
Jigloo GUI editor).
-
Be sure to always check if the text field's text is
null before proceeding.
-
Be sure to always check if the return value from a lambda
command is null, which means that an error
occurred.
-
Demo control panel layout is 3 panels and a button in a
Flow layout. Each sub-panel is a Grid layout with 1 column and 2
or 4 rows. Students need not replicate this component
layout exactly, but it should be something reasonable, i.e. not everything
in a row.
Model:
-
You will need to modify the dynamic class loading code to
handle strategies. Can you make the loader more generic so it
can be used in more situations without modification? Please
see the web page in the Java Resources on
dynamic class loading.
-
Since the IStrategyFac.make()
method is assumed to return a valid IUpdateStrategy,
then if an error occurs you need to return some valid strategy, e.g. a
StraightStrategy. Think very
carefully about where this correction would take place. Here's
really cute thing to use:
/**
* A factory for a beeping error strategy that beeps the speaker every 25 updates.
* Either use the _errorStrategyFac variable directly if you need a factory that makes an error strategy,
* or call _errorStrategyFac.make() to create an instance of a beeping error strategy.
*/
private IStrategyFac _errorStrategyFac = new IStrategyFac(){
@Override
/**
* Make the beeping error strategy
* @return An instance of a beeping error strategy
*/
public IUpdateStrategy make() {
return new IUpdateStrategy() {
private int count = 0; // update counter
@Override
/**
* Beep the speaker every 25 updates
*/
public void updateState(Ball context) {
if(25 < count++){
java.awt.Toolkit.getDefaultToolkit().beep();
count = 0;
}
}
};
}
};
-
If you use a utility method like"fixName"
to save typing by the user, be sure to reconstruct the "fully qualified"
class name, which includes the package name.
For those people who have way too much time on their hands and want to
try something really nuts, try making
this.
Warning: This application is quite challenging, though it does technically only use
techniques you've already seen. Talk to the instructor for guidance
if you want to tackle this project.
Grading Criteria
- Replicating the demo's behavior
- Can type in either full or abbreviated strategy class name into text
field (not required to support both full and abbreviated class names). - 5
- "Add" button adds strategy to both lists - 5
- "Make" button will create ball with selected strategy. - 5
- "Combine" button will add a strategy (factory, actually) to the
lists that is the combination of two selected strategies. - 10
- "Make Switcher" makes a ball with a Switcher strategy (initial
strategy unspecified here). - 5
- "Switch" button will change all current Switcher balls to the
selected behavior -5
- At least 5 different types of strategies can be made and combined.''
- 10
- Discretionary pts - 5
- Architecture
- UML diagrams. - 5
- Ball class is fully concrete. Only 1 ball class.
- 10
- All variant updating delegated to a strategy - 10
- Strategy factories sent to/from the GUI. - 5
- Combination of two strategies created through the use of a generic
strategy that holds and executes two arbitrary strategies. -
10
- Swtcher strategy is implemented as a wrapper over a single arbitrary
strategy with a "setStrategy" accessor-type method to change the
strategy. -5
- Discretionary pts - 5
© 2018 by Stephen Wong