Lecture 09: Composing Behaviors

Ambient Temperature: 74.8oF

Concepts

  1. Practical demonstration of dynamic classloading in action – the JVM only loads the bytecode of a class into memory when the class is used by code that is running. When a hitherto-unknown class is requested by the JVM, it goes and tries to find the class, and loads the class if it succeeded in finding the class. The JVM does not care if the code was present, was compiled, or even existed prior to its attempt to load the class.
  2. Demo of Homework 3: Ability to select two ball behaviors (such as “Curve” and “Color”(-changing)) and add a ball that exhibits both behaviors.
  3. Dr. Wong will never have lush, curly hair. He blames his parents.
  4. The downfall of inheritance-based ballworlds: You can only add behaviors by extending an existing behavior into a new one. If both CurveBall and ColorChangeBall extend ABall, you cannot produce a ball that posesses both of those behaviors with inheritance. You could produce a “CurvingColorChangeBall” that extends one or the other, but you would have to copy/paste code from one of them, because you cannot inherit from multiple classes. Additionally, inheritance is a compile-time feature that cannot be changed while code is running.
  5. Composition
    1. “Behavior as an entity:” encapsulate ball behaviors (such as changing color, or curving) as Java Objects to allow for potentially infinite and arbitrary combinations of behaviors.
    2. IUpdateStrategy objects that take in an IBallContext Object representing the state of a ball (location, velocity, color, etc), and make some changes to it.
    3. When updating, a ball will ask each of its IUpdateStrategies to operate on its IBallContext object. When every IUpdateStrategy has finished, the ball will use the data contained in the IBallContext Object to paint. In this way, any number of arbitrary behaviors can be exhibited by a ball and those behaviors can be assigned at compile-time and/or at run-time.

Quotes

  1. (Shouting like Christian Bale’s Batman) “But we shall use our powers for good!” – Dr. Wong

References

  1. Lecture webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec09/
  2. Homework 3 Demo: http://www.clear.rice.edu/comp310/f12/demos/ballworld_compose/index.html