designing classes chapter 3. 2 chapter contents encapsulation specifying methods java interfaces...

27
Designing Classes Chapter 3

Post on 19-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

Designing Classes

Chapter 3

Page 2: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

2

Chapter ContentsEncapsulationSpecifying MethodsJava Interfaces• Writing an Interface• Implementing an Interface• An Interface as a Data Type• Type Casts Within an Interface Implementation• Extending an Interface• Named Constants Within an Interface• Interfaces Versus Abstract Classes

Choosing Classes• Identifying Classes• CRC Cards

Reusing Classes

Page 3: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

3

Encapsulation

Hides the fine detail of the inner workings of the class• The implementation is hidden• Often called "information hiding"

Part of the class is visible• The necessary controls for the class are left

visible• The class interface is made visible• The programmer is given only enough

information to use the class

public stuff

Page 4: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

4

Encapsulation

Fig. 3-1 An automobile's controls are visible to the driver, but its inner workings are hidden.

Page 5: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

5

Abstraction

A process that has the designer ask what instead of why or how.• What is it you want to do with the data• What will be done to the data

The designer does not consider how the class's methods will accomplish their goals

The client interface is the what

The implementation is the how

goes into theinterface

Page 6: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

6

Abstraction

Fig. 3-2 An interface provides well-regulated

communication between a hidden implementation and

a client.

We are using “interface” in twosenses here – public face of a class and a Java Interface. The latter is a “forced” set of rules for

the former.

Page 7: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

7

Specifying Methods

Specify what each method does

Precondition• Defines responsibility of client code. What must be

true before the method is called.

Postcondition• Specifies what will happen if the preconditions are met.

What is true about the data after the method is called.

Assertions can be written as comments to identify design logic// Assertion: intVal >= 0

like a precondition

Page 8: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

8

Specifying Methods

I also add:• Overview: A simple, English-language explanation of what the method does.

• Exception: An explanation of any exceptions a method handles or at least encounters even if the method doesn’t “handle” them.

I rename Precondition as Requires

I rename Postcondition as Returns

Page 9: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

9

Java Interface

A program component that contains• Public constants• Signatures for public methods• Comments that describe them

Begins like a class definition• Use the word interface instead of class

public interface someClass{ public int someMethod();}

“final” stuff

Page 10: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

10

Java Interface Examplepublic interface NameInterface

{ /** Task: Sets the first and last names.

* @param firstName a string that is the desired first name

* @param lastName a string that is the desired last name */

public void setName(String firstName, String lastName);

/** Task: Gets the full name.

* @return a string containing the first and last names */

public String getName();

public void setFirst(String firstName);

public String getFirst();

public void setLast(String lastName);

public String getLast();

public void giveLastNameTo(NameInterface child);

public String toString();

} // end NameInterface

javadoc comment. Lookin the Javadoc view of Eclipse.

Page 11: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

11

Implementing an Interface

A class that implements an interface must state so at start of definitionpublic class myClass implements someInterface

The class must implement every method declared in the interface

Multiple classes can implement the same interface

A class can implement more than one interfaceAn interface can be used as a data typepublic void someMethod (someInterface x)

a promise to implement all methods in the interface.

Important: someMethod() can be written before any class that implements someInterface

Page 12: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

12

Implementing an Interface

Fig. 3-3 The files for an interface, a class that implements the interface, and the client.

Page 13: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

13

Type Casts Within an Interface Implementation

In any class that implements an interface• A parameter may be of type Object• A type cast would probably be needed within that

method public class Pet implements Comparable{ private String name;

private int age; // in yearsprivate double weight; // in pounds

/** Task: Compares the weight of two pets. */public int compareTo(Object other){ Pet otherPet = (Pet)other;

return weight - otherPet.weight;} // end compareTo< Other methods are here. >

} // end Pet

Page 14: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

14

Generic Comparable Interface:

package java.lang;public interface Comparable <T> { public int compareTo(T,other);}

public class Circle implements Comparable<Circle>, Measurable { private double radius; . . . public int compareTo(Circle other) { // notice; no need to cast if ( this.equals(other)) return 0; // other as a Circle; it already if ( radius < other.radius ) return -1; // is a Circle. return 1; }}

Page 15: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

15

Extending an Interface

Use inheritance to derive an interface from another

When an interface extends another• It has all the methods of the inherited interface• Also include some new methods

Also possible to combine several interfaces into a new interface• Not possible with classes

not “implements”

Page 16: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

16

Named Constants Within an Interface

An interface can contain named constants• Public data fields initialized and declared as final

Consider an interface with a collection of named constants• Then derive variety of interfaces that can make

use of these constantspublic interface ConstantsInterface { public static final int STUNPort = 3478; . . .}

Page 17: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

17

Interfaces Versus Abstract Classes

Purpose of interface similar to purpose of abstract class

But … an interface is not a base class• It is not a class of any kind

Use an abstract base class when• You need a method or private data field that

classes will have in common

Otherwise use an interfaceclasses are much more “valuable” than interfaces

Page 18: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

18

Choosing Classes

Look at a prospective system from a functional point of view

Ask • What or who will use the system• What can each actor do with the system• Which scenarios involve common goals

Use a case diagram

Page 19: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

19

Choosing Classes

Fig. 3-4 A use case diagram for a registration system

Page 20: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

20

Identifying Classes

Describe the system• Identify nouns and verbs

Nouns suggest classes• Students• Transactions• Customers

Verbs suggest appropriate methods• Print an object• Post a transaction• Bill the customer

Page 21: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

21

Identifying Classes

Fig. 3-5 A description of a use case for adding a course

Page 22: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

22

CRC Cards

Index cards – each card represents one class

Write a descriptive name for class at top

List the class's responsibilities• The methods

Indicate interactions• The collaborations

These are CRC cards "Class-Responsibility-Collaboration"

Page 23: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

23

CRC Cards

Fig. 3-6 A class-responsibility-collaboration card

Page 24: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

24

Unified Modeling Language

Used to illustrate a system's classes and relationships

Provides a class diagram• Class name• Attributes• Operations

Fig. 3-7 A class representation that can be part of a class diagram.

Page 25: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

25

Unified Modeling Language

Fig. 3-8 UML notation for a base class Student and two derived classes

Page 26: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

26

Unified Modeling Language

Fig. 3-9 Part of a UML class diagram with associations.

Page 27: Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface

27

Reusing Classes

Much software combines:• Existing components• New components

When designing new classes• Plan for reusability in the future• Make objects as general as possible• Avoid dependencies that restrict later use by

another programmer