generic programming amit shabtay. march 3rd, 2004 object oriented design course 2 the problem assume...
Post on 21-Dec-2015
212 views
TRANSCRIPT
![Page 1: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/1.jpg)
Generic Programming
Amit Shabtay
![Page 2: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/2.jpg)
March 3rd, 2004 Object Oriented Design Course 2
The Problem
Assume we have a nice Stack implementation.•Our stack receives only Objects
The problem: •We need different stacks for int, String and Method.
•We don’t want the problems of:Stack.add(“clearly not a method”);
…
Method m = (Method)stack.pop();
![Page 3: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/3.jpg)
March 3rd, 2004 Object Oriented Design Course 3
Solution (?)
Let’s create IntStack , StringStack and MethodStack.
All of them will rely on the original Stack as internal implementation.
Are there any problems?•A lot of code duplication
•It’s hard to add new types
![Page 4: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/4.jpg)
March 3rd, 2004 Object Oriented Design Course 4
Another Problem
We want to use a swap function between two ints.void swap(int& a, int&b) {
int temp = a; a = b; b = temp;
} What about swap(double&, double&)
and swap(Method&, Method&) ?
![Page 5: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/5.jpg)
March 3rd, 2004 Object Oriented Design Course 5
The Actual Solution
Generic programming.(The ability to have type parameters on your type)
Write the code once, and worry about the type at compile time•The code is suitable to all types
•Easy to add types later
•No code duplication
•Demands from types
![Page 6: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/6.jpg)
March 3rd, 2004 Object Oriented Design Course 6
So How Do We Do It?
swap<Class T>(T& a, T& b) {T temp = a;a = b;b = temp;
} Looks simple?
![Page 7: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/7.jpg)
March 3rd, 2004 Object Oriented Design Course 7
Java 1.4.2 vs. 1.5 and Autoboxing ArrayList list = new ArrayList(); //1.4.2
list.add(0, new Integer(42)); int total = ((Integer)list.get(0)).intValue();
ArrayList<Integer> list = new ArrayList<Integer>();//1.5 list.add(0, new Integer(42)); int total = list.get(0).intValue();
ArrayList<Integer> list = new ArrayList<Integer>(); //1.5 auto-
boxing list.add(0, 42);int total = list.get(0);
![Page 8: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/8.jpg)
March 3rd, 2004 Object Oriented Design Course 8
Uses
Containers• list
•set
•vector
•map Algorithms
•sort
•search
•copy
![Page 9: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/9.jpg)
March 3rd, 2004 Object Oriented Design Course 9
C++ Templates
The most known use of generic programming
STL – Standard Template Library•Containers
•vector, set, hash_map
•Algorithms•for_each, swap, binary_search, min, max
![Page 10: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/10.jpg)
March 3rd, 2004 Object Oriented Design Course 10
What about Java?
Until now Java had large collection set•Set, List, Map, Iterator, and more
•sort(), search(), fill(), copy(), max(), min() One major problem – the collections
are not type safe•No problem to doMap.put(“key”, “4”);
Integer i = (Integer)map.get(“key”);
![Page 11: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/11.jpg)
March 3rd, 2004 Object Oriented Design Course 11
Java Generics
Added as one of the new features of Java 1.5 (“Tiger”)
Done in the compiler only•Converts String s = vector<String>.get(3) to String s = (String)vector.get(3)
![Page 12: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/12.jpg)
March 3rd, 2004 Object Oriented Design Course 12
How to Use Generics ?
List<Integer> myIntList = new LinkedList<Integer>();
myIntList.add(new Integer(0));
Integer x = myIntList.iterator().next();
![Page 13: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/13.jpg)
March 3rd, 2004 Object Oriented Design Course 13
And What About the Collection ?
public interface List<E> {
void add(E x);
Iterator<E> iterator();
}
public interface Iterator<E> {
E next();
boolean hasNext();
}
![Page 14: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/14.jpg)
March 3rd, 2004 Object Oriented Design Course 14
Subtyping
List<String> ls = new ArrayList<String>();
List<Object> lo = ls; lo.add(new Object()); //Attempts to assign an Object to a String!
String s = ls.get(0);
//Compiler will not permit
![Page 15: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/15.jpg)
March 3rd, 2004 Object Oriented Design Course 15
Subtyping (cont.)
Foo is subtype of Bar if:•Foo extends Bar
•Foo implements Bar C is a generic container C<E> Results that C<Foo> is not subtype
of C<Bar>
![Page 16: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/16.jpg)
March 3rd, 2004 Object Oriented Design Course 16
Generic Algorithms (1)
How to print entire Collection? Do we have to use Collection<Object> ? Use wildcardvoid printCollection(Collection<?> c){ for (Object e : c) { System.out.println(e);
}
}
![Page 17: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/17.jpg)
March 3rd, 2004 Object Oriented Design Course 17
Generic Algorithms (2) What happens when we want to use
specific method?public void drawAll(List<Shape> shapes) { for (Shape s: shapes) { s.draw(this);}
} What about subtyping?
•List<Circle>
![Page 18: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/18.jpg)
March 3rd, 2004 Object Oriented Design Course 18
Generic Algorithms (3)
The solutionpublic void
drawAll(List<? extends Shape> shapes) {…} //Called bounded wildcard.
![Page 19: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/19.jpg)
March 3rd, 2004 Object Oriented Design Course 19
More About Wildcards
Collection<?> c = new ArrayList<String>();
c.add(new Object());
public void addRectangle(List<? extends Shape> shapes) {shapes.add(0, new Rectangle());
}
![Page 20: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/20.jpg)
March 3rd, 2004 Object Oriented Design Course 20
Super vs. Extends
The syntax ? super T denotes an unknown type that is a supertype of T.
It is the dual of the ? extends T to denote an unknown type that is a subtype of T.
![Page 21: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/21.jpg)
March 3rd, 2004 Object Oriented Design Course 21
Collection<Object>vs Collection<?> vs Collection
Collection<Object> is a collection of heterogeneous instances of potentially no common type
Collection<?> is a collection of homogeneous instances of some common types – we just don’t know what that common type is
Collection is a raw type – we should avoid it
![Page 22: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/22.jpg)
March 3rd, 2004 Object Oriented Design Course 22
Many More Features
Java Generics are one of the important language features of Java 1.5
More information in http://java.sun.com/developer/technicalArticles/J2SE/generics/
J2SE 5.0 in a Nutshellhttp://java.sun.com/developer/technicalArticles/releases/j2se15/
![Page 23: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/23.jpg)
March 3rd, 2004 Object Oriented Design Course 23
Java Generics Summary
Java Generics use a technique known as type erasure which is the process of translating or rewriting code that uses generics into non-generic code
all information between angle brackets is erased.
![Page 24: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/24.jpg)
March 3rd, 2004 Object Oriented Design Course 24
C# Generics
Very similar to Java public struct Point<T>
{ public T X; public T Y; } Point<int> point;
point.X = 1; point.Y = 2; See http://msdn.microsoft.com/
for more information
![Page 25: Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives](https://reader030.vdocument.in/reader030/viewer/2022032521/56649d5d5503460f94a3cac5/html5/thumbnails/25.jpg)
March 3rd, 2004 Object Oriented Design Course 25
Java Generics vs. C++ Templates vs. C#
Java borrowed C++ syntax and made it mean something very different
Type erasure vs. code generation in C++.
Evaluated in compile time in java and c++, run time in c#