+ All Categories
Home > Documents > Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Date post: 27-Dec-2015
Category:
Upload: morris-heath
View: 217 times
Download: 3 times
Share this document with a friend
29
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities
Transcript
Page 1: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Computer Science II

810:062 Section 01

Session 2 - Objects and Responsibilities

Page 2: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

The anatomy of an OO program

The world

consists of objects

that interact

to solve a problem.

Page 3: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Last class we considered a MemoPad example

Allows for the storage of a collection of memos that are associated with keys

Page 4: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Object-Oriented Programs

• MemoPad models the piece of the world of interest. • main() is the “big bang” that creates one or more objects

for this MemoPad “world”.

public class MemoPadApp {

public static void main( String [] args ) { MemoPad p = new MemoPad(); p.show(); } // end main

} // end class MemoPadApp

Page 5: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

MemoPad’s collaborating objects

Field Panel

Button Panel

Message Panel

MemoDatabase

key value MemoAssociation

Page 6: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

MemoPad UML Diagram

Page 7: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

MemoPad’s Constructor public class MemoPad extends CloseableFrame {

private MemoDatabase database; //Used interface type! private FieldPanel fieldPanel; private ButtonPanel buttonPanel; private MessagePanel messagePanel;

public MemoPad() { // can assign any object that satisfies the interface

database = new DefaultMemoDatabase();... fieldPanel = new FieldPanel(); ... buttonPanel = new ButtonPanel( this ); ... messagePanel = new MessagePanel( "..No message..”); ... } // end MemoPad constructor

...

Page 8: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Objects interact by sending each other messages

// later in MemoPad class

public void find() { String value = database.find(fieldPanel.currentKey()); if ( value == null ) messagePanel.setMessage( "Key not found." ); else { fieldPanel.setValue( value ); messagePanel.setMessage( fieldPanel.currentKey() +

" : " + value ); } // end if

} // end find

Page 9: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Focus on MemoDatabase interface

• its interface is the set of messages to which it should respond

• but to make an database object we need a class definition that adheres to the MemoDatabase interface, i.e., that implements the specified methods

• here the DefaultMemoDatabase class is used which stores MemoAssociations in a hashTable

Page 10: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Interface for the memo database

public interface MemoDatabase {

public String find ( String key );

public boolean remove ( String key );

public boolean insert (MemoAssociation m);

public boolean containsKey( String key );

} // end interface MemoDatabase

Page 11: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

DefaultMemoDatabase class import java.util.Hashtable;

public class DefaultMemoDatabase implements MemoDatabase{ private Hashtable associations;

public DefaultMemoDatabase() { associations = new Hashtable(); } // end DefaultMemoDatabase constructor

public boolean insert( MemoAssociation newEntry ) { if ( containsKey( newEntry.key() ) ) return false; associations.put( newEntry.key(),

newEntry.value() ); return true; } // end insert

...

Page 12: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Homework #1

• Write a different class that implements the MemoDatabase using a different representation (e.g., array, ArrayList, vector, two arrays, ...)

• Modify the MemoPad class to use your class.• Submission details at http://www.cs.uni.edu/~fienup/cs062s06/homework/requirements.htm

Page 13: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

How your understanding of private vs. public?

• What things are declared private?• What things are declared public?

• Why are they declared private?• Why are they declared public?

Page 14: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Access Modifiers

• A Java programmer can control access to a class and its parts (both data and actions) through the use of Access Modifiers.– Any item that is declared public can be

accessed by any piece of code that knows the name of the item.

– Any item that is declared private can be accessed only be code within the same class.

Page 15: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

So when do we use what?

Because of this, we normally followed the following conventions --

• Classes are usually declared public, because we want to create instances of the class that can help us solve a problem.

• Methods are sometimes declared public, because they correspond to messages that other objects can send to the instance.

• Instance variables are private, because we want the instance to have exclusive control over changes to their values.

Page 16: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Access ModifiersBut what if want to do things a little differently?  Could we have-- • a private class?

– Sure.  You might want to declare a private class because you don’t want just anyone to create instances of it.

• a private method? – You might want to declare a private method, because it is not a part of

the object’s public interface. Perhaps it is simply a helper to one of the public methods.

• a public instance variable? – You might want to declare a public instance variable simply for your

convenience as a programmer. BUT..

Page 17: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Lunar Lander Example

• Lander starts some initial distance above the surface of the moon with a limited amount of fuel

• As time ticks off, the lander falls toward the moon, gaining speed according to the gravity of the moon.

• The user can request to thrust the lander's engines which counteract the force of gravity and slow down the vehicle.  However, this also burns fuel.

• The user can request multiple burns in any time interval.

• If the vehicle hits the surface traveling slow enough, then the user wins!

Page 18: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Lunar Lander Example

• What objects can you identify?

• What behaviors must these objects perform?

Page 19: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Learning to Design Programs

You can learn to design programs by studying good and bad designs, and coming to understand why they are good and bad.  But that isn’t enough:

Do you think a violinist listens to music and reads a book and then steps onto stage to perform?

Professional writers are always writing, so it's no wonder they are good.

-- Richard Gabriel

Page 20: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Learning to Design Programs

You also learn to design programs by designing programs and studying the results.– Good design comes from experience.– Experience comes from bad design.

Page 21: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Programming in the small and in the large

What are some of the distinctions between these two concepts?

Page 22: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Object-Oriented Design

Designing any system requires:

1. identifying the key components, or objects, in the system

2. identifying the main responsibilities of each object

3. identifying the main communication paths among them

Page 23: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Object-Oriented Design

We focus on responsibility and behavior first because:

• we can understand the behavior of a system by looking at interactions among parts and users

• behavior makes no commitments to implementation

• designing to behavior promotes maximum reuse

Page 24: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Object-Oriented Design

Two tools capture the three main features of a design:

• CRC cards

• Interaction diagrams

Page 25: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Object-Oriented Design

CRC cards focus us on the who and the what and discourage the how. We design by decomposing responsibilities into small, cohesive, and well-defined sets.– When the responsibilities won’t fit on a CRC card, the set

probably isn’t small enough. – When the responsibilities are not cohesive, we split off new

objects. – When the responsibilities are not well-defined, we consider

whether some other object should have some of the responsibilities.

– Some advantages of CRC cards: low tech, right size, shuffleable, easy to throw away, etc.

Page 26: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

A Blank CRC CardClass Name: Superclass: Subclasses:

Responsibilities Collaborations

Page 27: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Object-Oriented Design

Interaction diagrams show us how different object collaborate to solve the problem.

Page 28: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

Summary

Designing a system requires:• identifying the key components, or objects, in the system • identifying the main responsibilities of each object • drawing the main communication paths among them

Together, CRC cards, class diagrams, and interaction diagrams document a design by recording these features.

• CRC document the who and the what and discourage the how.

• Class diagrams document communication paths.• Interaction diagrams document the how.

Page 29: Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.

For next class

• Read chapter 3.


Recommended