Class design
Download
1 / 29

Class Design - PowerPoint PPT Presentation


  • 189 Views
  • Updated On :

Class Design. CS 3331 Fall 2009. Outline. Organizing classes Design guidelines Canonical forms of classes equals method hashCode method. Client. LinkedList. Node. Public vs. Helper Classes. Motivation To hide implementation decisions and details from clients Approach

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Class Design' - ostinmannual


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Class design l.jpg

Class Design

CS 3331

Fall 2009


Outline l.jpg
Outline

  • Organizing classes

  • Design guidelines

  • Canonical forms of classes

    • equals method

    • hashCode method


Public vs helper classes l.jpg

Client

LinkedList

Node

Public vs. Helper Classes

  • Motivation

    • To hide implementation decisions and details from clients

  • Approach

    • Separate helper classes and make them not visible to clients

    • Public classes: for general use by clients

    • Helper (or auxiliary) classes: used solely to implement public classes

0,2

<<use>>

0,2

helper class

public class


Organizing helper classes l.jpg
Organizing Helper Classes

  • Approaches

    • Separate (standalone) classes in separate files

    • Separate (standalone) classes in the same file

    • Nested classes

public class LinkedList {

protected Node head, tail;

/* … */

protected static class Node {

private Object value;

private Node next, prev;

/* … */

}

}


Outline5 l.jpg
Outline

  • Public vs helper classes

  • Design guidelines

  • Canonical forms of classes

    • equals method

    • hashCode method


Design guidelines l.jpg
Design Guidelines

  • G1: Avoid public fields

    • To minimize accessibility of fields

    • Make fields non-public (e.g., private attributes)

    • Provide accessors (e.g., getAttr() or isAttr())

    • Provide mutators (e.g., setAttr())

  • G2: Completeness of public interfaces

  • G3: Separate interface from implementation


Question l.jpg
Question

  • Which is better and why?

public class StudentBody1 {

private ArrayList students;

/* … */

public void addAll(ArrayList students) { /* … */ }

}

public class StudentBody2 {

private List students;

/* … */

public void addAll(List students) { /* … */ }

}


Separation through interfaces l.jpg

service user

LinkedList

ArrayList

<<interface>>List

service provider

Separation through Interfaces

uses

Client


Outline9 l.jpg
Outline

  • Public vs. helper classes

  • Design guidelines

  • Canonical forms of classes

    • equals method

    • hashCode method


Canonical form of classes l.jpg
Canonical Form of Classes

  • Objective

    • To ensure that objects behave “well” when manipulated by Java runtime environment and other classes.

// E.g., what will be printed?

List points = new LinkedList();

points.add(new Point(10, 20));

If (points.contains(new Point(10, 20))) {

System.out.println(“Found!”);

} else {

System.out.println(“Not found!”);

}


Canonical form cont l.jpg
Canonical Form (Cont.)

  • Canonical forms of public classes

    • String representation

      • toString method

    • No-argument constructor

    • Object equality

      • equals and hashCode methods

    • Cloning

      • Cloneable interface and clone method

    • Serialization

      • Serializable interface and readObject and writeObject methods


The tostring method l.jpg
The toString Method

  • Why?

public class Point {

private int x, y;

/* … */

public String toString() {

return “Point(“ + x + “, “ + y + “)”;

}

}


No argument constructor l.jpg
No-argument Constructor

  • Why?

public class Point {

private int x, y;

/* … */

public Point() {

this(0, 0);

}

public Point(int x, int y) {

this.x = x;

this.y = y;

}

}


Equality l.jpg

: Point

: Point

x = 10

y = 20

x = 10

y = 20

p1:

p2:

p3:

Equality

  • Identity equality vs. value quality

    “==“ vs. equals()

    “==“ implies equals()

Point p1 = new Point(10, 20);

Point p2 = new Point(10, 20);

Point p3 = p1;

Is “p1 == p2” ?

Is “p1.equals(p2)” ?

Is “p1 == p3” ?

Is “p1.equals(p3)” ?


Equality cont l.jpg
Equality (Cont.)

  • Default implementation of equals method

    • Inherited from the class Object

    • Test for object identity

  • Can be overridden by subclasses


Contract of equality l.jpg
Contract of Equality

  • Equivalence relation

    • Reflective: x.equals(x)

    • Symmetric: x.equals(y)  y.equals(x)

    • Transitive: x.equals(y)  y.equals(z)  x.equals(z)

  • Consistency

    • x.equals(y) consistently returns true or false

  • Nonnullity

    • x.equals(null) always returns false


Exercise l.jpg
Exercise

  • Define equals method for the class Point. Assume that the class has two fields x and y, both of type int.


Template for equals method l.jpg
Template for equals Method

Proposal 1: for class T



Comparing fields l.jpg
Comparing Fields

  • Primitive types

if (f != otherObj.f) {

return false;

}

  • Reference types

if (f == null ? otherObj.f != null : !f.equals(otherObj.f)) {

return false;

}


Template for equals method21 l.jpg
Template for equals Method

When T has a superclass:


Exercise22 l.jpg
Exercise

  • Define equals method for the class ColoredPoint, a subclass of the class Point. Assume that the class has an additional field named color of type Color.


Outline23 l.jpg
Outline

  • Public vs. helper classes

  • Design guidelines

  • Canonical forms of classes

    • equals method

    • hashCode method


The hashcode method l.jpg
The hashCode Method

  • Purpose

    • Returns the hash code of an object

    • Used by hash-table-based collection classes (e.g., HashMap, HashSet)

  • Contract for hashCode

    • x.equals(y)  x.hashCode() == y.hashCode()


Defining hashcode methods l.jpg
Defining hashCode Methods

  • General scheme

    • Compute hash code for each significant field

    • Combine hash code of all significant fields

public int hashCode() {

int result = 0; // accumulative hash code

int h; // hash code for a field

<<for each field compute and combine the hash code>>

return result;

}


Computing hash code for fields l.jpg
Computing hash code for fields

  • boolean fields

    • f ? 0: 1

  • byte, char, short, and int fields

    • (int) f

  • long fields

    • (int)(f ^ (f >>> 32))

  • float fields

    • Float.floatToIntBits(f)

  • double fields

    • Double.doubleToLongBits(f) and then to int

  • References

    • If null, then 0 (or some fixed value),

    • Recursive equals  recursive hash on fields, or

    • Hash on canonical representation


How to combine hash code l.jpg
How to Combine Hash Code?

  • Bitwise-or (|)

    result = result << n | h;

    where n is an arbitrary integer constant, e.g., 8.

  • Addition

    result = result * p + h;

    where p is a prime number, e.g., 37.


Example l.jpg
Example

  • Hashcode method for class Point

public int hashCode() {

int result = x;

result = result << 8 | y;

return result;

}

public int hashCode() {

int result = x;

result = result * 37 + y;

return result;

}


Exercise29 l.jpg
Exercise

  • Write a hashCode method for the class ColoredPoint, a subclass of the class Point. Assume that the class has an additional field color of type Color.


ad