Sie sind auf Seite 1von 3

Outline

Designing MultiMulti-class Programs


COMP 102 #30

2 Oct 2007

Peter Andreae

COMP 102 :2

Multi-Class programs
Class Diagrams
Designing MiniDraw
Implementing Interface classes.

Reading:
Reading

Computer Science
Victoria University of Wellington

L&L Chapter 8 Interfaces.

Announcements:
Announcements
Lab 10 available
Copyright: Peter Andreae, Victoria University of Wellington 2007

Peter Andreae

Designing Programs

COMP 102 :3

What happens when programs get large?


More classes
More interactions
Harder to understand.

Designing Programs

COMP 102 :4

The central problem in programming is handling complexity


The central techniques to handle complexity are:
Modularity:

Breaking problems into smaller parts that can be solved separately.

methods:
classes:

a separate method for each subtask


a separate class for each component of the program

Questions: how big is a module?

Abstraction:
Hiding the details of a module, so that other parts
of the program only need to know a little bit about it:
eg:
name, parameters, return type
constructor and possible actions
Questions: what should you show? what should you hide?

Peter Andreae

Peter Andreae

Designing Classes

COMP 102 :5

Interesting programs require multiple classes


(Assig 3)

CartoonStrip

Choosing Classes !!!!!!!!!

COMP 102 :6

No fixed Formula! It is a matter of good design, but


Separate the user interface from the rest of the program.

(Assig 5)

ScreenSaver

One class to set up the interface and handle the interaction

CartoonFigure

(typically only one object of this class is created)

MovingShape

Other classes to store and manipulate the information


(typically multiple objects of these classes are created)
(Assig 9)

(Assig 8)

MineSweeper

Garden

Have a separate class for each type of information


One class for the Flower; another for the collection of flowers.

Grid

One class for a Cell; another for the Grid of Cells.


One class for the Maze; another class for the Mouse;

Flowers

another class for cells of the Maze; another class

Cell

Design problem: How do you break a task into classes?

for coordinates; another class for Directions.


L&L 6.2 talks about an approach for identifying
potential classes for a problem.

Peter Andreae

Designing Classes
(The interface that controlled the toons)

COMP 102 :7
(The user interface
and control loop)

(Assig 5)

(The individual shapes)

CartoonFigure

MovingShape

(Assig 9)

MineSweeper

Arrows to show that one class uses another class

Calls methods or constructors from the other class

flowers = new Flower(20, 10);


flowers[i].bloom();
Refers to public static fields of the other class

(The collection of Squares)

min = Integer.MAX_VALUE;

(field of Integer class)

eg, in Grid.java:

Grid

cells[r][c].draw(canvas,x, y, MineSweeper.cellSize, MineSweeper.cellSize)

(The individual Flowers)

Flowers

Class diagrams can help in understanding a program :

private Flower[ ] flowers;


(The user interface
and controller)

Garden

COMP 102 :8

Has variables of the type of the other class

(The individual toons)

(The user interface AND


the collection of Flowers)

Class Diagrams
Box for each class

ScreenSaver

CartoonStrip

Peter Andreae

(Individual Squares)

Cell
Peter Andreae

Other arrows for other relationships.


Peter Andreae

Class Diagrams

COMP 102 :9

Class boxes may list the class interface:

[ L&L: 4.1 ]

public constructors, methods, and possibly fields

the elements of the class that other classes can access

COMP 102 :10

Interface is the boundary/connection between two things


User interface:

also need a description of the semantics of the class

boundary between computer and user

(only given by comments in most languages)


Java

Interface
Interface is ambiguous:

javadoc documentation

The interface of a class is the abstraction represented by


the class

Class interface:
boundary between class and rest of program

other classes do not need to know any other details

A Java Interface:

BlueJ automatically draws class diagrams for you

Special kind of class that only defines a type

It sometimes misses out some arrows


doesnt display the interface easily.
Makes it easy to see the javadoc documentation of a class
(if you wrote javadoc comments: /** .. */)

Peter Andreae

javadoc describes the interface


interface
Use the javadoc description
of a class when writing code
that uses that class:

COMP 102 :11

(Assig 5)

ScreenSaver

Peter Andreae

Designing MiniDraw (Assig 10)

COMP 102 :12

A simple drawing editor


Allows user to create drawings consisting of shapes:
rectangles, ovals, lines, text strings, dots, trees,

MovingShape

different colours.
can add a new shape,
remove a shape

Constructor Summary

move a shape to a different position

MovingShape (int x, int y, int limit, DrawingCanvas c)


Construct a new MovingShape object.

save the current drawing to a file


load a previous drawing from a file and edit it further.

Method Summary

Run the demo!

void draw ()
Draw the shape on the canvas in its current position
void erase ()
Erase the rectangle containing the shape
void move (int stepX, int stepY)
Move the shape by the given step size.
Peter Andreae

Peter Andreae

Das könnte Ihnen auch gefallen