Upload
sharon-bradley
View
266
Download
0
Tags:
Embed Size (px)
Citation preview
Object-Oriented Programme Object-Oriented Programme 11
SSD3: Object-Oriented Programming and SSD3: Object-Oriented Programming and Design Design
Object-Oriented Programme Object-Oriented Programme 22
1.1 Review of Java Basics 1.2 Java Application 1.3 Designing Classes
Unit 1. Class DesignUnit 1. Class Design
Object-Oriented Programme Object-Oriented Programme 33
1.3 Designing Classes1.3 Designing Classes
Contents 1.3.1 UML Class Diagrams 1.3.2 Relationships Between Classes 1.3.3 Common Class Structures 1.3.4 UML with Eclipse 1.3.5 Modeling Classes 1.3.6 Modeling the Library System
Assessments Practical Quiz 3 Practical Quiz 4 Multiple-Choice Quiz 2 Exercise 2
Object-Oriented Programme Object-Oriented Programme 44
1.31.3.1 UML Class Diagrams .1 UML Class Diagrams
Introduction Class Notation
Object-Oriented Programme Object-Oriented Programme 55
IntroductionIntroduction
The Unified Modeling Language (UML, 统一建模语言 ) is a graphical language for modeling the static and dynamical aspects of a system.
给系统的静态与动态特征建模 unifies the best engineering practices for modeling
systems 集合了最好的工程实践方法 defines different graphical diagrams that provide multiple
perspectives of a system under development 多个视图 UML is a unification of the methodologies developed by
Grady Booch, James Rumbaugh, and Ivar Jacobsen. The Object Management Group (OMG), a not-for-profit
organization that Promotes the use of object-oriented technology, has responsibility for the further development of the UML
standard.
Object-Oriented Programme Object-Oriented Programme 66
UMLUML
UML(Unified Modeling Language) 是下面这些最好的建模方法中最好部分的集成 :
◇ 业务流程模型 (Work Flow)
◇ 对象建模方法 ◇ 软构件建模思想
UML 是一种用可视化方法对软件系统进行描述、实施和说明的标准语言。
支持用不同实现技术进行的软件开发全过程。
Object-Oriented Programme Object-Oriented Programme 77
UML DiagramsUML Diagrams
Use case diagram ( 用例图 ) Class diagram ( 类图 ) Object diagrams ( 对象图 ) Behavior diagrams:
Statechart diagram ( 状态图 ) Activity diagram ( 活动图 ) Interaction diagrams:
Sequence diagram ( 顺序图 ) Collaboration diagram ( 协作图 )
Implementation diagrams: Component diagram ( 组件图 ) Deployment diagram ( 部署图 )
Object-Oriented Programme Object-Oriented Programme 88
UMLUML 的发展的发展
Object-Oriented Programme Object-Oriented Programme 99
对对 UMLUML 做出贡献者做出贡献者
Object-Oriented Programme Object-Oriented Programme 1010
UMLUML 简介简介
Use CaseDiagramsUse Case
Diagrams用例图
ScenarioDiagramsScenario
Diagrams协作图State
DiagramsStateDiagrams构件图
ComponentDiagramsComponent
Diagrams部署图
StateDiagramsState
Diagrams对象图
ScenarioDiagramsScenario
Diagrams状态图
Use CaseDiagramsUse Case
Diagrams序列图
StateDiagramsState
Diagrams类图
活动图
UML
Object-Oriented Programme Object-Oriented Programme 1111
◇ 用例图用于显示若干角色以及这些角色与系统提供的功能之间的连接关系。
用例是系统提供的功能的描述
Object-Oriented Programme Object-Oriented Programme 1212
◇ 类图表示系统中的类和类与类之间的关系
它是对系统静态结构的描述
Object-Oriented Programme Object-Oriented Programme 1313
◇ 序列图
用来反映若干个对象之间的动态协作关系,也就是随着时间的推移,对象之间是如何交互的
Object-Oriented Programme Object-Oriented Programme 1414
◇ 协作图描述对象间的协作关系,协作图跟序列图相似,显示对象间的动态合作关系。
如果强调时间和顺序,则使用序列图;
如果强调上下级关系,则选择协作图。
这两种图合称为交互图。
Object-Oriented Programme Object-Oriented Programme 1515
◇ 状态图描述类的对象所有可能的状态以及事件发生时状态的转移条件。
通常,状态图是对类图的补充
Object-Oriented Programme Object-Oriented Programme 1616
◇ 活动图描述满足用例要求所要进行的活动以及活动间的约束关系,有利于识别并行活动
Object-Oriented Programme Object-Oriented Programme 1717
◇ 构件图描述代码构件的物理结构及各构件之间的依赖关系
Object-Oriented Programme Object-Oriented Programme 1818
◇ 部署图
部署图定义系统中软硬件的物理体系结构
Object-Oriented Programme Object-Oriented Programme 1919
Class Notation Class Notation 类的表示符号类的表示符号 A class
represented by a rectangle with three compartments. 分为三个部分的矩形框
The first compartment contains the name of the class 类名 ;
the second compartment describes the attributes of the class; 类的属性
third compartment describes the methods of the class.类的方法
Object-Oriented Programme Object-Oriented Programme 2020
Sample Sample
class Employee 类符号的多种形式 Representation Abbreviated representation Shortest form Essential representation
Object-Oriented Programme Object-Oriented Programme 2121
1.31.3.2 Relationships Between Classes .2 Relationships Between Classes
Associations 关联 One-way and Two-way Associations 单向与双向关
联 Multiplicity 多重性 Aggregation 聚合 Specialization/Generalization 具体化 / 抽象化
Object-Oriented Programme Object-Oriented Programme 2222
Associations Associations 关联关联 An association represents the relationship between
two or more classes. 类与其它类存在某种联系 A binary association is a relationship between two
classes. 二元关联An object of a class requires an object of another
class to do its work. 教师要求学生提交作业 In UML, a binary association is represented by a
solid line (实线) that connects the two classes. In a classroom, a professor instructs students.
In a bank system, clients hold bank accounts.
Object-Oriented Programme Object-Oriented Programme 2323
One-Way and Two-Way Associations One-Way and Two-Way Associations
An association can be a one-way or two-way relationship. A one-way association indicates 单向关联
the direction in which one can navigate from an object of one class to an object of another class. 学生看电影
A two-way association indicates 双向关联 bi-directional navigation between objects of two classes. 教学互动
difference one-way association : the first class has a reference( 引
用 ) to an object of the second class, but the second class does not have a reference to an object of the first class.
two-way association : each class contains a reference to an object of the other class.
Object-Oriented Programme Object-Oriented Programme 2424
UML samplesUML samples
UML indicates a one-way association with an arrow at the end of the association line.
The attribute in the first class that contains a reference to an object of the second class is also written at the end of the line.
One-way association between classes Car and Engine indicates that an engine is part of a car
Object-Oriented Programme Object-Oriented Programme 2525
samplessamples
the relationship between the classes Country, Government, and Capital the class Country has a reference to an object of
class Governmentthe class Country a reference to an object of
class Capital
Object-Oriented Programme Object-Oriented Programme 2626
A class can contain A class can contain more than one associationmore than one association with another class with another class
Two associations between classes Flight and Pilot, one association with the attribute pilot and another with the attribute coPilot.
如航班, 飞行员与助驾
Object-Oriented Programme Object-Oriented Programme 2727
Multiplicity Multiplicity 多重性多重性 Multiplicity
indicates the number of instances of a class that may be associated with a single instance of another class. 对象关联时的数量特征
one car has four tires and one engine one client holds one or more bank accounts.
The multiplicity can be specified with a single integer a range n..m, where n is the lower limit and m is the upper limit. “ * “ used to denote no upper limit.
The following are the most common multiplicities: 0..1 Zero or one instance 0..* or * Zero or more instances 1 Exactly one instance 1..* One or more instances
Object-Oriented Programme Object-Oriented Programme 2828
One-to-one Association One-to-one Association
In a one-to-one association, exactly one instance of each class is related with exactly one instance of the other class.
Object-Oriented Programme Object-Oriented Programme 2929
One-to-many Association One-to-many Association
In a one-to-many association between classes A and B, one instance of class A may be related with
many instances of class B, but one instance of class B is related with exactly
one instance of class A.
Object-Oriented Programme Object-Oriented Programme 3030
Many-to-many Association Many-to-many Association
In a many-to-many association between classes A and B, one instance of class A may be related with
many instances of class Bone instance of class B may be related with
many instances of class A.
Object-Oriented Programme Object-Oriented Programme 3131
Aggregation Aggregation 聚合 聚合 Aggregation 聚合
Each instance of class A is composed by instances of class B.
An instance of class B is part of an instance of class A.
The instance of class A is called the aggregate( 聚合体 )
The instance of class B is called the component( 部件 ).
Object-Oriented Programme Object-Oriented Programme 3232
Aggregation in UMLAggregation in UML
Object-Oriented Programme Object-Oriented Programme 3333
Specialization/Generalization Specialization/Generalization 具象化具象化 // 抽象化抽象化 (( 具体化具体化 // 一般化一般化 ))
Specialization/Generalization represents the is-a relationship. “ 是一个……”关系a whale is-a mammala client is-a person.
If specialization/generalization allows class A to be defined as specialization of another class B. Class A is called the specialization class 具体类
Whale, client
class B the generalization class. 一般类Mammal, person
Object-Oriented Programme Object-Oriented Programme 3434
Specialization/Generalization Specialization/Generalization
Specialization/generalization represented in UML by adding a triangle next to
the generalization class:
Object-Oriented Programme Object-Oriented Programme 3535
specialization/generalization relationshipspecialization/generalization relationship
If there is a specialization/generalization relationship between classes A and B ,so if A is-a B, then all instances of class A are also
instances of class B. java 程序设计 / 英语 / 大学物理教材 书
An important consequence of this relationship is class A inherits all the features of class B. 继承All the attributes and methods of class B are also
attributes and methods of class A.
Object-Oriented Programme Object-Oriented Programme 3636
specialization/generalization relationshipspecialization/generalization relationship
The following class diagram represents the relationships Client is-a Person and Employee is-a Person:
Object-Oriented Programme Object-Oriented Programme 3737
Sample noteSample note
They are specialization/generalization relationshipsthe attributes of class Person are also attributes
of classes Client and Employee. Each instance of class Client contains the attributes
name, age, address , accounts (new attributes ); Each instance of class Employee contains the
attributes name, age, address, salary (new attributes ) , departments (new attributes )
Object-Oriented Programme Object-Oriented Programme 3838
Example of a system of multimedia files Example of a system of multimedia files
includes images, audio, and text files. The classes are defined as follows:
Object-Oriented Programme Object-Oriented Programme 3939
1.31.3.3 Common Class Structures.3 Common Class Structures
Introduction Collections Self-Containing Classes Relationship Loops
Object-Oriented Programme Object-Oriented Programme 4040
IntroductionIntroduction
Although the definition of classes and their relationships depends on the particular application, some class structures are common to many designs. 常用类结构
These class structures, which can be thought of as basic building blocks, can be composed to design complex systems. 像用各种预制件造高楼大厦
Object-Oriented Programme Object-Oriented Programme 4141
Collections Collections 集合集合 A collection models a
one-to-many relationship. Collections store many instances of one class. 同
一个类的多个实例 班级 - 学生
Object-Oriented Programme Object-Oriented Programme 4242
samplesample
The diagram uses a collection to represent the relationship
"one client holds one or more bank accounts"defines methods to handle the collection of
BankAccount instances. Each BankAccount instance in the collection has an
index indicating its location in the collection.
Object-Oriented Programme Object-Oriented Programme 4343
Self-Containing Classes Self-Containing Classes 自包含自包含 A class can have an association with itself.
the class contains attributes with references to objects of the same class.
Object-Oriented Programme Object-Oriented Programme 4444
exampleexample
Class Person in a genealogical tree ( 家族谱系树 )system.
The relationships "each person has one mother" and "each person has one father" are indicated with the attributes mother and father.
The attributes mother and father are references to objects of class Person.
Object-Oriented Programme Object-Oriented Programme 4545
Another exampleAnother example
The relationship "each employee has one boss." The diagram indicates that the boss of an
employee is another employee.
Object-Oriented Programme Object-Oriented Programme 4646
Relationship Loops Relationship Loops 关系循环关系循环 Self-containment can be found in relationships that
include two or more classes. 循环自包含
Object-Oriented Programme Object-Oriented Programme 4747
exampleexample
A file system. A file system has folders, and folders contain files or
more folders, or both. We can define a class Folder, which contains a collection
of FolderItem objects:
Object-Oriented Programme Object-Oriented Programme 4848
1.31.3.4 UML with Eclipse .4 UML with Eclipse Introduction Create Project Create Folder Create New Class Diagram Create Class BankAccount Create Class Person Create Class Client Create Specialization/Generalization Relationship Create Association Relationship Export Image Note:
We can also use JUDE Download from :
http://jude.change-vision.com/jude-web/download/index.html
Object-Oriented Programme Object-Oriented Programme 4949
1.31.3.5 Modeling Classes.5 Modeling Classes
Process of Modeling Classes Identifying Classes Identifying Relationships Identifying Attributes Identifying Methods Modeling Using UML
Object-Oriented Programme Object-Oriented Programme 5050
Process of Modeling ClassesProcess of Modeling Classes
Object-oriented design is 面向对象设计the process of building an object model for a
system to be developed. OOD 就是一个建模过程 An object model specifies 对象模型详细指明了
the classes attributesMethodsrelationships with other classes.
How to building an object model from a system specification( 系统规格说明 ) ?
Object-Oriented Programme Object-Oriented Programme 5151
The steps to create an object modelThe steps to create an object model
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. 用 UML 为系统建模
Object-Oriented Programme Object-Oriented Programme 5252
Identifying Classes Identifying Classes 找出类找出类 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” ) 专有名词nouns of direct reference ( “the person that
owned the account” ) indicate objects. 指代词
Object-Oriented Programme Object-Oriented Programme 5353
The steps for identifying the classes - 1The steps for identifying the classes - 11. List all nouns in the specification. 列出所有名词2. 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. 去掉特指变一般 use “client” instead of “John Smith.” “ 我去上 java 课”
Eliminate nouns that are class attributes. 去掉表示类属性的名词 If cannot identify the attributes of the noun, it is possible a class
attribute. 无法确定名词的属性,就可能是某类的属性3. Group the synonyms and then choose the best name for the
class from the group. 归纳同义词,起个恰当的名字 "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.
Object-Oriented Programme Object-Oriented Programme 5454
The steps for identifying the classes - 2The steps for identifying the classes - 2
4. Select the classes that are relevant to the system. Look for more relevant classes. Search the application domain
for: 在应用领域进行搜索,找到与系统相关的类 Physical things. “person,” “book,” and “computer.” 现实事物 Roles played by persons or organizations.“employer” and
“supplier.” 角色 Objects that represents an occurrence or event. “system
crash,” “flight,” and “mouse click.” 表示发生的事件的对象 Objects that represent a relationship between other objects in
the model. “purchase” (related to “buyer,” “seller,” and “merchandise”) and “marriage” (related to “man” and “woman”). 表示关联关系的对象
People who carry out some function. "student" and "clerk." Places. “library,” “classroom,” and “bank.” 场所 Collections of objects, people, resources, or facilities. “catalog”
and “group.” 目标、人员、资源、设施集合 Concepts or ideas that are intangible. 抽象名词 “ money” and
“bank account.”
Object-Oriented Programme Object-Oriented Programme 5555
The steps for Identifying Relationships The steps for Identifying Relationships 找出关联 找出关联 - 1- 1
1. Create an n x n table where n is the number of classes. Label the rows and columns with the
class names.
Object-Oriented Programme Object-Oriented Programme 5656
The steps for Identifying Relationships - 2The steps for Identifying Relationships - 2
2. 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?
Analyze the answer, If the answer both yes
the class names might be synonyms. the first is yes
class A is a specialization of class B. ( 子类关系 ) Mark the cell in the row A and column B with an
S. the second is yes, then
class A is a generalization of class B. ( 超类关系 ) Mark the cell in the row A and column B with a G.
Object-Oriented Programme Object-Oriented Programme 5757
The steps for Identifying Relationships - 3The steps for Identifying Relationships - 3
3. Identify the association relationships, evaluate each cell in the row A and column B: If no association between class A and class B 无关
mark the cell with an X. If one or more associations between class A and
class B, 存在某种关联性 insert the association attributes. “study," “buy,"
"ownedAccounts," and "clients."
Object-Oriented Programme Object-Oriented Programme 5858
The table told us …..The table told us …..
Shows the relationships "a client is a person that has one or more accounts." Indicates that Class Client is a specialization of class PersonClass Client has an association relationship with
class Account.
Object-Oriented Programme Object-Oriented Programme 5959
Identifying Attributes Identifying Attributes 找出属性找出属性 Look for adjectives and possessive phrases 所有格
"the X of Y" and "Y's X" in the system specification.
"number of the account" and "client's name.“
Use knowledge of the application domain to define the set of attributes needed for the system being developed. 问题领域的知识及经验,可从专业人员那里获取(因此设计开发前要详细调研)
Object-Oriented Programme Object-Oriented Programme 6060
Identifying Methods Identifying Methods 找出方法找出方法
To identify behaviors, look for verbs. "the client deposits money into the account“ indicates
that class Account should define a method deposit.
Use knowledge of the application domain to define the set of methods needed for the system being developed.
Object-Oriented Programme Object-Oriented Programme 6161
Identifying Methods Identifying Methods
除了以上的,还要利用开发者的经验 一般的应用系统都有的方法 (或系统行为 )
Create and initialize new instances. 创建、初始化实例 Set and get values of attributes. 访问属性值 Load to and save from persistent storage. 持久存储访问 Destroy instances. 销毁实例 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. 集合操作
Object-Oriented Programme Object-Oriented Programme 6262
Modeling Using UML Modeling Using UML
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. 多重性
Object-Oriented Programme Object-Oriented Programme 6363
1.31.3.6 Modeling the Library System .6 Modeling the Library System
Specification of the Library System Identifying Classes Identifying Relationships Identifying Attributes Identifying Methods Modeling Using UML
Object-Oriented Programme Object-Oriented Programme 6464
Specification of the Library System Specification of the Library System 规格说明规格说明 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.
Object-Oriented Programme Object-Oriented Programme 6565
Specification of the Library SystemSpecification of the Library System
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). 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.
Object-Oriented Programme Object-Oriented Programme 6666
Specification of the Library SystemSpecification of the Library System 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.
Object-Oriented Programme Object-Oriented Programme 6767
Identifying ClassesIdentifying Classes
First, we 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
Object-Oriented Programme Object-Oriented Programme 6868
Identifying ClassesIdentifying Classes Then, we prune the list by eliminating the following nouns:
(item) code, title, year, availability These nouns are attributes of a catalog item.
author, number of pages These nouns are attributes of a book.
performer, format These nouns are attributes of an audio item.
CD, tapeThese nouns are values for the attribute
format.
name, identification code, (borrower) code
These nouns are attributes of a borrower.
userThis noun is an element outside the library
system.
informationThis noun is a generic term for the class
attributes.
kinds This noun is not relevant to the system.
Object-Oriented Programme Object-Oriented Programme 6969
Identifying ClassesIdentifying Classes Next, we group the synonyms and then choose the best name
for the class from each group:
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
Object-Oriented Programme Object-Oriented Programme 7070
Finally, we have ……Finally, we have ……
classes that are relevant to the system
LibrarySystem Catalog CatalogItem Book RecordingBorrower BorrowedItems BorrowerDatabase
Object-Oriented Programme Object-Oriented Programme 7171
Identifying Relationships Identifying Relationships
table identifies the association and specialization/generalization relationships:
Object-Oriented Programme Object-Oriented Programme 7272
Identifying AttributesIdentifying Attributes
The following is the list of attributes for each class:
Class Attributes
LibrarySystem catalog, borrowerDB
Catalog items
CatalogItem code, title, year, available
Book author, numberOfPages
Recording performer, format
Borrower name, id, borrowedItems
BorrowedItems items
BorrowerDatabase borrowers
Object-Oriented Programme Object-Oriented Programme 7373
Identifying MethodsIdentifying Methods
The following is the list of methods for each class:
Class Methods
LibrarySystem
displayCatalog() , displayCatalogItem(),displayBorrowerDatabase(),displayBorrower(),checkIn(),checkOut()
Catalog addItem(item:CatalogItem)getItem(index:int):CatalogItem getNumberOfItems():Int getItem(code:String):CatalogItem
CatalogItem getCode():StringgetTitle():StringgetYear():intisAvailable():booleansetAvailable(value:boolean)
Object-Oriented Programme Object-Oriented Programme 7474
Identifying MethodsIdentifying Methods
The following is the list of methods for each class:
Class Methods
Book getAuthor():StringgetNumberOfPages():int
Recording getPerformer():StringgetFormat():String
Borrower getId():StringgetName():StringgetBorrowedItems():BorrowedItems
Object-Oriented Programme Object-Oriented Programme 7575
Identifying MethodsIdentifying Methods
The following is the list of methods for each class:
Class Methods
BorrowedItems addItem(item:CatalogItem)removeItem(item:CatalogItem)getItem(index:int):CatalogItemgetItem(code:String):CatalogItemgetNumberOfItems():int
BorrowerDatabase
addBorrower(borrower:Borrower)getBorrower(index:int):BorrowergetNumberOfBorrowers():intgetBorrower(id:String):Borrower
Object-Oriented Programme Object-Oriented Programme 7676
Modeling Using UMLModeling Using UML
The following is a class diagram for the library system: