Lecture 7 - Modularity + Sequence Diagram
Lecture 7 - Modularity + Sequence Diagram
Lecture 7
OOP DESIGN
Class design
• Separation of concerns
– A class should represent a single concept
(i.e. it should provide a single abstraction)
• Completeness
– For YOUR code, avoid unnecessary work
(you aren’t gonna need it)
– Leave TODOs for what you want to add later
• Consistency
– Consistency in names, param/returns, ordering, and
behavior
Class documentation
• Keep internal and external documentation
separate
• External (design specification): /** ... */
– What clients need to know about your class
– e.g. pre/postconditons, side effects, warnings & notices
• Internal (implementation notes): //
– What programmers (incl. yourself) need to know
– Tricky parts of the code
– Loop invariants, mid conditions...
Class documentation
public class HinhTron {
/** (Internal)
* Demo sự giống nhau giữa Thuộc tính và Biến
* nếu như không có thành phần khả_năng_truy_cập trước
* các thuộc tính
*/
// Thuộc tính (Internal)
final float PI = 3.14f;
float r;
float cv;
float dt;
void tinhChuVi() {
// Thuộc tính
float banKinh = 10;
cv = 2 * PI * banKinh;
}
}
Class cohesion
• Do one thing
– Observe or mutate, but not both!
• Compute a value but let client decide what
to do with it
– Don’t print in the same method that generates data
(This locks your code into a text representation)
– Return the data and let the views display it
(flexibility, reusability and separation of concerns, again)
2. Inconsistency
setFirst(int index, String value)
setLast(String value, int index)
Introduction to
Lifeline
Message Activation
Elements of a sequence diagram (1)
• Objects: represented by boxes at top of
diagram
• Lifeline: the time during which an object exists
• Messages: means by which objects
communicate with each other
Elements of a sequence diagram (1)
• Activation: the time period during which an
object performs an operation
• Synchronous message: a type of message in
which the caller has to wait for the receiving
object to finish executing the called operation
before it can resume execution itself
Elements of a sequence diagram (3)
• Simple message: a message that transfers
control from the sender to the recipient
without describing the details of the
communication
• Asynchronous message: a message in which
the sender does not have to wait for the
recipient to handle the message
Use case diagram of a school registration system
Written use case «Register for classes»
Normal flow:
an open course
with prerequisites