Transcript CS 151: Object-Oriented Design
Object-Oriented Analysis and Design Phân tích thiết kế hướng đối tượng
Phân tích Chỉnh từ bài giảng của Ron Mak www.cs.sjsu.edu/~mak
Ye Olde Waterfall Model
Requirements Design Implementation Testing SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 2
The Waterfall Model
Đảm bảo mỗi pha hoàn thành 100% và tuyệt đối đúng trước khi tiến sang pha tiếp theo.
Big Design Up Front (BDUF) Cố định yêu cầu trước khi bắt đầu thiết kế.
Nếu không, sẽ phí công thiết kế cho các yêu cầu “sai”.
Dành thêm thời gian ở giai đoạn đầu để đảm bảo là các yêu cầu và thiết kế tuyệt đối đúng .
The waterfall model has been mostly discredited.
It doesn’t work!
But it’s still commonly practiced.
Requirements Design
Source: Wikipedia article on “Waterfall Model”
Implementation SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak Testing 3
Project Phases
Ngày nay, ta biết rằng quá trình phát triển phần mềm là một chuỗi các vòng lặp ( iteration) .
Mỗi vòng lặp là một “mini waterfall” bao gồm design, code, và test .
Những người ủng hộ Extreme Programming sẽ nói: design, test, code SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 4
Agile Software Development
Iterative and incremental development Requirements Mỗi iteration là một “mini waterfall”: Lập kế hoạch ( với requirement mới) Điều chỉnh thiết kế Thêm code mới unit and integration testing Iteration ngắn: chứ không tính theo tháng.
_ tính theo ngày và tuần Design Implementation Testing SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 5
Where Do Classes Come From?
Phân tích văn bản Tìm danh từ và động từ trong các use case.
Danh từ lớp Một số danh từ là các actor Động từ method Tên lớp nên là danh từ số ít như
Inventory
,
Instrument
,
InstrumentSpec
.
Các lớp sẽ hỗ trợ các hành vi như thế nào?
mà các use case mô tả Tập trung vào mức khái niệm, đừng vội nghĩ đến cách cài đặt.
_ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 6
Các loại lớp
Sự vật Ví dụ:
Instrument
,
InstrumentSpec
Agent doers of tasks.
Tên thường có tận cùng
“er”
hoặc
“or”
Ví dụ:
Scanner
,
Paginator
Các sự kiện và giao tác - Events and transactions Ghi lại mô tả những gì đã xảy ra hoặc cần làm trong tương lai Ví dụ:
MouseEvent
chuyển hoặc click.
ghi lại thời gian và vị trí mà chuột được di SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 7
Categories of Classes
Users and roles Stand-in for the actual users of the application.
Common in systems that are used by more than one person or where one person needs to perform distinct tasks.
Examples:
Administrator
,
Reviewer
System Model a subsystem or the overall system being built.
Typical methods: initialize, shutdown, read input System interfaces and devices Interfaces to the host operating system, file system, etc.
Foundation Typically the built-in classes. Examples:
String
,
Date
SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 8
Class Responsibilities
Các trách nhiệm case.
tương ứng với các động từ trong use Mỗi trách nhiệm chỉ nên thuộc về đúng một lớp.
Lỗi thường gặp: Gán trách nhiệm cho lớp không phù hợp.
Một lớp được gán quá nhiều trách nhiệm.
Về lí tưởng, mỗi lớp chỉ nên có một trách nhiệm chính duy nhất .
_ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 9
Class Responsibilities Example
class
Automobile
start()
stop()
changeTires() drive() wash() displayOilLevel() checkOil()
class
Automobile
start()
stop() displayOilLevel()
class
Driver drive()
Too many responsibilities!
class
CarWash
wash()
A
cohesive one thing
class does really well and does not try to be something else.
SJSU Dept. of Computer Science Spring 2013: September 3 class
Mechanic
changeTires() checkOil()
CS 151: Object-Oriented Design © R. Mak 10
Class Relationships: Dependency
Lớp
C
phụ thuộc lớp
D
: Có phương thức của lớp
C
dùng đến đối tượng thuộc
D
Ví dụ: đối tượng
Mailbox
sử dụng đối tượng
Message
.
Quan hệ phụ thuộc là bất đối xứng.
Lớp
Message
không biết sự tồn tại của lớp
Mailbox
.
Do đó, các đối tượng
Message Mailbox
.
không phụ thuộc các đối tượng Loose coupling – ghép cặp lỏng Tối thiểu hóa số quan hệ phụ thuộc.
Một cách để thiết kế chịu được các thay đổi trong tương lai.
_ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 11
Class Relationships: Aggregation
Class
C
aggregates class
A
: Objects of class
C
contains objects of class
A
over a period of time.
A special case of dependency.
The “ has-a ” relationship.
Example: An
Inventory
object has a list of
Instrument
objects.
Multiplicity 1:1 – Example: Each
Person StreetAddress
object.
object has a single 1:n – Example: Each
Inventory
multiple
Instrument
objects.
object has an array of SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 12
Class Relationships: Inheritance
Class
C
inherits from class
S
.
The “ is-a ” relationship.
All class
C
objects are special cases of class
S
objects.
Class
S
is the superclass of class
C
.
Class
C
is a subclass of class
S
.
An object of class
C
is an object of class
S
.
Note
Aggregation:
A
Mailbox
object has a
Message
object.
Inheritance:
is a
Message
A
ForwardedMessage
object.
object _ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 13
The CRC Technique
Class-Responsibilities-Collaborations (lớp – trách nhiệm – cộng tác) Một kĩ thuật hiệu quả để phát hiện các lớp, trách nhiệm, và quan hệ giữa các lớp.
CRC card là một thẻ Mô tả một lớp.
Liệt kê các trách nhiệm của nó.
Liệt kê quan hệ của nó với các lớp khác.
_ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 14
The CRC Technique
Example: class
Mailbox Responsibilities
Manage passcode Manage greeting Create new message
Mailbox Relationships
MessageQueue SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 15
The CRC Technique
1.
Viết tên mỗi lớp lên một thẻ.
2.
Phân công các trách nhiệm cho các lớp.
3.
Tìm quan hệ giữa các lớp và liệt kê tất cả các lớp mà mỗi lớp phụ thuộc.
4.
Đừng viết các phương thức hay các biến thực thể. Chỉ viết các trách nhiệm ở ngôn ngữ trừu tượng hóa cao.
_ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 16
UML Diagrams
A picture is worth a thousand words!
Thu nhận thông tin từ một hình ảnh đồ họa dễ hơn nhiều so với đọc một văn bản bằng chữ.
Thể hiện nội dung phân tích/thiết kế bằng UML diagram .
UML : Unified Modeling Language Có nhiều loại biểu đồ UML. Tạm thời, ta sẽ dùng Class diagrams Sequence diagrams State diagrams SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 17
UML Class Diagram
A class diagram has three compartments: Attributes : types
Class Name
Methods(parms : types) : return type SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 18
UML Class Diagram: Attributes and Methods
Chỉ liệt kê những thuộc tính và phương thức quan trọng nhất. Nếu một lớp có quá nhiều thuộc tính, hãy xem có thể gộp một số thuộc tính thành một lớp mới hay không.
Ví dụ: Một lớp có các thuộc tính đặc thù của nó.
Ngoài ra còn có name, street, city, state, và zip.
Hãy tạo lớp mới
Address
chứa các thuộc tính đó.
Khi đó lớp đó has an address.
SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 19
Example UML Class Diagram
Mailbox
newMessages : ArrayList
UML Class Diagram: Relationships
Relationships among classes using arrows. Dependency Aggregation Inheritance Composition Association Direct association Interface implementation SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 21
UML Class Diagram: Multiplicities
Multiplicity in a “has” relationship.
Sign
* 1..* 0..1
1
Purpose
Zero or more One or more Zero or one Exactly one SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 22
UML Class Diagram: Aggregation
Aggregation A “has a” relationship.
Đối tượng thành phần có thể tồn tại một cách độc lập với đối tượng chứa nó.
Example A mailbox has a set of messages.
A message can exist without a mailbox.
Therefore, a mailbox aggregates messages.
Mailbox 1 * Message
SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 23
UML Class Diagram: Composition
Composition A “has a” relationship.
Đối tượng thành phần về lôgic không thể tồn tại độc lập với đối tượng chứa nó.
Example A mailbox has a message queue.
The message queue cannot (logically) exist without a mailbox.
Therefore, a mailbox composes a message queue.
1 1 Mailbox MessageQueue
SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 24
Class Diagram Examples
What’s in the
frontend
package of a compiler?
SJSU Dept. of Computer Science Fall 2013: September 10 CS 151: Object-Oriented Design © R. Mak UML package diagram What information can you learn from the class diagrams?
Access control
+ public – private # protected ~ package From:
Writing Compilers and Interpreters, 3 rd ed.
, John Wiley & Sons, 2009.
25
Class Diagram Examples
, cont’d
Message handling in the front end of a compiler.
frontend
and
message
packages SJSU Dept. of Computer Science Fall 2013: September 10 CS 151: Object-Oriented Design © R. Mak 26
Class Diagram Examples
, cont’d
The
frontend
,
intermediate
, and
backend
packages.
SJSU Dept. of Computer Science Fall 2013: September 10 CS 151: Object-Oriented Design © R. Mak 27
Class Diagram Examples
, cont’d
Implement the abstract base classes
Parser Scanner
and with language specific subclasses.
SJSU Dept. of Computer Science Fall 2013: September 10 CS 151: Object-Oriented Design © R. Mak 28
UML Sequence Diagram
Biểu đồ lớp mang đặc điểm tĩnh.
Nó vẽ các lớp tồn tại trong suốt thời gian sống của hệ thống.
Biểu đồ tuần tự (sequence diagram) thể hiện các mối quan hệ động giữa các lớp tại thời gian chạy Nó mô tả tương tác giữa các đối tượng trong thời gian chạy.
_ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 29
UML Sequence Diagram
Withdraw cash
T I M E
Customer select notify display confirmation enter amount display bank ads Display notify Keypad notify Bank verify accept notify dispense cash Account Withdraw Cash Sequence Diagram SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 30
UML State Diagram
Tại thời gian chạy, trạng thái của một đối tượng được xác định bởi giá trị của các thuộc tính của nó.
Ở một số đối tượng, các trạng thái khác nhau có thể gây ra các hành vi khác nhau.
Ví dụ: một hệ thống thư thoại (voice mail system) Người gọi quay số đúng trạng thái "Connected" Nếu người gọi gõ một mã mở rộng đúng "Recording" trạng thái là Nếu trong giai đoạn "Recording“, người gọi có thể gõ passcode trạng thái là "Main menu“ _ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 31
UML State Diagram
Connected Change passcode
SJSU Dept. of Computer Science Spring 2013: September 3
Main menu
CS 151: Object-Oriented Design © R. Mak
Recording
32
Free UML Design Tools
StarUML: http://staruml.sourceforge.net/en/ Violet: http://horstmann.com/violet/ Dr. Horstmann’s product _ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 33
javadoc
The javadoc utility creates a set of HTML pages for your classes. It reads specially formatted comments in your code.
Delimit comments for javadoc by
/**
and
*/
It copies the first sentence of your comment to a summary table.
Write the first sentence carefully.
It should start with an uppercase letter and end with a period.
SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 34
javadoc
Javadoc comment example:
/** * Choose the test string with the highest score and return it.
* @param sequence the current sequence.
* @param * @return store the sequence store.
the chosen test string.
{ */ private String chooseString(CurrentSequence sequence, SequenceStore store) ...
}
Method parameter:
@param
paramname explantion Function return value:
@return
explanation If the method throws an exception:
@throws
exceptionname explanation SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 35
Run the javadoc Utility
On the command line:
javadoc
path
/*.java
You can also run javadoc from within NetBeans or Eclipse.
_ Demo SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 36
Program Design and javadoc
Write only the skeletons of your classes.
No implementation initially.
Just include javadoc comments.
The result is an outline summary of your design.
Example:
/** { * Choose the test string with the highest score and return it.
* @param sequence the current sequence.
* @param store the sequence store.
* @return the chosen test string.
*/ private String chooseString(CurrentSequence sequence, SequenceStore store) // To be implemented.
}
SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design © R. Mak 37