Vous êtes sur la page 1sur 40

Object-oriented Analysis and Design

S.Balaji

4/15/2012

Introduction
OOAD: object-oriented analysis and design Class and object concepts Discovering classes CRC card Word problem to classes Classes and relationships Inheritance and polymorphism OOP: Object-oriented programming in Java At the end of this class you should be able to analyze a problem, design a OO solution and implement it in Java programming language
4/15/2012 2

Object-Oriented Principles
OOP

Encapsulation (class concept) -- Information Hiding -- Interface and Implementations -- Standardization -- Access Control mechanisms (private /public etc.)

Inheritance

Polymorphism

-- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems

-- Many forms of same function -- Abstract Methods -- Abstract Classes

4/15/2012

What is an Object?
Object-oriented programming supports the view that programs are composed of objects that interact with one another. How would you describe an object? Using its characteristics (has a ----?) and its behaviors (can do ---?) Object must have unique identity (name) : Basketball, Blue ball Consider a ball: Color and diameter are characteristics (Data Declarations) throw, bounce, roll are behaviors (Methods)

4/15/2012

Classes are Blueprints


A class defines the general nature of a collection of objects of the same type. The process creating an object from a class is called instantiation. Every object is an instance of a particular class. There can be many instances of objects from the same class possible with different values for data.
4/15/2012 5

Example

objects

Object References

redRose

class Rose blueRose

class
4/15/2012 6

Inheritance Hierarchy
Foo d eat( )

Food Hierarchy

IceCream eat( )

Spagh etti eat( )

Pi zza eat( )

eat() is an example of polymorphic operation.


Obj ect equ al s( ) tostri ng ( ) cl one( )

(Java) Object Hierarchy


equals(), clone() and toString() illustrate sub-type polymorphism

Sal ary equ al s( ) toStri n g( ) cl one( )

Autom obi l e equ al s( ) cl one( ) toStri n g( )

Fam i l yT ree equ al s( ) toStri n g( ) cl one( )

4/15/2012

Polymorphism (subtype)
Consider a class Food. What can you do with Food? What does it have? Consider specific food items Ice Cream, Spaghetti and Pizza. How will you eat these? (Invoke eat() operation on objects of these classes)? eat() operation is polymorphically invoked depending on the type of the item it is invoked on.
4/15/2012 8

Use-case Analysis
Use case analysis involves reading and analyzing the specifications, as well as discussing the system with potential users of the system. Actors of the LMS are identified as the librarians and borrowers. Librarians directly interact with the system whereas borrowers interact with the system through the librarian.
4/15/2012 9

Use-case Diagram for Librarian


Librarian

addTitle

removeUpdateTitle

addItem

removeUpdateItem

addBorrower

4/15/2012

removeUpdateBorrower

10

Classes
OO paradigm supports the view that a system is made up of objects interacting by message passing. Classes represent collection of objects of the same type. An object is an instance of a class. A class is defined by its properties and its behaviors. A class diagram describes the static view of a system in terms of classes and relationships among the classes.
4/15/2012 11

Example (Take 2)
objects
Object References

redRose

class Rose blueRose

class
4/15/2012 12

Class Diagram : Automobile


Automobile public: seat seatBelt accelerator private: sparkPlugs gear protected: gloveCompartment public: startEngine brake protected: transmission private: fuelInjection

4/15/2012

13

Automobile Class Using Rational Rose Tool


Automobile seat seatBelt acceleratorPedal sparkPlugs gear gloveCompartment startEngine( ) brake( ) transmission( ) fuelInjection( )
4/15/2012 14

Access Control
Public, protected, private Public properties and behaviors are available to any other object to use/invoke Private: available only within the objects. Protected: available within the objects and to the class hierarchy inherited from the class. (We will discuss more about this when dealing with OO concept Inheritance.)
4/15/2012 15

Relationships
Typically an application consists of many related classes. Commonly used relationships include: associations, aggregations, and generalizations.

4/15/2012

16

Association
An association is a connection between classes, a semantic connection between objects of classes involved in the association. Association typically represents has a or uses relationships. Indicated by a line,

sometimes with arrow indicating unidirectional relationship, adorned by the name of the relation, and the ends of the line adorned by cardinality of relationship and optionally by the roles connected to each class.
17

4/15/2012

Association : Examples
Person Uses Computer

A person uses a computer.

Person

Owns

0..*

Car

A person may own many (zero..many) cars.

4/15/2012

