Object-Oriented Design and Programming (Java). 2 Topics Covered Today 1.2 Designing Classes –1.2.5...

Preview:

Citation preview

Object-Oriented Design and Programming (Java)

2

Topics Covered Today

• 1.2 Designing Classes– 1.2.5 Modeling Classes – 1.2.6 Modeling the Library System

3

Modeling Classes

• 1. Identify classes of objects from the system specification.

• 2. Identify relationships between classes.

• 3. Identify attributes of each class.

• 4. Identify methods of each class.

• 5. Model system using UML.

4

Identify Classes and Objects

• An easy way to identify classes is to analyze the textual description in the system specification.

• In textual analysis, the nouns and the noun phrases often indicate objects and their classes. – Singular nouns ("book," "library catalog," and "client")

and plural nouns ("users," "books," and "accounts") indicate classes.

– Proper nouns ("the ACME Bank") and nouns of direct reference ("the person that owned the account") indicate objects.

ibm
专用名词ACME

5

Example

• In banking system, a client is a person that has one or more accounts.

• John Smith has a checking account in CITI bank.

6

Steps for Identify Classes (1)

• List all the nouns in the specification. • Prune the list:

– Convert plural nouns to their singular form. In an object model, class names are singular.

– Eliminate nouns that represent objects. Replace them with generic nouns. For example, use "client" instead of "John Smith."

– Eliminate vague nouns. – Eliminate nouns that are class attributes.

• Group the synonyms and then choose the best name for the class from the group. – For example, "user" and "client" are synonyms. In a bank

system, the best name is "client" because the system may have two types of users: the clients and the bank's employees.

MC SYSTEM
修剪
MC SYSTEM
模糊的

7

Steps for Identify Classes (2)

• Select the classes that are relevant to the system. • Look for more relevant classes.

– Physical things. For example, "person," "book," and "computer."

– Roles played by persons or organizations. For example, "employer" and "supplier."

– Objects that represents an occurrence or event. For example, "system crash," "flight," and "mouse click."

– Objects that represent a relationship between other objects in the model. For example, "purchase" (related to "buyer," "seller," and "merchandise") and "marriage" (related to "man" and "woman").

8

Steps for Identify Classes

– People who carry out some function. For example, "student" and "clerk."

– Places. For example, "library," "classroom," and "bank."

– Collections of objects, people, resources, or facilities. For example, "catalog" and "group."

– Concepts or ideas that are intangible. For example, "money" and "bank account."

9

Identify Relationships Between Classes

• Create an n x n table where n is the number of classes. Label the rows and columns with the class names.

In banking system, a client is a person that has one or more accounts.

  Person Client Account

  Person      

 Client      

  Account      

10

Identify Relationships

• Identify the specialization/generalization relationships (识别具体化 /概括化关系 )– For each cell in the row A and column B, ask the following

questions: • Is an instance of class A an instance of class B? • Is an instance of class B an instance of class A?

– If the answer to both questions is yes, then the class names might be synonyms.

– If the answer to the first question is yes, then class A is a specialization of class B. Mark the cell in the row A and column B with an S.

– If the answer to the second question is yes, then class A is a generalization of class B. Mark the cell in the row A and column B with a G.

11

Identify Relationships

Person Client Account

  Person    

 Client    

  Account      

AB

Is an instance of class A an instance of class B?

Is an instance of class B an instance of class A?

G

S

12

Identify Relationships

• Identify the association relationships– Evaluate each cell in the row A and column B:

• If there is no association between class A and class B, mark the cell with an X.

• If there are one or more associations between class A and class B, then insert the association attributes. For example, "pilot," "wife," "ownedAccounts," and "clients."

13

Identify Relationships

Person Client Account

  Person x  G x 

 Client S x  

  Account  x    x

AB

x

ownedAccounts

14

Identifying Attributes

• Look for adjectives and possessive phrases such as "the X of Y" and "Y's X" in the system specification. – For example, "number of the account" and "client's

name."

• Use your knowledge of the application domain to define the set of attributes needed for the system being developed.

15

Identifying Attributes

• Person : name

• Client : account

• Account : numOfAccout , password , balance

16

Identify Methods

• To identify behaviors, look for verbs. – The client deposits money into the account.– The client checks account balance.

• Use your knowledge of the application domain to define the set of methods needed for the system being developed. – Create and initialize new instances. – Set and get values of attributes. – Load to and save from persistent storage. – Perform calculations using an object's values. – Output or display a result.

• If there are any collections held by the object, include the methods needed to add, remove, and access elements of these collections.

17

Identify Methods

• Person:– getName()– setName()

• Client:– getAccount()– setAccount()

• Account:– deposit()– withdraw()– getBalance()

18

Modeling Using UML

• The steps to produce a UML class diagram are the following: – Use class notation to represent classes. Include

attributes and methods. – Use link notation to describe association and

specialization/generalization relationships between classes.

