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.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

GCSE Computing Lesson 5.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Written by: Dr. JJ Shepherd
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Interface COMP T1.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Creating Shareable Models By: Eric Hutton CSDMS - Community Surface Dynamics Modeling System (pronounced ˈ s ɪ stəms) Image by Flickr user Let There Be.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
CSE305 Programming Languages Notes are based primarily on Sebesta text. Quotes are from Sebesta text (7 th edition), unless otherwise noted. Sources other.
Protection of Agent Teamwork By Jeremy Hall. Agent Teamwork Overview ● Mobile agent framework  AgentTeamwork 2 is a mobile-agent based middleware system.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Source Code Basics. Code For a computer to execute instructions, it needs to be in binary Each instruction is given a number Known as “operation code”
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
1 CSC 222: Object-Oriented Programming Spring 2013 Course goals:  To know and use basic Java programming constructs for object- oriented problem solving.
Objects to Protect Private Details: Memento and Iterator Snarf the iterator code for today’s class.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Model View Controller A Pattern that Many People Think They Understand, But Has A Couple Meanings.
1 Object Oriented Programming Lecture XI An abstract function plotter, using the Template and the Strategy design patterns.
Storing Data. A Note About Creating Games Why do you want to store data? 1.Data files 2.Configuration files.
Observer Please Snarf the Code for Today’s Class..
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
M1G Introduction to Programming 2 5. Completing the program.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Written by: Dr. JJ Shepherd
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Proxy. PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access.
Introduction to Exceptions in Java CS201, SW Development Methods.
CSCE 240 – Intro to Software Engineering Lecture 3.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
Reference: Object Oriented Design and Programming (Horstmann)
Interface Java 7 COMP T1.
CSC 222: Object-Oriented Programming
1.1 Reasons to study concepts of PLs
Interfaces and Inheritance
More Object-Oriented Programming
Exceptions 10-May-19.
CMSC 202 Exceptions.
Design Patterns
Presentation transcript:

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 DataSource interface). Your visualizer takes a list of DataSource objects, and lets you choose the data to visualize. Bad news: some of these data sets are really huge. So to create the DataSource objects really slows down your system, which is bad because most of the DataSources won’t actually get used. What you want is to only load the data if it’s really necessary (when the getData is called). But you’d like to do this without adding complexity to the Visualizer object OR to the GiganticDataSource object that actually loads the data. Hint: It’s sort of like a Decorator

Solution: A Proxy Make a new class GiganticDataSourceProxy that has a filename and has an instance variable of type GiganticDataSource Initially, the variable is null But then, when somebody calls getData on the proxy, the proxy actually creates a GiganticDataSource and loads the data, then forwards the results GiganticDataSourceProxy data = new GiganticDataSourceProxy(“myData.dat”); //does not really load the file //..a lot of other code… //Now the proxy creates a new GiganticDataSource and passes the // get data call to it data.getData(77); //subsequent calls are just forwarded to the proxy’s internal // GiganticDataSource object data.getData(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 exampleMethodWithNoTypes(myVar) { myVar.doFunction(); //myVar could be any type //if it is not a supported function, an //exception would be thrown return myVar.doOtherThing(); }

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 someFunction(ArrayList list) { SpecificClass var = (SpecificClass) list.get(0); var.callSpecificMethod(); } Combines the verbosity of Java with the poor type checking of Smalltalk So generics were added so you can say ArrayList (or whatever)

Imagine We Decided to Make a SpecificSpriteGroup SpecificSpriteGroup n = new SpecificSpriteGroup (); Don’t make it inherit from SpriteGroup at least not yet…make it have an array list of sprites and a method addSprite that adds a sprite of the appropriate type

A Few Complications getActive…goes through the list of sprites and determines which one is active AddAll…we would like to be able to add a list of Sprites

Write removeInactiveSprites (if you have time) sort…which should take a comparator