ballworld.model
Class BallModel

java.lang.Object
  extended by ballworld.model.BallModel

public class BallModel
extends java.lang.Object

The model that represents the balls and their associated control systems. The balls are held by a Dispatcher which is used for all communications to the balls. Uses a Timer object to update the model and the view at a regular time slice. This will give the effect of animation.�

Author:
swong

Field Summary
private  Dispatcher _dispatcher
          The dispatcher that updates all the balls.
private  IPaintStrategyFac _errorPaintStrategyFac
           
private  IUpdateStrategyFac _errorUpdateStrategyFac
          A factory for a beeping error strategy that beeps the speaker every 25 updates.
private  int _MaxRadius
          The maximum radius for a new ball
private  int _MaxSpeed
          The maximum speed for a new ball.
private  java.awt.Rectangle _MaxVel
          The bounds for the velocity of a new ball
private  int _MinRadius
          The minimum radius for a new ball
private  SwitcherStrategy _switcher
          The one switcher strategy instance in the system.
private  javax.swing.Timer _timer
          The timer that controls when the balls are periodically updated.
private  int _timeSlice
          The time interval between timer ticks, in milliseconds
private  javax.swing.Timer _updateTimer
           
private  int _updateTimeSlice
           
private  IRandomizer rand
          Randomizer utility object, used for random values for new balls
private  IViewControlAdapter viewCtrlAdpt
          Adapter to the view for control tasks.
private  IViewUpdateAdapter viewUpdateAdpt
          Adapter to the view for updating tasks
 
Constructor Summary
BallModel(IViewControlAdapter viewCtrlAdpt, IViewUpdateAdapter viewUpdateAdpt)
          Constructor for the model
 
Method Summary
 void clearBalls()
          Clear all the ball from the system.
 IUpdateStrategyFac combineStrategyFacs(IUpdateStrategyFac stratFac1, IUpdateStrategyFac stratFac2)
          Returns an IStrategyFac that can instantiate a MultiStrategy with the two strategies made by the two given IUpdateStrategyFac objects.
private  java.lang.String fixName(java.lang.String folder, java.lang.Object classname, java.lang.String append)
          Fixes abbreviated, shortened names by adding the "ballworld.model.strategy." package part of the name and adding a "Strategy" suffix to the name.
 SwitcherStrategy getSwitcherStrategy()
          Getter for the one switcher strategy instance
 void loadBall(IUpdateStrategy updateStrategy, IPaintStrategy paintStrategy)
          Load a ball into the system, where the ball uses the given update strategy instance.
 IPaintStrategy loadPaintStrategy(java.lang.String className)
          Uses dynamic class loading to load and instantiate an IUpdateStrategy implementation, given its class name.
 IUpdateStrategy loadUpdateStrategy(java.lang.String className)
          Uses dynamic class loading to load and instantiate an IUpdateStrategy implementation, given its class name.
 IPaintStrategyFac makePaintStrategyFac(java.lang.String classname)
          Returns an IPaintStrategyFac that can instantiate the strategy specified by classname.
 IUpdateStrategyFac makeUpdateStrategyFac(java.lang.String classname)
          Returns an IUpdateStrategyFac that can instantiate the strategy specified by classname.
 void start()
          Start the model, in particular, start the timer.
 void switchSwitcherStrategy(IUpdateStrategy strat)
          Change the switcher strategy's decoree to the supplied strategy
 void update(java.lang.Object g)
          Update all the balls in the system, passing the given input parameter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_switcher

private SwitcherStrategy _switcher
The one switcher strategy instance in the system. Allows all balls made with this strategy to be controlled at once.


rand

private IRandomizer rand
Randomizer utility object, used for random values for new balls


_MaxRadius

private int _MaxRadius
The maximum radius for a new ball


_MinRadius

private int _MinRadius
The minimum radius for a new ball


_MaxSpeed

private int _MaxSpeed
The maximum speed for a new ball.


_MaxVel

private java.awt.Rectangle _MaxVel
The bounds for the velocity of a new ball


_dispatcher

private Dispatcher _dispatcher
The dispatcher that updates all the balls.


_timeSlice

private int _timeSlice
The time interval between timer ticks, in milliseconds


_timer

private javax.swing.Timer _timer
The timer that controls when the balls are periodically updated. Every _timeSlice milliseconds, it loads a timer event onto the GUI event thread. This insures that all processes happen on the GUI event thread, minimizing cross-thread invocation problems.


