Download presentation
Presentation is loading. Please wait.
Published byBeverly Leonard Modified over 9 years ago
1
Object-Oriented Design and Programming (Java)
2
2 Topics Covered Today 1.2 Designing Classes –1.2.5 Modeling Classes –1.2.6 Modeling the Library System
3
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
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.
5
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
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.
7
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
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
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. PersonClientAccount Person Client Account
10
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
11 Identify Relationships PersonClientAccount Person Client Account A B 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
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
13 Identify Relationships PersonClientAccount Person x Gx Client Sx Account x x A B x ownedAccounts
14
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
15 Identifying Attributes Person : name Client : account Account : numOfAccout , password , balance
16
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
17 Identify Methods Person : –getName() –setName() Client : –getAccount() –setAccount() Account : –deposit() –withdraw() –getBalance()
18
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
19 Modeling Using UML
20
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
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
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
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
24 Identifying Classes List the nouns in the system specification: librarysystembookCD itemsrecordingtapeborrowers copydatabasesystemInformation borrowercatalogitemidentification code librarytitlenamekinds yearlistcatalogitems availabilityuserbooksauthor (borrower)co de audio itemsnumber of pages list of borrowed items (item)codeperformercopiesformat
25
25 Identifying Classes Prune the list by eliminating the following nouns: (item) code, title, year, availability attributes of a catalog item. author, number of pagesattributes of a book. performer, formatattributes of an audio item. CD, tapevalues for the attribute format. name, identification code, (borrower) code attributes of a borrower. useran element outside the library system. informationa generic term for the class attributes. kindsnot relevant to the system.
26
26 Identifying Classes Group the synonyms and then choose the best name for the class: CatalogItemitems, catalog items, copies, copy, item LibrarySystemlibrary system, system, library Borrowerborrowers, borrower Bookbooks, book Recordingrecordings, recording BorrowedItemslist, list of borrowed items Catalog BorrowerDatabase
27
27 Identifying Relationships Library System Catalog Item BookRecordingBorrower Items Borrower Database Library System catalogborrowerDB Catalogitem Catalog Item GG BookS RecordingS Borrower borrowerIte m Borrower Items items Borrower Database borrower
28
28 Identifying Attributes LibrarySystemcatalog, borrowerDB Catalogitems CatalogItemcode, title, year, available Bookauthor, numberOfPages Recordingperformer, format Borrowername, id, borrowedItems BorrowerItemsitems BorrowerDatabaseborrowers
29
29 Identifying Methods LibrarySystem displayCatalog() displayCatalogItem() displayBorrowerDatabase() displayBorrower() checkIn() checkOut()
30
30 Identifying Methods Catalog addItem(item:CatalogItem) getItem(index:int):CatalogItem getNumberOfItems():int getItem(code:String):CatalogItem
31
31 Identifying Methods CatalogItem getCode():String getTitle():String getYear():int isAvailable():boolean setAvailable(value:boolean)
32
32 Identifying Methods Book getAuthor():String getNumberOfPages():int Recording getPerformer():String getFormat():String Borrower getId():String getName():String getBorrowedItems():BorrowedItems
33
33 Identifying Methods BorrowerItems addItem(item:CatalogItem) removeItem(item:CatalogItem) getItem(index:int):CatalogItem getItem(code:String):CatalogItem getNumberOfItems():int
34
34 Identifying Methods BorrowerDatabase addBorrower(borrower:Borrower) getBorrower(index:int):Borrower getNumberOfBorrowers():int getBorrower(id:String):Borrower
35
35 Modeling Using UML
36
36 Part of Class Diagram
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.