Generics Proxy and The Compile Time Type Checking










- Slides: 10

Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class

Two Camps C++ • Strong Type Checking • Ensured at compile time that you never called a method on an object that didn’t support it • Focused on efficient compilation • *Very* complex type system, including “templates” Smalltalk • Variables do not have types • Extremely easy to call a method on an object that didn’t support it • Not super efficient (but not terrible) • Pretty simple type system (at least on the surface)…also quite flexible

Why Does Type Checking Inhibit Flexibility? • Example: The Proxy pattern • Scenario: You’ve written a very cool application that visualizes large data sets (represented as objects that implement the Data. Source interface). Your visualizer takes a list of Data. Source objects, and lets you choose the data to visualize. Bad news: some of these data sets are really huge. So to create the Data. Source objects really slows down your system, which is bad because most of the Data. Sources won’t actually get used. What you want is to only load the data if it’s really necessary (when the get. Data is called). But you’d like to do this without adding complexity to the Visualizer object OR to the Gigantic. Data. Source object that actually loads the data. Hint: It’s sort of like a Decorator

Solution: A Proxy • • • Make a new class Gigantic. Data. Source. Proxy that has a filename and has an instance variable of type Gigantic. Data. Source Initially, the variable is null But then, when somebody calls get. Data on the proxy, the proxy actually creates a Gigantic. Data. Source and loads the data, then forwards the results Gigantic. Data. Source. Proxy data = new Gigantic. Data. Source. Proxy(“my. Data. dat”); //does not really load the file //. . a lot of other code… //Now the proxy creates a new Gigantic. Data. Source and passes the // get data call to it data. get. Data(77); //subsequent calls are just forwarded to the proxy’s internal // Gigantic. Data. Source object data. get. Data(12);

Proxy Objects Exist in Both Strongly Typed Object Oriented Languages and Weekly Typed Ones • Used to delay initialization or other expensive operations • Used to hide “remote” objects that are actually running on distant computers • Sometimes even protect certain functions from being run (although I tend to think this particular use blurs the line between proxy objects and decorators)

Can We Build a Generic Proxy Object? • • • That is, initially does nothing but then creates the object when a method is called on it In C++ or Java…no because that “generic” proxy would not implement the appropriate interface In a language with untyped variables like Smalltalk, Ruby or Python…yes. They usually even have a special method that you can override when someone calls a method you wouldn’t normally support public example. Method. With. No. Types(my. Var) { my. Var. do. Function(); //my. Var could be any type //if it is not a supported function, an //exception would be thrown return my. Var. do. Other. Thing(); }

Generics • Deal with some of the problems of Strong Typed Languages • Prior to Java 1. 5, all collections in Java used to take and return objects public void some. Function(Array. List list) { Specific. Class var = (Specific. Class) list. get(0); var. call. Specific. Method(); } • Combines the verbosity of Java with the poor type checking of Smalltalk • So generics were added so you can say Array. List<String> (or whatever)

Imagine We Decided to Make a Specific. Sprite. Group<Ninja. Sprite> n = new Specific. Sprite. Group<Ninja. Sprite>(); • Don’t make it inherit from Sprite. Group at least not yet…make it have an array list of sprites and a method add. Sprite that adds a sprite of the appropriate type

A Few Complications • get. Active…goes through the list of sprites and determines which one is active • Add. All…we would like to be able to add a list of Sprites

Write • remove. Inactive. Sprites • (if you have time) sort…which should take a comparator