viewCtrlAdpt

private IViewControlAdapter viewCtrlAdpt
Adapter to the view for control tasks.


viewUpdateAdpt

private IViewUpdateAdapter viewUpdateAdpt
Adapter to the view for updating tasks


_updateTimeSlice

private int _updateTimeSlice

_updateTimer

private javax.swing.Timer _updateTimer

_errorUpdateStrategyFac

private IUpdateStrategyFac _errorUpdateStrategyFac
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.


_errorPaintStrategyFac

private IPaintStrategyFac _errorPaintStrategyFac
Constructor Detail

BallModel

public BallModel(IViewControlAdapter viewCtrlAdpt,
                 IViewUpdateAdapter viewUpdateAdpt)
Constructor for the model

Parameters:
viewCtrlAdpt - The view adapter for control tasks
viewUpdateAdpt - The view adapter for updating tasks
Method Detail

loadBall

public void loadBall(IUpdateStrategy updateStrategy,
                     IPaintStrategy paintStrategy)
Load a ball into the system, where the ball uses the given update strategy instance.

Parameters:
updateStrategy - An IUpdateStrategy for the new ball to use.
paintStrategy - An IPaintStrategy for the new ball to use.

start

public void start()
Start the model, in particular, start the timer.


clearBalls

public void clearBalls()
Clear all the ball from the system.


update

public void update(java.lang.Object g)
Update all the balls in the system, passing the given input parameter.

Parameters:
g - An input parameter to give to each ball's update method.

fixName

private java.lang.String fixName(java.lang.String folder,
                                 java.lang.Object classname,
                                 java.lang.String append)
Fixes abbreviated, shortened names by adding the "ballworld.model.strategy." package part of the name and adding a "Strategy" suffix to the name.

Parameters:
folder - TODO
classname - The abbreviated, shortened name
append - TODO
Returns:
The fully qualified class name.

loadUpdateStrategy

public IUpdateStrategy loadUpdateStrategy(java.lang.String className)
Uses dynamic class loading to load and instantiate an IUpdateStrategy implementation, given its class name. If the classname is invalid, a beeping error strategy instance is returned.

Parameters:
className - The fully qualified classname of a strategy
Returns:
An IUpdateStrategy instance

loadPaintStrategy

public IPaintStrategy loadPaintStrategy(java.lang.String className)
Uses dynamic class loading to load and instantiate an IUpdateStrategy implementation, given its class name. If the classname is invalid, a beeping error strategy instance is returned.

Parameters:
className - The fully qualified classname of a strategy
Returns:
An IUpdateStrategy instance

makeUpdateStrategyFac

public IUpdateStrategyFac makeUpdateStrategyFac(java.lang.String classname)
Returns an IUpdateStrategyFac that can instantiate the strategy specified by classname. Returns a factory for a beeping error strategy if classname is null. The toString() of the returned factory is the classname.

Parameters:
classname - Shortened name of desired strategy
Returns:
A factory to make that strategy

combineStrategyFacs

public IUpdateStrategyFac combineStrategyFacs(IUpdateStrategyFac stratFac1,
                                              IUpdateStrategyFac stratFac2)
Returns an IStrategyFac that can instantiate a MultiStrategy with the two strategies made by the two given IUpdateStrategyFac objects. Returns null if either supplied factory is null. The toString() of the returned factory is the toString()'s of the two given factories, concatenated with "-". If either factory is null, then a factory for a beeping error strategy is returned.

Parameters:
stratFac1 - An IUpdateStrategyFac for a strategy
stratFac2 - An IUpdateStrategyFac for a strategy
Returns:
An IUpdateStrategyFac for the composition of the two strategies

getSwitcherStrategy

public SwitcherStrategy getSwitcherStrategy()
Getter for the one switcher strategy instance

Returns:
the SwitcherStrategy instance

switchSwitcherStrategy

public void switchSwitcherStrategy(IUpdateStrategy strat)
Change the switcher strategy's decoree to the supplied strategy

Parameters:
strat - the new decoree for the switcher

makePaintStrategyFac

public IPaintStrategyFac makePaintStrategyFac(java.lang.String classname)
Returns an IPaintStrategyFac that can instantiate the strategy specified by classname. Returns a factory for a beeping error strategy if classname is null. The toString() of the returned factory is the classname.

Parameters:
classname - Shortened name of desired strategy
Returns:
A factory to make that strategy