Lecture 10: Faking out the GUI with Factories

Ambient temperature: 74.9oF

Concepts

  1.  We have learned two of the Four Pillars of Abstraction from the Lecture Webpage. Go read them.
    1. Abstract Structure was demonstrated with various types of Balls inheriting from ABall,and us writing code only for ABalls.
    2. Abstract Behavior was demonstrated by the “Strategies” that we will use in Homework 3 to make ball behavior highly configurable.
  2. Today: The third pillar, Abstract Construction via the Factory Design Pattern. Factories implement an IFactory method that has a public ISomething makeSomething(…); method. Any class anywhere can use an IFactory to make objects that implement the ISomething interface, and it doesn’t care at all how they are constructed, or what they are constructed with.
  3. Consider an IBallFactory that produces IBall instances. Concrete IBallFactories might include a StraightBallFactory, CurveBallFactory, and BreathingBallFactory, but your controller would only ever see them as IBallFactory objects. Any specifics of constructing a particular ball (perhaps BreathingBall needs to know how much it should breathe, or how fast) are handled by the individual factory instance, not by the controller.
  4. Dr. Wong explains how JComboBox works: You can store Objects in it. Java will use their toString() method to display their labels. This can save you lots of time by allowing users to select instantiated Java objects (which then get fed into actionPerformed) directly.
  5. Dr. Wong shows off his Strategy implementation:  His controller has a public IStrategy makeStrategy(String className) and public IStrategyFactory combineStrategy(IStrategyFactory one, IStrategyFactory two) methods. makeStrategy takes in a class name in string format and emit an IStrategy based on that class name. One of his strategies is a MultiStrategy that will take in two strategies and exhibit both behaviors. Chaining MultiStrategies leads to Color-Breathe-Straight-Curve balls, etc. MultiStrategy is used in combineStrategy to return a factory that uses both strategies.
  6. Dr. Wong’s factory- and strategy- generation methods do so by creating closures in the … new IStrategyFactory() { … public IStrategy make() { … }  … } manner.

Quotes

  1. “A factory that makes tress, and trees and trees and trees, of factories!” – Dr. Wong

Resources

  1. Lecture Webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec10/