SE450: Horstmann Chapter 7

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]
Subtype Examples [9/86]
Subtype Examples [10/86]
The ArrayStoreException [11/86]
Array References [12/86]
Wrapper Classes [13/86]
Enumerated Types [14/86]
Typesafe Enumerations [15/86]
Type Inquiry [16/86]
The Class Class [17/86]
An Employee Object vs. the Employee.class Object [18/86]
Type Inquiry [19/86]
Array Types [20/86]
Object: The Cosmic Superclass [21/86]
The toString Method [22/86]
Overriding the toString Method [23/86]
Overriding toString in Subclass [24/86]
The equals Method [25/86]
Overriding the equals Method [26/86]
Overriding equals in Subclass [27/86]
Not all equals Methods are Simple [28/86]
The Object.equals Method [29/86]
Requirements for equals Method [30/86]
Fixing Employee.equals [31/86]
The Perfect equals Method [32/86]
Hashing [33/86]
Hashing [34/86]
Shallow and Deep Copy [35/86]
Cloning [36/86]
Cloning [37/86]
The Cloneable Interface [38/86]
The clone Method [39/86]
Shallow Cloning [40/86]
Deep Cloning [41/86]
Deep Cloning [42/86]
Cloning and Inheritance [43/86]
Serialization [44/86]
Serialization [45/86]
How Serialization Works [46/86]
How Serialization Works [47/86]
Serialing Unserializable Classes [48/86]
Reflection [49/86]
Reflection [50/86]
Enumerating Fields [51/86]
Enumerating Constructors [52/86]
Getting A Single Method Descriptor [53/86]
Invoking a Method [54/86]
Inspecting Objects [55/86]
Inspecting Objects [56/86]
Inspecting Array Elements [57/86]
Generic Types [58/86]
Generic Methods [59/86]
Type Bounds [60/86]
Type Bounds [61/86]
Wildcards [62/86]
Wildcards [63/86]
Wildcards [64/86]
Wildcards [65/86]
Type Erasure [66/86]
Limitations of Generics [67/86]
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]
Facade Pattern [77/86]
Facade Pattern [78/86]
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]
Composing Beans [85/86]
Composing Beans [86/86]

Object-Oriented Design & Patterns [1/86]

Cay S. Horstmann

Chapter 7

The Java Object Model

horstmann-oodp2

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]

Array References [12/86]

.

Wrapper Classes [13/86]

Enumerated Types [14/86]

Typesafe Enumerations [15/86]

Type Inquiry [16/86]

The Class Class [17/86]

An Employee Object vs. the Employee.class Object [18/86]

.

Type Inquiry [19/86]

Array Types [20/86]

Object: The Cosmic Superclass [21/86]

The toString Method [22/86]

Overriding the toString Method [23/86]

Overriding toString in Subclass [24/86]

The equals Method [25/86]

Overriding the equals Method [26/86]

Overriding equals in Subclass [27/86]

Not all equals Methods are Simple [28/86]

The Object.equals Method [29/86]

Requirements for equals Method [30/86]

Fixing Employee.equals [31/86]

The Perfect equals Method [32/86]

Hashing [33/86]

Hashing [34/86]

Shallow and Deep Copy [35/86]

Cloning [36/86]

.

Cloning [37/86]

The Cloneable Interface [38/86]

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]

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]

Enumerating Constructors [52/86]

Getting A Single Method Descriptor [53/86]

Invoking a Method [54/86]

Inspecting Objects [55/86]

Inspecting Objects [56/86]

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]

Generic Types [58/86]

Generic Methods [59/86]

Type Bounds [60/86]

Type Bounds [61/86]

Wildcards [62/86]

Wildcards [63/86]

Wildcards [64/86]

Wildcards [65/86]

Type Erasure [66/86]

Limitations of Generics [67/86]

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
  1. A subsystem consists of multiple classes, making it complicated for clients to use
  2. Implementor may want to change subsystem classes
  3. Want to give a coherent entry point
Solution
  1. Define a facade class that exposes all capabilities of the subsystem as methods
  2. The facade methods delegate requests to the subsystem classes
  3. The subsystem classes do not know about the facade class

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.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;
}
file:horstmann/ch07_carbean/CarBean.mf

Composing Beans [85/86]

Composing Beans [86/86]



Revised: 2007/11/02 18:30