– For associations, specify the multiplicity and the name of the attribute associated with the relationship.

19

Modeling Using UML

20

Topics Covered Today

• 1.2 Designing Classes– 1.2.5 Modeling Classes – 1.2.6 Modeling the Library System

• 2.1 Implementing Classes– 2.1.5 Implementing the Library System

21

Specification of the Library System (1)

• The library system tracks the items checked out by borrowers.(图书馆管理系统记录读者的借阅记录 )

• The system contains a catalog of the items owned by the library. There are two kinds of catalog items: books and recordings. All catalog items are identified by a unique code. (If the library owns several copies of the same book or recording, each copy has a unique code.) The information for each item includes title, year, and availability. An item is available if it is not checked out. (该系统有项目目录,所有的目录项目使用唯一码标识,目录项有两个种类(如果同一本书或记录有多个拷贝,每本图书或记录有一个唯一码):书和记录(磁带或CD)。每个项目包括的信息有名称、年、是否借出。如果没有借出,则该项目可被读者借阅。 )

22

Specification of the Library System (2)

• In addition: – The information for a book includes the author and number of

pages.(一本书包含作者和页数的信息。 )

– The information for a recording includes the performer and format (CD or tape).(一个记录有表演者和格式 (CD 或磁带 )的信息。 )

• The system contains a database of the borrowers. Each borrower has a unique identification code in addition to a name. The system maintains a list, for each borrower, of the catalog items checked out.( 系统有一个读者库,每个读者除了一个名字之外有一个唯一标识符。系统维护每个读者的借阅书目。 )

23

Specification of the Library System (3)

• In the library system, the user should be able to:– Display the catalog by listing the code, title, and availability of

each item.(列出每个项目的编号、主题和每个项目是否可借; )

– Display a catalog item.(显示一个目录项目; )– Display the borrowers by listing the identification code and

name of each borrower.(列出借阅证号和借阅者的名字 )– Display the catalog items checked out by a borrower.(显示借阅者借出的项目 )

– Check out a catalog item by adding the item to borrower‘s list of borrowed items.(增加一条借阅记录 )

– Check in a catalog item by removing the item from borrower‘s list of borrowed items.(删除一条借阅记录 )

24

Identifying Classes

• List the nouns in the system specification:

library system book CD

items recording tape borrowers

copy database system Information

borrower catalog item identification code

library title name kinds

year list catalog items

availability user books author

(borrower)code

audio items number of pages

list of borrowed items

(item)code performer copies format

25

Identifying Classes

• Prune the list by eliminating the following nouns:(item) code, title, year, availability

attributes of a catalog item.

author, number of pages attributes of a book.

performer, format attributes of an audio item.

CD, tape values for the attribute format.

name, identification code, (borrower) code

attributes of a borrower.

user an element outside the library system.

information a generic term for the class attributes.

kinds not relevant to the system.

26

Identifying Classes

• Group the synonyms and then choose the best name for the class:

CatalogItem items, catalog items, copies, copy, item

LibrarySystem library system, system, library

Borrower borrowers, borrower

Book books, book

Recording recordings, recording

BorrowedItems list, list of borrowed items

Catalog

BorrowerDatabase

27

Identifying Relationships Library

SystemCatalog

Catalog

ItemBook Recording Borrower

Borrower

Items

Borrower

Database

Library

Systemcatalog borrowerDB

Catalog item

Catalog

ItemG G

Book S

Recording S

BorrowerborrowerIte

m

Borrower

Itemsitems

Borrower

Databaseborrower

28

Identifying Attributes

LibrarySystem catalog, borrowerDB

Catalog items

CatalogItem code, title, year, available

Book author, numberOfPages

Recording performer, format

Borrower name, id, borrowedItems

BorrowerItems items

BorrowerDatabase borrowers

29

Identifying Methods

LibrarySystem

displayCatalog()displayCatalogItem()displayBorrowerDatabase()displayBorrower()checkIn()checkOut()

30

Identifying Methods

Catalog

addItem(item:CatalogItem)getItem(index:int):CatalogItemgetNumberOfItems():intgetItem(code:String):CatalogItem

31

Identifying Methods

CatalogItem

getCode():StringgetTitle():StringgetYear():intisAvailable():booleansetAvailable(value:boolean)

32

Identifying Methods

BookgetAuthor():String

getNumberOfPages():int

RecordinggetPerformer():StringgetFormat():String

BorrowergetId():StringgetName():StringgetBorrowedItems():BorrowedItems

33

Identifying Methods

BorrowerItems

addItem(item:CatalogItem)removeItem(item:CatalogItem)getItem(index:int):CatalogItemgetItem(code:String):CatalogItemgetNumberOfItems():int

34

Identifying Methods

BorrowerDatabase

addBorrower(borrower:Borrower)getBorrower(index:int):BorrowergetNumberOfBorrowers():intgetBorrower(id:String):Borrower

35

Modeling Using UML

36

Part of Class Diagram

Recommended