Eagleson’s Law: Any code of your own that you haven't looked at for 6+ months might as well have been written by someone else.
Computer Science 313 – Advanced Programming Topics
How About that Recursion Flat profile of 0.67 secs (32 total ticks): main Interpreted + native Method 3.1% Recursive02.binomialExpansion Compiled + native Method 96.9% Recursive02.binomialExpansion Flat profile of 2.61 secs (145 total ticks): main Interpreted + native Method 0.7% java.util.Vector.elementAt Compiled + native Method 85.5% Recursive02.binomialExpansion 13.8% java.util.Vector.addElement
What Happened? Why?
Simple Explanation Java’s implementation of Stack sucks
Real Explanation Java’s Stack slower than system stack Well, duh, but more important is why Stack class must work in lots of situations Includes checks to grow on each call to push Must balance size and speed demands Decent performance often; Great performance never System stack is highly specialized Explodes size to gain speed Benefits from knowing it has only one use
One is Special
There Can Be Only One! Uniqueness leads to improvements Provide universal access Eliminate unneeded checks for errors Avoid creating large number of small objects Make space-time tradeoffs with confidence Can also make correctness easy Pretty easy to find object triggering error Ensure order of actions Prevents problems over which instance in charge
Common Situation Graphic & game engines System.out System.err AbstractFactory instances FactoryMethod instances Device drivers
Singleton Pattern Ensures class has exactly 1 instance Always use same instance of class Fields can change, but instance should not Provides single access point to instance All uses of class should go through this Methods should not let this reference leak
Singleton Anti-Pattern Process or model harmful to overall process Alcohol Fueled Development Big Ball Of Mud Blowhard Jamboree Design By Committee Kill Two Birds With One Stone Singleton is harmful when a global variable Also known as public static field Can be updated & changed in many places
For Next Class Lab #5 on web/Angel & due week from Tues. Implements Decorator pattern & a factory You can choose the factory to implement Read pages 179 – 186 in the book How do we implement these Singletons? What are the myriad ways to screw them up? Which system bugs are exposed by this pattern?