Object-Oriented Design & Patterns

Cay S. Horstmann

Chapter 7

The Java Object Model

Chapter Topics

Types

Java Types

Exercise: What kind of type?

Java Values

Exercise: What kind of value?

Subtype Relationship

S is a subtype of T if

Subtype Examples

Subtype Examples

.

The ArrayStoreException

Array References

.

Wrapper Classes

Enumerated Types

Typesafe Enumerations

Type Inquiry

The Class Class

An Employee Object vs. the Employee.class Object

.

Type Inquiry

Array Types

Object: The Cosmic Superclass

The toString Method

Overriding the toString Method

Overriding toString in Subclass

The equals Method

Overriding the equals Method

Overriding equals in Subclass

Not all equals Methods are Simple

The Object.equalsMethod

Requirements for equals Method

Fixing Employee.equals

The Perfect equals Method

Hashing

Hashing

Shallow and Deep Copy

Cloning

.

Cloning

The Cloneable Interface

The clone Method

public class Employee 
implements Cloneable
{
public Object clone()
{
try
{
return super.clone();
}
catch(CloneNotSupportedException e)
{
return null; // won't happen
}
}
...
}

Shallow Cloning


Deep Cloning

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

.

Cloning and Inheritance

Serialization

Serialization

How Serialization Works

How Serialization Works

.

Serialing Unserializable Classes

Reflection

Reflection

Enumerating Fields

Enumerating Constructors

Getting A Single Method Descriptor

Invoking a Method

Inspecting Objects

Inspecting Objects

Inspecting Array Elements

Generic Types

Generic Methods

Type Bounds

Type Bounds

Wildcards

Wildcards

Wildcards

Wildcards

Type Erasure

Limitations of Generics

Limitations of Generics

Components

A Builder Environment



Java Beans

Java Beans

.

A Calendar Bean



A Property Sheet

 

Façade Class

Façade Pattern

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

Façade Pattern

  .

Façade Pattern

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

Property Syntax

Java Naming Conventions

Editing Beans in a Builder Tool


Editing Beans in a Builder Tool



Packaging a Bean

Composing Beans

Composing Beans