September 24, 2012
Ambient temperature: 73.2oF
Concepts
- Dr. Wong is in a food coma from BBs Cafe, probably because today is his birthday.
- Never do ball.get_update_strategy().update();. That is an encapsulation violation! Instead, use ball.update_state(), and inside update_state(), the ball itself may ask its update strategy to update(). Or, the ball may implement its updating in an entirely different manner, but it doesn’t matter because encapsulation is intact.
- Your balls should have some method (Observer’s update() method could be a good place, if you’re using Java’s Observer-Observable framework) in which they will execute all of their commands.
- Anonymous inner classes are important because they create Closures of their current environment, allowing you to have access to the Graphics object and any other Adapters you might want, without having to pass any of them as parameters to a method or a constructor.
- Commands are introduced in-depth, with sample code.
Quotes
- “And those fields [captured in a closure] never disappear, unlike your hometowns that get turned into suburban wastelands or plains of townhouses.” – Dr. Wong
Resources
- Lecture 14 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec14/
Comments Off on Lecture 14: Command Dispatching |
Lectures | Tagged: closure, command |
Permalink
Posted by amw2
September 21, 2012
Ambient Temperature: 73.2oF
Concepts
- Dr. Wong explains how proper MVC design facilitates launching ballworld as a Java Applet, by showing how the demo pages’ code was set up and invoked.
- Dr. Wong demonstrates “Infection Balls,” “Eating Balls,” and “Bouncing Balls” as part of the command-driven ballworld demo.
- Some pool halls have cue balls that are smaller than normal.
- Strategies are used with a specific purpose in the mind (paint, update, etc) of the object that uses them. Commands are used without any specific purpose in the mind of the object that uses them. IPaintStrategy may have a paint() method, IUpdateStrategy will have an update() method, etc. When these methods are called in code, the object calling them will be calling .paint() because it wants to paint. An ICommand will have only an apply() method, and when a command is run in code, the object running it won’t know why, or what the command does – only that it should run the command.
Resources
- Oracle’s Java Applet Tutorial: http://docs.oracle.com/javase/tutorial/deployment/applet/
- Lecture 13 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec13/
Comments Off on Lecture 13: Command-Driven Dispatching |
Lectures | Tagged: applet, command, MVC |
Permalink
Posted by amw2
September 19, 2012
Ambient temperature: 73.2oF
Concepts
- Systems with prototypes do not care where the prototypes come from, so long as they have one. Have a “IPrototypeShapeFactory” hierarchy that can produce the prototype (size ~ 1.0 units) of any kind of shape, when you need it.
- Polygon and Image factories cannot store a unit-sized prototype; they will instead store whatever size is available. When asked for a prototype, they will apply a transformation to create a reduced-size version that is unit-sized, then return that new version instead of their “internal” prototype.
- The Decorator Design Pattern allows you to extend or customize the behavior of one instance of an Object, versus the entire class of Objects. If you wanted exactly one specific ball to always be a color that the user chooses, for example, but didn’t want to write “AlwaysRedStrategy, AlwaysBlueStrategy, AlwaysGreenStrategy…” implementations, you could create a Decorator class that wraps a Strategy instance, and forces that one instance to always paint a particular way.
- The Template Design Pattern features an Interface, implemented by an Abstract Class. That Abstract Class implements the default behavior of each of the Interface’s methods. Wherever possible, it breaks this default behavior up into a series of other method calls in the Abstract Class (NOT in the interface). These methods are an example of “extensibility hooks.” To customize the behavior of a sublcass, programmers then override just the hook methods that deal with the behavior they want to modify. For example, instead of overriding and completely re-implementing paint(Graphics _g) { … } just to change the color, a Template-based Abstract Class might define the protected abstract void prepare_color(Graphics _g, IUpdateContext _c) method, which you would override instead. That way, you do not have to re-write code for painting, transforming, or any other behavior. This is another way of “separating the variant from the invariant.”
- Variable Input Argument Lists were introduced.
- The JVM does not implement them; they are compile-time syntactic sugar, therefore
- Java Reflection does not (and can not) support calling vararg methods.
- You cannot pass a single object that is an array.
- They are a convenience feature only; no new capabilities are granted, so
- Just don’t use them.
Resources
- Lecture 12 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec12/
- Lecture 13 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec13/
Comments Off on Lecture 12 Part 2: Factories and Superclasses as Service Providers |
Lectures | Tagged: decorator, factory, prototype, template, varargs |
Permalink
Posted by amw2
September 17, 2012
Ambient temperature: 75.1oF
Concepts
- Fishworld uses “Affine Transformations” to make its fish turn to face the correct direction.
- Everything takes place relative to the origin, and order of operations is important.
- This is why CS majors must take math courses: Their concepts become freakishly germane in matters of computer graphics.
- Dr. Wong advises us to always do scaling and rotation first, before transformation.
- Any graphical element has one “prototype,” which is its state before any transformations have been applied to it.
- The size of a prototype is “one unit.” The size of all subsequent instances of the prototype can then be described solely in terms of the scale factor (say “3 times”), without having to worry about multiplication anywhere.
- Dr. Wong shows off his class hierarchy for Fishworld (from Lecture 12 webpage) – this would also serve the needs of Homework 3’s ballworld, though it would be overkill.
- Math.atan2(double _y, double _x) will return a value that is rotated correctly into a quadrant; Math.atan(double _ratio) will only return a value in the first or fourth quadrant. If you want fish to rotate to properly face in the direction of its velocity, atan2 is your friend.
- The Lecture 12 webpage is full of provided code and detailed design explanation for Homework 4.
Resources
- Lecture 11 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec11/
- Lecture 12 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec12/
Comments Off on Lecture 11 Part 2 & Lecture 12: Transforming how we Paint & Using Superclasses as Service Providers |
Lectures | Tagged: fishworld, hw04, prototype, service, transformation |
Permalink
Posted by amw2
September 14, 2012
Ambient temperature: 74.8oF
Concepts
- If you use reflection to instantiate a class, you must check that the constructor takes the correct amount of arguments, and that they are of the type your code expects them to be. Dr. Wong discovered that in Java 7, the behavior of Java Reflection’s Class.forName( class_name ).getConstructors() method changed, depending on whether the code runs in a 64-bit or 32-bit JVM. The 64-bit JVM returns the constructors in the order they were defined in the class file, which was the original behavior. The 32-bit JVM returns them in a different order. Dr. Wong did not do a thorough check to determine if there was a pattern to the 32-bit ordering.
- Dr. Wong went over code and advice for Homework 3 in detail.
- The Lecture 10 page on the course website contains full code for most of the Interfaces used in Dr. Wong’s implementation of Factories in Homework 3. These could be a good starting point and/or reference.
- JComboBox<T> – only ever use a generic JComboBox, and never use box.getSelectedItem(); – use box.getItemAt( box.getSeletectedIndex() ); instead. The latter is generically-typed and will help you avoid errors.
- When you choose a name for a generic class, choose something descriptive.
- You will be choosing generic types for your classes, because you will use generics to make sure that the Model, View, and Controller are all using the same type of adapter. Furthermore, doing so will prevent a Model, View, or Controller from being used with another Model, View, or Controller that uses a different type of adapter. Type-safety is good!
- Dr. Wong demonstrated Homework 4: FishWorld.
Quotes
- “It’s whatever part of the shark I want it to be.” – Dr. Wong
Resources
- Lecture webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec11/
Comments Off on Lecture 11: Factories & Transforming How We Paint |
Lectures | Tagged: bug, generics, hw03 |
Permalink
Posted by amw2
September 12, 2012
Ambient temperature: 74.9oF
Concepts
- We have learned two of the Four Pillars of Abstraction from the Lecture Webpage. Go read them.
- Abstract Structure was demonstrated with various types of Balls inheriting from ABall,and us writing code only for ABalls.
- Abstract Behavior was demonstrated by the “Strategies” that we will use in Homework 3 to make ball behavior highly configurable.
- 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.
- 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.
- 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.
- 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.
- Dr. Wong’s factory- and strategy- generation methods do so by creating closures in the … new IStrategyFactory() { … public IStrategy make() { … } … } manner.
Quotes
- “A factory that makes tress, and trees and trees and trees, of factories!” – Dr. Wong
Resources
- Lecture Webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec10/
Comments Off on Lecture 10: Faking out the GUI with Factories |
Lectures | Tagged: factory, strategy |
Permalink
Posted by amw2
September 10, 2012
Ambient Temperature: 74.8oF
Concepts
- 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.
- Demo of Homework 3: Ability to select two ball behaviors (such as “Curve” and “Color”(-changing)) and add a ball that exhibits both behaviors.
- Dr. Wong will never have lush, curly hair. He blames his parents.
- 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.
- Composition
- “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.
- IUpdateStrategy objects that take in an IBallContext Object representing the state of a ball (location, velocity, color, etc), and make some changes to it.
- 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
- (Shouting like Christian Bale’s Batman) “But we shall use our powers for good!” – Dr. Wong
References
- Lecture webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec09/
- Homework 3 Demo: http://www.clear.rice.edu/comp310/f12/demos/ballworld_compose/index.html
Comments Off on Lecture 09: Composing Behaviors |
Lectures | Tagged: composition, strategy |
Permalink
Posted by amw2
September 7, 2012
Ambient Temperature: 73.5oF
Concepts
- Pre-Game Show:
- Join the Hacker Games with your @rice.edu e-mail for a not-quite-normal computer contest.
- “The world is bigger than Microsoft, Amazon, and Google,” so consider interning or working for other companies, too.
- When the “is-a” relationship of inheritance truly exists, it is acceptable to have a concrete sub-class of a concrete class. However, do not forget to use abstract classes where appropriate, not just at the top of the class hierarchy, to factor out common code.
- Feel free to create ball types of your own imagination in Homework 2.
- Two approaches to (Model, View, Controller) design for Homework 2:
- The Model has a Timer which will use a Dispatcher to tell all of the Balls to update their positions. Every time the Model updates, it will use a Dispatcher provided to it by the Controller to tell the View the new information that the View should display.
- The Controller has a Timer which will tell the Model to update its state, and then tell the View to update what it shows. The Model will use its Dispatcher to tell all of its Balls to update. The View will use an Adapter provided by the Controller to get the information it needs from the Model.
- In all cases, when Buttons in the View are clicked, they will use Adapters provided by the Controller to communicate the necessary changes to the Model.
Quotes
- “I’ve been told: be careful about watching BallWorld after imbibing certain substances.” – Dr. Wong
- “You don’t put a fly method on a fish.” – Dr. Wong
Resources
- Dem Dry Bones: http://www.brownielocks.com/dembones.html
- Lecture webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec08/
Comments Off on Lecture 08: Connecting the Pieces |
Lectures | Tagged: hw02, MVC |
Permalink
Posted by amw2
September 5, 2012
Ambient Temperature: 73.8oF
Concepts
- Separating the Invariant from the Variant.
- “Invariant” properties (and methods) live in the (abstract) superclass. Anything you put in AShape must satisfy the statement “All shapes everywhere forever have a ________.”
- Things that can be invariant include (but are not necessarily limited to) a method’s presence, a field’s presence, a method’s implementation, and a field’s value.
- Write programs targeting the Invariant (‘s interface), so that your programs can work with any, all, and arbitrary variants, including ones you may not have ever dreamed of.
- Objects have behavior, which you experience when you interact with them, and a construction, which is how they’re built. When objects interact with each other in Java, they should only care about the behavior, not the underlying construction or data. Therefore, any object whose behaviors include “all of the things a duck can do” (for example) can be treated as a duck (for example) by other Java objects. Moral: Use Interfaces.
- Concrete methods are rarely overridden, because that’s not what overriding is really for: If a child class is fundamentally changing the behavior, you’re probably doing it wrong.
- Drive-by incidental introduction to “hey you can use wildcards in Java Generics.” Hand-waving ensued.
- Observer-Observable design pattern will be necessary in Homework 2.
- There are 32 different types of bridges, if you don’t count sub-types. Source: http://en.wikipedia.org/wiki/List_of_bridge_types
Quotes
- “(paraphrased) What if I took some fluid and put it inside my duck robot so that when you stuck a needle in and drew it out, it contained DNA that perfectly matched your duck? Is that a duck?” – Dr. Wong
- “Why isn’t there a bridge that swoops through the world and expresses the angst of society?” – Dr. Wong
Resources
- Lecture Webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec07/
- Hipster Flipbook on YouToube: http://www.youtube.com/watch?v=4B3vNE459CQ
- Wildcards in Java Generics: http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html
1 Comment |
Lectures | Tagged: invariant, observer-obsevable, variant |
Permalink
Posted by amw2