18

Roles in Association
Person
driver

drives
company car

Car

A person (driver) drives a (company) car.

wife

Person
husband

married to
4/15/2012 19

Aggregation
Aggregation represents a relation contains, is a part of, whole-part relation. Indicated by a line adorned on the whole by a hollow diamond

Along with name of relationship and Cardinality.

4/15/2012

20

Aggregation: Example
League contains Team
*

Membership aggregation: A league is made up of Many teams.


4

wheel

made of

Auto
1

engine

Strong aggregation.

part
21

4/15/2012

Generalization
Generalization is a relationship between a general and a specific class. The specific class called the subclass inherits from the general class, called the superclass. Public and protected properties (attributes) and behaviors (operations) are inherited. Design representation inheritance OO concept.
4/15/2012 22

Generalization: Symbol
It represents is a relationship among classes and objects. Represented by a line with an hollow arrow head pointing to the superclass at the superclass end.

4/15/2012

23

Generalization: Example
Vehicle

Car

Boat

Truck

4/15/2012

24

Combined Example
Person

drives

0..*

Vehicle

Car

Boat

Truck

4/15/2012

25

Static Analysis: Initial Class Diagram


Item 0..* Title

0..1 LoanTransaction 0..*

0..* Reservation 0..*

BookTitle

MagazineTitle

Borrow er

Objects of these classes are all persistent data (in a Database)

4/15/2012

26

Dynamic Analysis
Borrow Item use case using Sequence Diagram Add Title use case using Collaboration diagram Add Item using Activity diagram Reservation state diagram

4/15/2012

27

Borrow Item: Sequence Diagram


: Borrower Bison : LMS : Title : Borrower : Loan Transaction : Item 1: findTitle ( ) 2: find ( )

3: findItem ( ) 4: searchItem ( )

5: identifyBorrower ( ) 6: findBorrower ( ) 7: createLoanTrans ( )

4/15/2012

28

Add Title: Collaboration Diagram


Assuming that add title implies addi ng an item 1: create ( ) 3: addItem ( ) name, ISBN id Objid : Title

: Librarian ObjId,id titleObj

2: setItem ( )

: Item 4: storeTi tle ( )

itemObj 5: storeItem ( )

DB : DB

4/15/2012

29

Add Item: Activity Diagram


Title Item Database
createItem

setItem

addToTitle

updateDatabase

4/15/2012

30

Component Diagram
Business Package + Item + Loan + Title + Borrower inf ormation + Book Title + Reserv ation + Magazine Title

GUI Package + Lend Window + Return Window + Reserv ation Window + Maintenance Window

4/15/2012

31

Elements of a Class
class data declarations (variables, constants) methods

header

header

body

modifiers, type, name

parameters

variables, constants

statements

selection

repetition

others

assignment
4/15/2012 32

Class Structure
class

variables constants

methods

4/15/2012

33

Defining Methods
A method is group of (related) statements that carry out a specified function. A method is associated with a particular class and it specifies a behavior or functionality of the class. A method definition specifies the code to be executed when the method is invoked/activated/called.
4/15/2012 34

Method Invocation : semantics


Operating System 1 2 Main method Rect.Area(.) 8 7 1. OS to main method 2. Main method execution 3. Invoke Area 4. Transfer control to Area 5. Execute Area method 6. Return control back to main method 7. Resume executing main 8. Exit to OS

3 4

Area method

4/15/2012

35

Visibility Modifiers
type Method/variable name

public

protected

nothing DEFAULT

private static

To indicate class method/ variable

nothing DEFAULT To indicate object method/ variable


36

4/15/2012

..Modifiers (contd.)
private : available only within class nothing specified : DEFAULT: within class and within package protected : within inherited hierarchy (only to sub classes) public : available to any class.

4/15/2012

37

Inheritance
Inheritance is the act of deriving a new class from an existing one. A primary purpose of inheritance is to reuse existing software. Original class used to derive a new class is called super class and the derived class is called sub class. Inheritance represents is a relationship between the superclass and the subclass.
4/15/2012 38

Modifers
Visibility modifiers: private, public, protected Protected modifier is used when the entity needs to be available to the subclass but not to the public.

4/15/2012

39

Next Steps
Develop a multi-class java application Develop a application with graphical user interface Develop the solution for LMS Where can you get more info?
http://www.netbeans.org/kb/trails/java-se.html

4/15/2012

40

Vous aimerez peut-être aussi