Contents [0/86] |
Object-Oriented Design & Patterns [1/86] |
Chapter Topics [2/86] |
Types [3/86] |
Java Types [4/86] |
Exercise: What kind of type? [5/86] |
Java Values [6/86] |
Exercise: What kind of value? [7/86] |
Subtype Relationship [8/86] |
S is a subtype of T if
Subtype Examples [9/86] |
Subtype Examples [10/86] |
The ArrayStoreException [11/86] |
Rectangle[] r = new Rectangle[10];
Shape[] s = r;
Array References [12/86] |
Wrapper Classes [13/86] |
Integer Short Long Byte
Character Float Double Boolean
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(13); // calls new Integer(13)
int n = numbers.get(0); // calls intValue();
Enumerated Types [14/86] |
Typesafe Enumerations [15/86] |
public class Size { private /* ! */ Size() { } public static final Size SMALL = new Size(); public static final Size MEDIUM = new Size(); public static final Size LARGE = new Size(); }
Type Inquiry [16/86] |
The Class Class [17/86] |
Object e = new Rectangle();
Class c = e.getClass();
System.out.println(c.getName()); // prints java.awt.Rectangle
An Employee Object vs. the Employee.class Object [18/86] |
Type Inquiry [19/86] |
Array Types [20/86] |
double[] a = new double[10];
Class c = a.getClass();
if (c.isArray())
System.out.println(c.getComponentType());
// prints double
[D for double[])
[[java.lang.String; for String[][]
Object: The Cosmic Superclass [21/86] |
The toString Method [22/86] |
Overriding the toString Method [23/86] |
public class Employee
{
public String toString()
{
return getClass().getName()
+ "[name=" + name
+ ",salary=" + salary
+ "]";
}
...
}
Employee[name=Harry Hacker,salary=35000]
Overriding toString in Subclass [24/86] |
public class Manager extends Employee
{
public String toString()
{
return super.toString() + "[department=" + department + "]";
}
}
Manager[name=Dolly Dollar,salary=100000][department=Finance]
The equals Method [25/86] |
/**
Searches for the first occurrence of the given argument,
testing for equality using the equals method.
@param elem an object.
@return the index of the first occurrence
of the argument in this list; returns -1 if
the object is not found.
*/
public int indexOf(Object elem)
{
if (elem == null)
{
for (int i = 0; i < size; i++)
if (elementData[i] == null) return i;
}
else
{
for (int i = 0; i < size; i++)
if (elem.equals(elementData[i])) return i;
}
return -1;
}
Overriding the equals Method [26/86] |
public class Employee
{
public boolean equals(Object otherObject)
// not complete--see below
{
Employee other = (Employee)otherObject;
return name.equals(other.name)
&& salary == other.salary;
}
...
}
Overriding equals in Subclass [27/86] |
public class Manager
{
public boolean equals(Object otherObject)
{
Manager other = (Manager)otherObject;
return super.equals(other)
&& department.equals(other.department);
}
}
Not all equals Methods are Simple [28/86] |
public boolean equals(Object o)
{
if (o == this) return true;
if (!(o instanceof Set)) return false;
Collection c = (Collection) o;
if (c.size() != size()) return false;
return containsAll(c);
}
The Object.equals Method [29/86] |
public class Object
{
public boolean equals(Object obj)
{
return this == obj;
}
...
}
Requirements for equals Method [30/86] |
Fixing Employee.equals [31/86] |
The Perfect equals Method [32/86] |
public boolean equals(Object otherObject)
{
if (this == otherObject) return true;
if (otherObject == null) return false;
if (getClass() != otherObject.getClass()) return false;
...
}
Hashing [33/86] |
Hashing [34/86] |
public class Employee
{
public int hashCode()
{
return name.hashCode()
+ new Double(salary).hashCode();
}
...
}
Shallow and Deep Copy [35/86] |
Cloning [36/86] |
Cloning [37/86] |
public class Employee
{
public Object clone()
{
return super.clone(); // not complete
}
...
}
The Cloneable Interface [38/86] |
public interface Cloneable
{
}
The clone Method [39/86] |
public class Employee
implements Cloneable
{
public Object clone()
{
try
{
return super.clone();
}
catch(CloneNotSupportedException e)
{
return null; // won't happen
}
}
...
}
Shallow Cloning [40/86] |
Deep Cloning [41/86] |
public class Employee
implements Cloneable
{
public Object clone()
{
try
{
Employee cloned = (Employee)super.clone();
cloned.hireDate = (Date)hiredate.clone();
return cloned;
}
catch(CloneNotSupportedException e)
{
return null; // won't happen
}
}
...
}
Deep Cloning [42/86] |
Cloning and Inheritance [43/86] |
Serialization [44/86] |
Employee[] staff = new Employee[2];
staff.add(new Employee(...));
staff.add(new Employee(...));
ObjectOutputStream out
= new ObjectOutputStream(
new FileOutputStream("staff.dat"));
out.writeObject(staff);
out.close();
Serialization [45/86] |
How Serialization Works [46/86] |
How Serialization Works [47/86] |
Serialing Unserializable Classes [48/86] |
file:horstmann/ch07_serial2/Car.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package horstmann.ch07_serial2; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.RectangularShape; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** A serializable car shape. */ public class Car implements Serializable { private static final long serialVersionUID = 2008L; /** Constructs a car. @param x the left of the bounding rectangle @param y the top of the bounding rectangle @param width the width of the bounding rectangle */ public Car(int x, int y, int width) { body = new Rectangle(x, y + width / 6, width - 1, width / 6); roof = new Rectangle(x + width / 3, y, width / 3, width / 6); frontTire = new Ellipse2D.Double(x + width / 6, y + width / 3, width / 6, width / 6); rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3, width / 6, width / 6); } private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); writeRectangularShape(out, frontTire); writeRectangularShape(out, rearTire); } /** A helper method to write a rectangular shape. @param out the stream onto which to write the shape @param s the shape to write */ private static void writeRectangularShape(ObjectOutputStream out, RectangularShape s) throws IOException { out.writeDouble(s.getX()); out.writeDouble(s.getY()); out.writeDouble(s.getWidth()); out.writeDouble(s.getHeight()); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); frontTire = new Ellipse2D.Double(); readRectangularShape(in, frontTire); rearTire = new Ellipse2D.Double(); readRectangularShape(in, rearTire); } /** A helper method to read a rectangular shape. @param in the stream from which to read the shape @param s the shape to read. The method sets the frame of this rectangular shape. */ private static void readRectangularShape(ObjectInputStream in, RectangularShape s) throws IOException { double x = in.readDouble(); double y = in.readDouble(); double width = in.readDouble(); double height = in.readDouble(); s.setFrame(x, y, width, height); } /** Draws the car. @param g2 the graphics context */ public void draw(Graphics2D g2) { g2.draw(body); g2.draw(roof); g2.draw(frontTire); g2.draw(rearTire); } public String toString() { return getClass().getName() + "[body=" + body + ",roof=" + roof + ",frontTire=" + formatRectangularShape(frontTire) + ",rearTire=" + formatRectangularShape(rearTire) + "]"; } /** A helper method to format a rectangular shape. @param s the shape to format @return a formatted representation of the given shape */ private static String formatRectangularShape(RectangularShape s) { return RectangularShape.class.getName() + "[x=" + s.getX() + ",y=" + s.getY() + ",width=" + s.getWidth() + ",height=" + s.getHeight() + "]"; } private Rectangle body; private Rectangle roof; private transient Ellipse2D.Double frontTire; private transient Ellipse2D.Double rearTire; }
Reflection [49/86] |
Reflection [50/86] |
Enumerating Fields [51/86] |
Field[] fields = Math.class.getDeclaredFields();
for (Field f : fields)
if (Modifier.isStatic(f.getModifiers()))
System.out.println(f.getName());
Enumerating Constructors [52/86] |
for (Constructor c : cons)
{
Class[] params = cc.getParameterTypes();
System.out.print("Rectangle(");
boolean first = true;
for (Class p : params)
{
if (first) first = false; else System.out.print(", ");
System.out.print(p.getName());
}
System.out.println(")");
}
Rectangle()
Rectangle(java.awt.Rectangle)
Rectangle(int, int, int, int)
Rectangle(int, int)
Rectangle(java.awt.Point, java.awt.Dimension)
Rectangle(java.awt.Point)
Rectangle(java.awt.Dimension)
Getting A Single Method Descriptor [53/86] |
Method m = Rectangle.class.getDeclaredMethod(
"contains", int.class, int.class);
Constructor c = Rectangle.class.getDeclaredConstructor();
Invoking a Method [54/86] |
Method m = PrintStream.class.getDeclaredMethod(
"println", String.class);
m.invoke(System.out, "Hello, World!");
Inspecting Objects [55/86] |
Class c = obj.getClass();
Field f = c.getDeclaredField(name);
f.setAccessible(true);
Object value = f.get(obj);
f.set(obj, value);
Inspecting Objects [56/86] |
int currentPosition=0
int newPosition=-1
int maxPosition=13
java.lang.String str=Hello, World!
java.lang.String delimiters=,
boolean retDelims=false
boolean delimsChanged=false
char maxDelimChar=,
---
int currentPosition=5
. . .
file:horstmann/ch07_reflect2/FieldTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package horstmann.ch07_reflect2; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Random; /** This program shows how to use reflection to print the names and values of all nonstatic fields of an object. */ public class FieldTester { public static void main(String[] args) throws IllegalAccessException { Random r = new Random(); System.out.print(spyFields(r)); r.nextInt(); System.out.println("\nAfter calling nextInt:\n"); System.out.print(spyFields(r)); } /** Spies on the field names and values of an object. @param obj the object whose fields to format @return a string containing the names and values of all fields of obj */ public static String spyFields(Object obj) throws IllegalAccessException { StringBuilder buffer = new StringBuilder(); Field[] fields = obj.getClass().getDeclaredFields(); for (Field f : fields) { if (!Modifier.isStatic(f.getModifiers())) { f.setAccessible(true); Object value = f.get(obj); buffer.append(f.getType().getName()); buffer.append(" "); buffer.append(f.getName()); buffer.append("="); buffer.append("" + value); buffer.append("\n"); } } return buffer.toString(); } }
Inspecting Array Elements [57/86] |
Object value = Array.get(a, i);
Array.set(a, i, value);
int n = Array.getLength(a);
Object a = Array.newInstance(type, length);
Generic Types [58/86] |
public class ArrayList<E>
{
public E get(int i) { . . . }
public E set(int i, E newValue) { . . . }
. . .
private E[] elementData;
}
Generic Methods [59/86] |
public class Utils
{
public static <E> void fill(ArrayList<E> a, E value, int count)
{
for (int i = 0; i < count; i++)
a.add(value);
}
}
ArrayList<String> ids = new ArrayList<String>();
Utils.fill(ids, "default", 10); // calls Utils.<String>fill
Type Bounds [60/86] |
public static <E> void append(ArrayList<E> a, ArrayList<E> b, int count)Cannot append an ArrayList<Rectangle> to an ArrayList<Shape>
{
for (int i = 0; i < count && i < b.size(); i++)
a.add(b.get(i));
}
Type Bounds [61/86] |
public static <E, F extends E> void append(
ArrayList<E> a, ArrayList<F> b, int count)
{
for (int i = 0; i < count && i < b.size(); i++)
a.add(b.get(i));
}
Wildcards [62/86] |
public static <E> void append(
ArrayList<E> a, ArrayList<? extends E> b, int count)
{
for (int i = 0; i < count && i < b.size(); i++)
a.add(b.get(i));
}
Wildcards [63/86] |
Wildcards [64/86] |
Wildcards [65/86] |
public static <E extends Comparable<E>> E getMax(ArrayList<E> a)
{
E max = a.get(0);
for (int i = 1; i < a.size(); i++)
if (a.get(i).compareTo(max) > 0) max = a.get(i);
return max;
}
public static <E extends Comparable<? super E>> E getMax(ArrayList<E> a)
Type Erasure [66/86] |
public class ArrayList
{
public Object get(int i) { . . . }
public Object set(int i, Object newValue) { . . . }
. . .
private Object[] elementData;
}
public static Comparable getMax(ArrayList a)
// E extends Comparable<? super E> erased to Comparable
Limitations of Generics [67/86] |
public static <E> void fillWithDefaults(ArrayList<E>,
Class<? extends E> cl, int count)
throws InstantiationException, IllegalAccessException
{
for (int i = 0; i < count; i++)
a.add(cl.newInstance());
}
Limitations of Generics [68/86] |
Components [69/86] |
A Builder Environment [70/86] |
Java Beans [71/86] |
Java Beans [72/86] |
A Calendar Bean [73/86] |
A Property Sheet [74/86] |
Facade Class [75/86] |
Facade Pattern [76/86] |
Context
Facade Pattern [77/86] |
Facade Pattern [78/86] |
Name in
Design Pattern |
Actual Name
(Beans) |
Client |
Builder tool |
Facade |
Main bean class with which the
tool interacts |
SubsystemClass |
Class used to implement bean functionality |
Bean Properties [79/86] |
Property Syntax [80/86] |
Java Naming Conventions [81/86] |
Editing Beans in a Builder Tool [82/86] |
Editing Beans in a Builder Tool [83/86] |
Packaging a Bean [84/86] |
file:horstmann/ch07_carbean/CarBean.mf
file:horstmann/ch07_carbean/CarBean.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package horstmann.ch07_carbean; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import javax.swing.JComponent; /** A component that draws a car shape. */ @SuppressWarnings("serial") public class CarBean extends JComponent { /** Constructs a default car bean. */ public CarBean() { x = 0; y = 0; width = DEFAULT_CAR_WIDTH; height = DEFAULT_CAR_HEIGHT; } /** Sets the x property. @param newValue the new x position */ public void setX(int newValue) { x = newValue; repaint(); } /** Gets the x property. @return the x position */ public int getX() { return x; } /** Sets the y property. @param newValue the new y position */ public void setY(int newValue) { y = newValue; repaint(); } /** Gets the y property. @return the y position */ public int getY() { return y; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle2D.Double body = new Rectangle2D.Double(x, y + height / 3, width - 1, height / 3); Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6, y + height * 2 / 3, height / 3, height / 3); Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + height * 2 / 3, height / 3, height / 3); // The bottom of the front windshield Point2D.Double r1 = new Point2D.Double(x + width / 6, y + height / 3); // The front of the roof Point2D.Double r2 = new Point2D.Double(x + width / 3, y); // The rear of the roof Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3, y); // The bottom of the rear windshield Point2D.Double r4 = new Point2D.Double(x + width * 5 / 6, y + height / 3); Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindshield = new Line2D.Double(r3, r4); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_PANEL_WIDTH, DEFAULT_PANEL_HEIGHT); } private int x; private int y; private int width; private int height; private static final int DEFAULT_CAR_WIDTH = 60; private static final int DEFAULT_CAR_HEIGHT = 30; private static final int DEFAULT_PANEL_WIDTH = 160; private static final int DEFAULT_PANEL_HEIGHT = 130; }
Composing Beans [85/86] |
Composing Beans [86/86] |
Revised: 2007/11/02 18:30