object-oriented modeling chapter 10 csci 1302. csci 1302 – object-oriented modeling2 outline the...

24
Object-Oriented Modeling Chapter 10 CSCI 1302

Upload: gillian-may

Post on 01-Jan-2016

241 views

Category:

Documents


0 download

TRANSCRIPT

Object-Oriented Modeling

Chapter 10CSCI 1302

CSCI 1302 – Object-Oriented Modeling 2

Outline

• The Software Development Process• Discovering Relationships Among

Classes– Association– Aggregation and Composition– Inheritance

• Class Design Guidelines– Designing a Class– Using the Visibility Modifiers

CSCI 1302 – Object-Oriented Modeling 3

Outline

• Class Design Guidelines (cont.)– Using the static Modifier– Using Inheritance or Composition– Using Interfaces or Abstract Classes

• Framework-Based Programming Using the Java API

CSCI 1302 – Object-Oriented Modeling 4

Software Development Process

• Software products follow a general process

Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

CSCI 1302 – Object-Oriented Modeling 5

Requirements Specification

• A formal process that seeks to understand the problem and document in detail what the system should do

• Close interaction between users and designers

• Real world projects are not as clearly defined or as simple as examples in class or book

CSCI 1302 – Object-Oriented Modeling 6

System analysis

• Analyze the business process in terms of data flow

• Identify input and output• Model the system’s behavior

CSCI 1302 – Object-Oriented Modeling 7

System design

• Process of designing the system’s components

• Many levels of abstraction to decompose the problem into manageable components

• Identify classes and interfaces and establish relationships among them

CSCI 1302 – Object-Oriented Modeling 8

Implementation

• Translate the design into programs• Separate programs are written for each

component• Involves coding, testing, and debugging

CSCI 1302 – Object-Oriented Modeling 9

Testing

• Ensures that the code meets the requirements specification and weeds out bugs

• Independent testers are usually used to conduct testing

CSCI 1302 – Object-Oriented Modeling 10

Deployment

• Makes the project available for use• Applets – Install on a web server• Application – Installing on client’s

computer• Projects consist of many classes, java

often packages all of them into a Java archive file (.jar file)

CSCI 1302 – Object-Oriented Modeling 11

Maintenance

• Changing and improving the product• Periodic upgrades• Bug fixing• Incorporate any changes

CSCI 1302 – Object-Oriented Modeling 12

Discovering Relationships

• Three types of class relationships– Association– Aggregation– Inheritance

CSCI 1302 – Object-Oriented Modeling 13

Association

• General binary relationship that describes an activity between two classes

Student Faculty Course * 5..60

Take Teach 0..3 1

Teacher

public class Student {

/** Data fields */ private Course[]

courseList;

/** Constructors */

/** Methods */

}

public class Course {

/** Data fields */ private Student[]

classList;

private Faculty faculty;

/** Constructors */

/** Methods */

}

public class Faculty {

/** Data fields */

private Course[]

courseList;

/** Constructors */

/** Methods */

}

CSCI 1302 – Object-Oriented Modeling 14

Association

• Illustrated using a solid line with optional label (“Take” and “Teach”)

• Optional triangle to indicate direction of relationship

• Each class may have a role name (“Teacher”)

• Each class may specify multiplicity• * - unlimited, m..n is an inclusive range• Usually represented as a data field

CSCI 1302 – Object-Oriented Modeling 15

Aggregation and Composition

• Aggregation is a special form of association that represents an ownership relationship between two classes

• Models “has-a” relationships• Object may be owned by other

aggregated objects, if exclusively owned, that is composition

• Empty diamond – aggregation, filled diamond – composition

CSCI 1302 – Object-Oriented Modeling 16

Aggregation and Composition

Name Address Person

Composition Aggregation

public class Name {

/** Data fields */

/** Constructors */

/** Methods */

}

public class Person {

/** Data fields */

private Name name;

private Address address;

/** Constructors */

/** Methods */

}

public class Address {

/** Data fields */

/** Constructors */

/** Methods */

}

CSCI 1302 – Object-Oriented Modeling 17

Inheritance

• Models “is-a” relationship between two classes

• “Strong is-a” describes direct inheritance, extending a class

• “Weak is-a” describes a class that has certain properties, implementing an interface

Person

Student Comparable

public class Student extends Person

implements Comparable {

}

CSCI 1302 – Object-Oriented Modeling 18

Case Studies

• Model the application in terms of cooperative objects

• Building an object-oriented system1. Identify classes for the system2. Describe the attributes and methods

in each class3. Establish relationships among classes4. Create classes

CSCI 1302 – Object-Oriented Modeling 19

Designing a Class

• Class should describe a single entity, and all the class operations should fit together to support a coherent purpose

• Used by many different customers, should be able to customize through properties and methods

• Designed for reuse (see p. 358)• Should provide a public no-arg

constructor, override equals and toString

CSCI 1302 – Object-Oriented Modeling 20

Using Visibility Modifiers

• Class can present two contracts: one for users, one for extenders

• Make fields private and accessor/mutator methods public if intended for users

• Make fields or methods protected if they are intended for extenders

• Use private to hide data from direct access by clients

CSCI 1302 – Object-Oriented Modeling 21

Using the static Modifier

• A shared property among all instances of a class should be declared static

• Non-instance specific methods should be declared as a static method

CSCI 1302 – Object-Oriented Modeling 22

Using Inheritance or Composition

• Inheritance – “is-a” relationshipApple is a Fruitpublic class Apple extends Fruit

• Composition – “has-a” relationshipPerson has a namepublic class Person { private Name name;}

CSCI 1302 – Object-Oriented Modeling 23

Using Interfaces or Abstract Classes

• Both used to generalize common features

• “Strong is-a” relationship that clearly describes a parent-child relationship should be modeled using classes

• “Weak is-a” or “is-kind-of” relationship indicates an object has a certain property and should be modeled using interfaces

CSCI 1302 – Object-Oriented Modeling 24

Framework-Based Programming

• Classes and interfaces provided by the Java API allow developers to harness the power of the framework

• GUI API established a framework for developing GUI programs

• Using these classes and interfaces to create applications is framework-based programming