360 likes | 373 Views
Learn how to identify classes and their attributes, methods, and relationships in object-oriented design and programming using Java. Explore techniques for modeling classes and creating effective class diagrams using UML.
E N D
Topics Covered Today • 1.2 Designing Classes • 1.2.5 Modeling Classes • 1.2.6 Modeling the Library System
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.
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.
Example • In banking system, a client is a person that has one or more accounts. • John Smith has a checking account in CITI bank.
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.
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").
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."
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.
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 Bwith 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 Bwith a G.
Identify Relationships B A G S Is an instance of class A an instance of class B? Is an instance of class B an instance of class A?
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."
Identify Relationships B A ownedAccounts x
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.
Identifying Attributes • Person:name • Client:account • Account:numOfAccout,password,balance
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.
Identify Methods • Person: • getName() • setName() • Client: • getAccount() • setAccount() • Account: • deposit() • withdraw() • getBalance()
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.
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
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)。每个项目包括的信息有名称、年、是否借出。如果没有借出,则该项目可被读者借阅。)
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.(系统有一个读者库,每个读者除了一个名字之外有一个唯一标识符。系统维护每个读者的借阅书目。)
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.(删除一条借阅记录)
Identifying Classes • List the nouns in the system specification:
Identifying Classes • Prune the list by eliminating the following nouns:
Identifying Classes • Group the synonyms and then choose the best name for the class: