object oriented development with uml class diagramclass diagram object oriented development with uml...

79
Class Diagram Object Oriented Development with UML By- Assoc. Prof. Rangsit Sirirangsi

Upload: others

Post on 19-Sep-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Class Diagram

Object Oriented Development with UML

By-

Assoc. Prof. Rangsit Sirirangsi

Page 2: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

What is a Class Diagram?

ใชสาหรบแสดงรายละเอยดชนดของคลาสตาง ๆ ทอยภายในระบบ ตลอดจน

แสดงถงความสมพนธชนดตาง ๆ ทเกดขน

● เปนมมมองแบบ static ของสวนประกอบตาง ๆ ของคลาสทถกประกาศไว

ถอเปนหวใจหลกของการวเคราะหและออกแบบระบบเชงวตถ

รายละเอยดทปรากฏอยภายในคลาสไดอาแกรมจะขนอยกบระดบของการ

พฒนาระบบ

Class Name

attribute: Type = initialValue....

method(arg list): return type....

What

How

Page 3: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Attributes

เปนขอมลทถกจดเกบไวโดยออปเจคทถกสรางขนจากคลาส และถอเปน

คณสมบตของแตละออปเจค

ชนดของตวแปรทกาหนดรวมกบแอททรบวตจะขนอยกบโปรแกรมภาษาท

ใช โดยปกตจะเปนชนดขอมลแบบพนฐาน (Built-in type) และชนดของตว

แปรทผใชกาหนดขน (User-defined type)

Visibility Name : Type = Default Value

+ : Public- : Private# : Protected

Attribute Name

SignatureAttribute Type

Attribute Default Value

Page 4: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Operations

ใชสาหรบจดการคาของแอทรบวตหรอใชสาหรบการกระทาอน ๆ

ทก ๆ ออปเจคทถกสรางจากคลาสเดยวกนจะใชการทางานรวมกนเสมอ

การทางานของออปเจคบางครงจะถกเรยกวาเมธอด ซงประกอบไปดวย ชนด

การคนคา ชอ และจานวนพารามเตอรตงแต 0 หรอมากกวา

รปแบบการทางานของเมธอดสามารถสรปไดดงน

Visibility Name (Parameter-List) : Return-Type-Expression {Property-String}

operations

Page 5: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Rectangle

- height- width

+ getArea()+ resize()

- Private

# Protected

+ Public

Access Specifier

Class Notation – visibility

Customername : Stringaddress : StringId : Integercity : String

getCustomerId( )getCustomerName( )getAddress( )

Visibility Notation Description

public + สามารถเขาถงไดจากภายนอกคลาส

protected # สามารถเขาถงไดจาก Container คลาสและคลาสสบทอด

package ~ สามารถเขาถงไดจากคลาสทอยภายในแพคเกจเดยวกน

private - ไมสามารถเขาถงไดจากภายนอก Container คลาส

Page 6: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Attribute and Class Diagram

– price: int = 30 public class ClassA {private int price = 30;

}

– price: int = 30 {frozen} public class ClassA {private final int price = 30;

}

+ price: int public class ClassA {public static int price;

}

– data[10]: int public class ClassA {private int [10] data;

}

Page 7: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

The Need for Relationships

ระบบเชงวตถทก ๆ ระบบประกอบไปดวยออปเจคและคลาสตาง ๆ

การทางานของระบบสามารถทาไดโดยการตดตอกนระหวางออปเจคภายใน

ระบบผานความสมพนธระหวางกน

เพอใหคลาสหนงสามารถสงแมสเสจไปยงคลาสอน ๆ จาเปนจะตองม

ความสมพนธระหวางคลาสทงสอง

Data-oriented

Conceptual/computational worldReal world

Abstraction

InterpretationObject-oriented

Page 8: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Relationship Types

ชนดความสมพนธระหวางคลาสใน UML มดงน

● Dependency

● Association

Aggregation

Composition

● Generalization และ Realization Dependency

Class with parts

AssemblyClass

Association

Class A

Class B

name

Class with parts

AssemblyClass

Aggregation

Class with parts

AssemblyClass

Composition

Superclass

Subclass

Inheritance Realization

Interface

ConcreteClass

Page 9: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Associations

เปนความสมพนธระหวางคลาสตงแตสองหรอมากกวา ทถกระบโดยการเชอมตอระหวางออปเจคตาง ๆ

Association ระหวางสองคลาสแสดงใหเหนวาออปเจคทปลายดานหนงของความสมพนธจดจา (recognize) อกออปเจคหนงทอยดานตรงกนขาม และอาจมการสงแมสเสจระหวางกน

● คณสมบตนจะชวยในการคนหาความสมพนธในขณะทใชไดอาแกรมแบบปฏสมพนธได

ตวอยางเชน

CompanyEmployee

Page 10: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Association End

หมายถง end ของการ association เมอมความสมพนธระหวางคลาสเกดขน

ถอเปนสวนหนงของ association แตไมใชสวนหนงของคลาส

สญลกษณตาง ๆ ทสามารถนาเสนอประกอบใน association end ได เชน:

● Navigability

● Role name

● Multiplicity

Page 11: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Association Direction

นาเสนอผานสญลกษณลกศรสองทศทาง หรอไมมหวลกศรทงสองดาน

สวนการกาหนดหวลกศรทางเดยวไวจะเปนความสมพนธแบบ Uni-direction

Bi-directional ....

..

..

Class 1 Class 2

Uni-directional ....

..

..

Class 1 Class 2

Page 12: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Association Name

ความสมพนธสามารถกาหนดชอได

โดยปกตแลวชอจะถกกาหนดดวย verb หรอ verb phrase ทใชสอความหมายใน

การตดตอกนของความสมพนธทปรากฏ เชน

จดประสงคของการระบชอความสมพนธไว เพอชวยเพมความชดเจนยงขนแก

ผใช ดงนนจงถอเปน optional

Lecturer Courseteaches

a Lecturer teaches a Course

Page 13: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Role Names

บรเวณปลายเสนความสมพนธทตดกบคลาสจะถกเรยกวา Role Name

โดยอยในรปของคานามทแสดงถงบทบาทของออปเจคทมความสมพนธท

เกดขน

role name จะถกกาหนดตาแหนงบนความสมพนธใกล ๆ กบคลาสทใชงาน

Class A Class BName Association

role_Brole_A

Car Persondrives

company car driver

เชน

Page 14: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Multiplicity

2, 4..6, 8Multiple, disjoint ranges

2..4Specified range

0..1Zero or one (optional association)

1..*One or more

* (0..*)Zero or more (unlimited)

1Exactly one

ใชระบจานวนความสมพนธของออปเจคทมสวนในความสมพนธทถกกาหนดไว

● จานวนออปเจคจากคลาสหนง ๆ ทมความเกยวของกบออปเจคจากคลาสอน ๆ

● สามารถระบไดบรเวณปลายเสนแสดงความสมพนธ

2,4…6, 8

0..1

1..*

*

1 Student

Student

Student

Student

Student2..4

Student

Page 15: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Multiplicity

นอกจากนนยงถอเปนการระบ lower และ upper bound ของจานวน instances

Book

..

Copy

Is a copy of

1 *

ทก ๆ ออปเจคจากคลาส copy จะมความสมพนธในรปของ “is a copy of” จาก Book เพยงหนงเลมเทานน

Page 16: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

หมายเหต ชอของคลาสและแอททรบวตตองเปนเอกพจนเทานน ตวอยางเชน

Sale ไมใช Sales

เงอนไขในการกาหนดชอ

● อกษรตวแรกของชอคลาสในแตละคาตองเปนตวใหญเสมอ โดยไมมชองวาง

ระหวางกลาง เชน SalesLineItem

● แอททรบวตตองเปนอกษรตวเลก เชน date

● ชอความสมพนธใชอกษรตวแรกเปนตวใหญและใชสญลกษณ dash ระหวางคา

เชน Contained-in

Class Naming Conventions

Page 17: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Dependency Relationship

ความสมพนธแบบ Dependency

● Dependency เปนความสมพนธทเกดขนและสนสดลงในระยะเวลาสน ๆ

● โดยความสมพนธของคลาสหนงจะขนอยกบอกคลาสหนงเทานน ดงนนเมอ

มการเปลยนแปลงทเกดขนภายในคลาสหนงจะมผลตออกคลาสหนงเสมอ

นยามทใช ไดแก uses, rely on, knows about เปนตน

ใชเสนประในการนาเสนอความสมพนธแบบน

Supplier

dependency

Client

Page 18: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Dependencies

ในระดบโคดความสมพนธแบบนเกดขนจากการเรยกใชเมธอดของคลาสอน โดยรบคาออปเจคของคลาสอนในรปของพารามเตอร โดยไมจาเปนตองถกกาหนดไวเปนแอทรบวตภายในคลาส

ตวอยางของความสมพนธแบบน ไดแก:

● ตวแปรแบบ local หรอพารามเตอรทปรากฏอยภายในเมธอดของคลาส

FilmClip

playOn(c:Channel)start()stop()

Channel

dependency

name

Page 19: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Association Relationship

เปนความสมพนธทมลกษณะคลายกบความสมพนธแบบ Dependency แตจะ

แตกตางกนตรงทชวงเวลาทเกดความสมพนธกนจะมระยะเวลานาน และม

ลกษณะเปนความสมพนธแบบโครงสราง

ความสมพนธแบบนอาจถกมองในรปของการมอย (has-a) ได เชน ออปเจค

จากคลาสหนงมคาอางองไปยงออปเจคจากคลาสอนได

ใชเสนทบในการนาเสนอความสมพนธแบบน

Student Teacher1..*1..*

Page 20: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Reflexive Relationships

คลาสอาจมความสมพนธกบตวเองได เชน ในกรณทคลาสมออปเจคท

สามารถเปนไดหลาย roles

ความสมพนธแบบนถอเปน Reflexive Association ซงเกดขนกบออปเจคท

สรางจากคลาสเดยวกน

ชอของ Role โดยปกตจะถกใชสาหรบ Reflexive Relationships

PersonParent

Child

2 Role nameNameAddressTelephone

*

Page 21: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Reflexive relationships cont..

หนง Course ออปเจคสามารถเปน role ของ Prerequisite ทเกยวของกบ

Course ออปเจคตงแต 0 หรอมากกวา

หนง Course ออปเจคจะเกยวของกบ Course ออปเจคตงแต 0 หรอมากกวาทม

role เปน Prerequisite

Course

0..*0..*

0..*0..*

+Pre-requisites

Page 22: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Associations (cont.)

StaffMember Student1..* *instructs

instructor

Association name

Role name

MultiplicityNavigable

(uni-directional) association

Courses

pre -requisites

0..3Reflexive association

Role

*

Page 23: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Association Class

บอยครงทการออกแบบคลาสอาจมจานวนความสมพนธเปนแบบ many-to-manyดงนนการกาหนดความสมพนธระหวางคลาสอาจมความจาเปนในการแกไขใหอยในรปของความสมพนธแบบ one-to-many

ตวอยางเชน: Student แตละคนสามารถลงทะเบยนไดหลาย Subject และแตละ Subject ประกอบไปดวย Student หลายคน

Student Subject

IDNameAddressTelephone

CodeNameCredit

Enrolment

* *

จะเกดอะไรขนหากมการกาหนดผลการเรยน (grade) ใหกบ Student ทลงทะเบยน

เรยนใน Subject ทกาหนดไว

Page 24: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Association Classes

การทางานของ Association คลาส สามารถทาไดโดยการกาหนดคลาสทเรยกวา

คลาส Registration ซงประกอบไปดวยแอททรบวตในรปของออปเจคจากคลาส

Student และ Subject

GradeAssociation class

Registration

Student Subject

IDNameAddressTelephone

CodeNameCredit

Enrolment

* *

1 ออปเจคของ Registration ใชสาหรบแตละออปเจคของ Student -Subject ทม

การลงทะเบยนเรยนนนเอง

Page 25: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

What is Aggregation?

ความสมพนธแบบ Aggregation บางครงจะถกเรยกวา Shared Aggregation

เปนความสมพนธในลกษณะทเรยกวา “whole-part” ซงประกอบไปดวยคา

ประเภทตอไปน“consists of”, “contains”, “is part of”

สวนทเปน part อาจเปนสวนประกอบของสวนทเปน whole อน ๆ ได

สญลกษณทใชใน UML จะมลกษณะคลายกบความสมพนธแบบ association แต

จะเพมสญลกษณ diamond ไวในสวนทเปน whole เพอระบวาเปนความสมพนธ

แบบ Aggregation

Department Teacher*

* consists of

partwhole

Page 26: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Aggregation Relationship

Team Person*

* Members

● Team ประกอบไปดวย Person ทเปนสมาชกภายในทม

● Person หนงสามารถเปนสมาชกของ Team อน ๆ ได

● Person ถอเปนสวนของ part ทมการใชงานรวมกน

Team ทาหนาทเปน whole สวน Person ทาหนาทเปน part

ความสมพนธแบบนชวงชวตของสวนทเปน part จะไมขนอยกบสวนทเปน whole

ในกรณท Team ถกลบออกจากระบบ Person ออปเจคจะไมถกลบตามไปดวย

Page 27: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Aggregation

weight

Automobile

serial numberyearmanufacturermodelcolor

drivepurchase

3,4,5 2,4*

Engine

horsepowervolume

onoff

Wheel

diameternumber of bolts

Brakelight

onoff

Door

openclose

Battery

ampsvolts

chargedischarge

Page 28: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Composition

เปนความสมพนธแบบ whole/part เชนเดยวกบความสมพนธแบบ aggregation

แตจะแตกตางกนตรงท Composition ถอเปนความสมพนธทแนนหนากวา

● โดยสวนทเปน whole ทาหนาทเปนเจาของสวนทเปน part

สวนทเปน part ไมสามารถเปนสวนประกอบของสวนทเปน whole อน

● สญลกษณ diamond สดาจะใชในการนาเสนอความสมพนธแบบcomposition ซงจะถกกาหนดไวทางดาน Book เนองจาก Book ประกอบไป

ดวย Chapter

ChapterBook

Page 29: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Whole/Part

ความสมพนธแบบ Composite ไมสามารถเกดขนไดโดยปราศจากสวนทเปน part

● เมอสวนทเปน whole ถกสรางขน สวนทเปน part จะตองถกสรางขนดวย

● เมอสวนทเปน whole ถกลบ สวนทเปน part จะถกลบตามไปดวย

จานวนความสมพนธดานท whole จะตองมคา 0 หรอ 1 เทานน

จานวนความสมพนธจะถกนาเสนอเฉพาะดานทเปน part (คา default = 1)

● ชวงชวตของสวนทเปน part จะตองขนอยกบสวนทเปน whole เสมอ

สวนทเปน whole จะตองทาหนาทในการจดการการสรางหรอลบสวนทเปน part เสมอ

ตวอยางเชน House และ House Rooms

● หากลบ House สวนทเปน rooms จะถกลบไปโดยอตโนมต

Page 30: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Aggregation & Composition

การจาแนกความสมพนธแบบ whole/part

เปนความสมพนธแบบ transitive

● ถาวตถ A เปนสวนหนงของ B และ B เปนสวนหนงของ C แลว A เปนสวน

หนงของ C ดวย

● เชน ถาทเปดประตเปนสวนหนงของประต ประตเปนสวนหนงของรถยนต

แลว ทเปดประตเปนสวนหนงของรถยนต

เปนความสมพนธแบบ Anti-symmetric

● วตถใดๆ อาจไมจาเปนตองเปนสวนประกอบของตวเองทงทางตรงและ

ทางออม

● เชน ถาประตเปนสวนหนงของรถยนต แตรถยนตไมจดเปนสวนหนงของ

ประต

Page 31: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Generalization

Generalization จะเปนความสมพนธในรปของ sub-class / super-class ซงจะ

ไดแก inheritance นนเอง

ในระดบ Conceptual: จะเปนความสมพนธแบบ supertype / subtype

เชนเดยวกบทใชใน ERD

ในระดบ Specification : เปนคณสมบตของ subtype ทรวบรวมเอาคณสมบต

ทงหมดทไดรบจาก supertype มาเปนสวนหนงของตวเองโดยอตโนมต

ในระดบ Implementation : จะเปนคณสมบตทเรยกวาการสบทอดของแอททร-

บวตและเมธอดของ subClass ทไดมาจาก superclass ในภาษาของการโปรแกรม

Page 32: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Relationships/Links: Generalization

เปนความสมพนธทเรยกวา “is-a” หรอ “a kind of”

Generalization เปนความสมพนธระหวางคลาสในลกษณะทมการสบทอด

เกดขน ตวอยางเชน

General

Refined UnderGraduateStudent

scholarships

getProject( )

Studentname : String = " "CPA : float = 0.0GPA : float = 0.0

generateSlip( )

PostGraduateStudentworkingExperience

getThesis( )

DiplomaStudentpracticalTraining

getTraining( )

Page 33: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Generalization

Sub-class สบทอดมาจาก Super-class

● ขอมลหรอแอททรบวต

● การทางาน

● ความสมพนธ

โดย Sub-class อาจ

● เพมแอททรบวตและการทางาน

● เพมความสมพนธ

● แกไข (override) การทางานของคลาสสบทอด

ความสมพนธแบบ generalization ไมถกนาไปใชในการนาเสนอความสมพนธทเกดขนในขนตอนการพฒนา interface

Page 34: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Generalization

จาวาจะนาเสนอความสมพนธแบบ generalization โดยใชคยเวรด: extends และimplements (Java)

Extends จะถกสรางขนเมอ stereotype ของ subclass อยในรปของ << class>>

Implements จะถกสรางขนเมอ stereotype ของ subclass เปน <<type>>

A<<type>>

B<<type>>

A is supertype

B is subtype

A

B

A is superclass

B is subclass

Page 35: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Realization

ความสมพนธแบบ realization จะเปนการระบวาคลาสหนงถกสบทอด

พฤตกรรมการทางานมาจากคลาสอนในรปของ interface

UML ใชสญลกษณของคลาสทมการระบ stereotype วา <<interface>>

สญลกษณ Realization จะตางจาก Generalization ตรงทเปนเสนประเสมอ

<<interface>>A

+operation1()+operation2()

B

Page 36: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Realization

UML ใชสญลกษณในการนาเสนอ interface ไดเปน 2 รปแบบคอ

● สญลกษณของคลาสทใช stereotype โดยระบ <<interface>>

Page 37: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Realization

สญลกษณในการนาเสนอ interface แบบท 2 คอ สญลกษณ "lollipop" ทใช

รวมกบคลาส หรอ subsystems อน ๆ

สญลกษณ Realization

กลายเปนเสนทบอตโนมต

Page 38: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

ความสมพนธทถกระบเปน 0..* แสดงใหเหนวาออปเจคจากคลาส Professor

สามารถปรากฏขนโดยไมจาเปนตองมออปเจคจากคลาส Student

สวนจานวนความสมพนธ 1..* แสดงวาออปเจคของคลาส Student จะตองมออป

เจคจากคลาส Professor อยางนอยหนงหรอมากกวานนเอง

Multiplicity Review 1

Professor Studentteaches

instructor learner

1..* 0..*

Page 39: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Multiplicity : Caution !

จานวนความสมพนธเรมตนดวย "0.." 0 เชน 0..1 หรอ 0..* (อานวา 0 หรอมากวา หรอ Optionally or More ) จะถอเปนความสมพนธแบบมเงอนไข

● ซงหมายความวาไมจาเปนตองมออปเจคในการนาเสนอ

● ความสมพนธแบบมเงอนไขตองการโคดทใชในการทดสอบวาความสมพนธแบบนวาออปเจคทถกระบไดถกสรางขนแลวหรอยง และโคดดงกลาวจะตองถกนาไปใชในทก ๆ ครงทความสมพนธถกนาไปใช ซงโคดในลกษณะนเปนสงทควรหลกเลยง

สวนจานวนความสมพนธทเรมตนดวย "1.." ถอเปนการกาหนดจานวนแบบไมมเงอนไข ซงหมายความวาตองมอยางนอยหนงออปเจคในการนาเสนอ

ในกรณทไมมการระบจานวนความสมพนธไวคาเรมตนจะถกกาหนดใหเปน 1 เสมอ

Page 40: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Multiplicity Review 2

Publisher Bookproduce Customerpurchase

Author

wrote

1 0..* 1..* 0..*

1..*

1..*

A Publisher produces 0 or more Books A Books is produced by exactly 1 Publisher. A Books is sold to 0 or More Customer. A Customer purchase at least 1 or More Books. An Author wrote at least 1 or More Books. A Book was written by At least 1 or More Author.

Page 41: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Dependency ในโปรแกรมเชงวตถทวสามารถนาเสนอได 3 รปแบบดงน

Dependency From Diagram to Java Code

class Computer {.............................

}

class Student { Student (Computer computer) {}

}

class Student { public Computer getComp() {

return new Computer(); }

}

class Student { public fun() {

Computer computer; }

}

Student Computer

Page 42: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

มลกษณะเปนสวนหนง “Part of” หรอ “Part Whole” โดยสวนทเปน part มชวง

ชวตทไมขนอยกบสวนทเปน whole และสวนทเปน part สามารถนาไปใชรวมกบ

สวนทเปน Whole อน ๆ ได

Aggregation

public class Address { } public class Customer {

private Address add;public Customer (Address add ) {

this.add = add;}

}Customer cus = new Customer(new Address);

Customer Address

Page 43: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

คลายกบความสมพนธแบบ Aggregation แตจะมความหนาแนนกวาและแตกตาง

กนตรงทสวนทเปน part มชวงชวตทขนอยกบสวนทเปน whole และสวนทเปน

part ไมสามารถนาไปใชรวมกบสวนทเปน Whole อน ๆ ได

Composition

public class Department { } public class University {

private Department dep = new Department();}

University cs = new University();

University Department

หมายเหต ความสมพนธแบบ Association , Aggregation และ Composition เมอ

แปลงเปนโคดดวยภาษาจาวาจะมลกษณะเหมอนกน

Page 44: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

ความสมพนธแบบนเรมตนจากการกาหนดพฤตกรรมทว ๆ ไปใน Superclass

สวนพฤตกรรมทมลกษณะเฉพาะจะถกกาหนดไวใน Subclass ความสมพนธแบบ

นมผลทาให Subclass ไดรบคณสมบตของ Superclass โดยอตโนมต

Generalization

public class Person { }

public class Student extends Person {……………….

}

Person Student

Page 45: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

เปนความสมพนธทถกกาหนดไวสาหรบการทางานกบ interface เพอใชสนบสนน

การสบทอดแบบหลายมตไดเชนเดยวกบภาษาเชงวตถอน ๆ การแปลงโคด

สามารถทาไดโดยใชคยเวรด implements ดงรป

Realization

interface UniversityStaff {void dateEntry();

}

public class Professor implements niversityStaff

{ public void dateEntry() { }

}

UniversityStaff Professor

Page 46: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Types of Classes

มมมองของคลาสอาจแบงออกไดเปน 3 ชนด ไดแก

● Data classes ใชสาหรบการจดเกบขอมล เชนเดยวกบเอนตท

● Control classes ใชสาหรบการทางานตามทกาหนดไวใน use case

● Interface classes เปน GUI ทผใชสามารถมองเหนได

นยามทสามารถใชทดแทนกนได คอ Model-View-Controller

● Model = data classes

● View = interface classes

● Controller = control classes

Page 47: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

The Conceptual Class Diagram

บางครงเรยกวา Domain Model ถอเปนคลาสทมรปแบบงายทสด

● คลาสระดบแนวคดเนนไปท Data และ Controller ทระบไวใน use case หลกกอน

● โดยแสดงใหเหนถงคลาส ความสมพนธ และแอททรบวต

● ไมมการแสดงเมธอดหรอ interface คลาส

-date-time

Sale-quantitySalesLineItem

-amountPayment

1

1

Paid-by

11..*Contained-in

Association

Conceptual class

Attribute

Multiplicity (discuss later)

Page 48: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Even More on Navigability

คลาสไดอาแกรมระดบ Conceptual จะไมมการนาเสนอทศทางความสมพนธ

เนองจากไดอาแกรมในระดบไมขนอยกบโปรแกรมภาษาทใช

ทศทางความสมพนธจะมความสาคญในขนตอนของการออกแบบมากกวาขนตอน

ของการวเคราะห และจะมความสาคญยงขนโดยเฉพาะอยางยงในขนตอนของการ

พฒนาระบบ

ในกรณทไมมการระบทศทางความสมพนธไว UML จะถอเปนความสมพนธแบบ

สองทศทางเสมอ

ในกรณทไมมการระบจานวนความสมพนธไว UML จะถอเปนความสมพนธแบบ

หนงตอหนงเสมอ

Student Teacher

Page 49: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Analogies to ERD

ERD Class Diagram

Entities Classes

Relationships Associations

Cardinality Multiplicity

Data elements or attributes Attributes

เนองจากคลาสไดอาแกรมในระดบ Conceptual มลกษณะใกลเคยงกบ ERD

แตไมใชสงเดยวกน

คลาสเกดขนในสวนทเปนขอมลแบบ Transient ดงนนจงไมมการจดเกบแบบ

ทเปน entity ได ขณะท ERD เปนแบบ Persistence

Page 50: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Class Modeling Technique

วธการออกแบบเชงวตถสามารถทาได 2 วธ ไดแก :

● Responsibility Driven Design (RDD)

เปนวธการออกแบบระบบในรปของคลาสทม collaborating รวมกน

โดย responsibilities จะถกพจารณาจาก requirements

กาหนด responsibilities ลงในคลาสทเหมาะสม

● Data Driven Design (DDD)

ใชเทคนคในการระบคานามจากขอมลทงหมดทอยภายในระบบ

สรางคลาสทประกอบไปดวยขอมลทเกยวของ

Page 51: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Responsibility ภายในออปเจคมสองรปแบบ ไดแก knowing และ doing

● Knowing responsibilities ของออปเจคทประกอบดวย

ทราบเกยวกบขอมลทสามารถนาไปใชในการทางานตาง ๆ

เชน Sale ม responsibility ทตองทราบหรอรเกยวกบยอดขาย

ทราบเกยวกบออปเจคทมความเกยวของกน

● Doing responsibilities ของออปเจคทประกอบดวย

การกระทาสงตาง ๆ ดวยตวเอง เชน การสรางออปเจคหรอการคานวณตาง ๆ

เรมตนการกระทาในออปเจคอน

ตวอยางเชน Sale ม responsibility ในการสราง SalesLineItems

Responsibility

Page 52: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

เปนผลมาจากการประยกตใชการออกแบบเมธอดของการโปรแกรมเชงวตถ

(Wirfs-Brock & Wilkerson, 1989) การออกแบบชนดนถกใชในการกาหนดคลาส

ในชวงตน ๆ ของการพฒนา

โดยเรมตนจากการคนหาคลาส แสดงรายละเอยดของความสมพนธทตองการ

ภายในแบบจาลองขอมล จากนนกาหนดพฤตกรรมการทางาน โดยพจารณาจาก

การเปลยนแปลงจากเหตการณทเกดขนเพอแกไขแบบจาลองซ า

สวนวธการทไดรบความนยมในการคนหาคลาสภายในระบบ ไดแก การ

วเคราะหคานามทถกพฒนาขนโดย Abbott (1983)

● คานามถกนามากาหนดเปน candidates คลาส

● คากรยาถกนามากาหนดเปนการทางาน

Data Driven Design

Page 53: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Modeling Technique : Candidate Class

เทคนคการระบคานามทไดมาจากสงทมอยจรงตาง ๆ ดงตอไปน :

● Tangible Thing เปนออปเจคหรอกลมของออปเจคทมตวตนและสามารถจบตองได เชน engine, invoice, vehicle book, car เปนตน

● Role เปนบทบาทการทางานของบคคลตามหนาทตาง ๆ เชน student, teacher,

หรอ customer

● Event เปนเหตการณทเกดขนในชวงเวลาหนงทมความจาเปนตองบนทกไว เชน

withdrawal, purchase, registration, meeting

● Organization เปนองคกรทผใชสงกด โดยปกตจะประกอบไปดวยกลมบคคล

ทรพยากร อปกรณตาง ๆ ทมความสามารถในการทางานทมขนาดใหญ เชน

bank, branch, department, committee

Page 54: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Class Modeling Technique

● Places & Locations เปนสถานทหรอพนททกาหนดไวสาหรบคนและสงตาง ๆ

ทเกยวของ เชน offices และไซดงานตาง ๆ

● Concepts เปนแนวคดหรอสงทไมมตวตนอยจรง เชน Citizenship,

authorization

● Other Systems เปนระบบหรออปกรณอน ๆ ทอยภายนอกและมการตดตอกบ

ระบบ เชน payroll system หรอ grading system

Page 55: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Class Modeling Technique

ระบคลาสทมความไมเหมาะสม :

● Redundant (มากเกนไป) - คลาสเดยวกนแตมหลายชอ

● Vague (คลมเครอ) - คานามทมความหมายคลมเครอ หรอไมแนชด

● Operation - ม state, behavior และ identity หรอไม ?

● Meta-language – คานามทเปนสวนประกอบของภาษาของการสรางแบบจาลอง และไมไดเปนสวนหนงของ problem domain

● Outside the scope of the system – คานามทตรงประเดนในการแสดงถงวธการทระบบทางาน แตไมไดหมายถงสงทอยภายในระบบ

● Attribute - เมอคานามอางถงบางสงบางอยางทไมสามารถเชอมโยงไปยงการทางานทเกยวของได

Page 56: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Class Diagram - Example

A zoo consists of a set of cages.

Every cage is the home of at least 2 animals.

Cages are located besides each other.

Every cage has at most one left neighbor and at most one right neighbor.

Animals can be reptiles, insects, and mammals.

Mammals are elephants, monkeys, and tigers.

Monkeys eat bananas.

Tigers prefer meat.

Page 57: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Zoo

Reptile

Animal

Insect Mamal

Elephant Monkey Tiger

Banana Meat

Cages

11.*

1 2.*

eat prefer

Zoo Class Diagram

Page 58: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

How to find Class:

เรยนรเกยวกบ problem domain โดยอาศยการสงเกตจาก client

ประยกตใชกบสภาพความเปนจรงในปจจบน

ใชลาดบเหตการณทปรากฏอยในรายละเอยดของยสเคส เพอชวยในการคนหาออปเจคทเกยวของ

ใชวธการวเคราะหคานามจาก scenario หรอกระแสของเหตการณ (Abbott Textual Analysis, 1983)

● แสดงรายการของคานามทงหมดในรปของคลาสคแขง

● ตดคานามทมความหมายซ ากนออกไป

● กาหนดคานามทเปนแอททรบวตหรอคานามทสามารถเปนคลาส

Page 59: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

class diagram from flow of events

The customer enters the store to buy a toy. It has to be a toy that his daughter likes and it must cost less than 50 Euro. He tries a videogame, which uses a data glove and a head-mounted display. He likes it.

An assistant helps him. The suitability of the game depends on the age of the child. His daughter is only 3 years old. The assistant recommends another type of toy, namely a boardgame. The customer buy the game and leaves the store

type of toy

customer

depends

storeenters

Customer

?

enter()

toy

daughter

suitable

*

than 50 Euro

store

enter()

daughterage

toy

buy()

videogame boardgame

toy

age

videogame

daughter

boardgame

Flow of events:

toypricebuy()like()

buy

less

Page 60: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Grammatical construct UML Component

noun class

verb Operation

Classifying verb Inheritance

Possessive Verb Aggregation

modal Verb Constraint

Adjective Attribute

Intransitive verb Operation (Event)

Textual Analysis using Abbot‘s technique

Example

“toy"

“enters"

“is a" ,“either..or", “kind of…"

"Has a ", “consists of"

“must be", “less than…"

"3 years old"

“depends on…."

Page 61: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

“The library management system would be used by the librarian to keep track of

books, library members and the borrowing activity. Not all books can be

borrowed by the members. Some books may only be available to review in the

library; all such books are termed as Reference books. There are 2 types of

library members students and college faculty members. Faculty members can

also check out research papers and magazines where as students can only

checkout books. The system need to send alerts whenever a book that has to be

returned within a specific date is not returned. An email is sent to the librarian

and the borrower. A librarian uses the book and student information to issue a

book. The catalogue needs a reference of the book to add or update itself. The

system maintains a catalogue having a description of each book that is available

in the library”

Exercise

Page 62: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

“ระบบจดการหองสมด (The library management system) ถกใชงานโดยบรรณารกษ

(librarian) เพอตรวจสอบหนงสอ (books) สมาชกหองสมด (library members) และกจกรรมท

เกยวของกบการยม โดยสมาชกไมสามารถยมหนงสอไดทกรายการ ซงหนงสอบางรายการมไว

สาหรบการใชงานเฉพาะภายในหองสมด (Library) ซงจะถกเรยกวาหนงสอสาหรบการอางอง

(Reference book) สมาชกหองสมดแบงออกเปนสองประเภทไดแก สมาชกทเปนนกศกษา

(Student member) และสมาชกทเปนเจาหนาทของคณะ ( faculty members) โดยเจาหนาท ฯ

สามารถยมไดทงเอกสารงานวจย (research paper) และนตยสาร (magazine) ขณะทนกศกษา

สามารถยมไดเฉพาะหนงสอเทานน ระบบจาเปนตองมการแจงเตอน (alerts) สาหรบหนงสอท

ยงไมไดคนเมอครบกาหนดเวลาการยมแลว โดยระบบจะสงอเมลแจงไปทบรรณารกษและผยม

(borrower) นอกจากนนบรรณารกษยงใชขอมลจากหนงสอและนกศกษาในการพจารณาออก

หนงสอ (issue a book) ใหม สวนการจดทาบญชรายการ (catalogue) จาเปนตองอางองถง

หนงสอเพอใชเปนขอมลสาหรบการเพมหรอแกไขรายการโดยเฉพาะ โดยระบบจดการ

หองสมดตองมการดาเนนการตรวจสอบบญชรายการทประกอบไปดวยรายละเอยดของหนงสอ

แตละเลมทอยภายในหองสมด”

Exercise

Page 63: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

วเคราะหคานาม (Noun Analysis) โดยการเลอกคานามทปรากฏอยในความ

ตองการของระบบ ซงคานามทเหนไดชดเจน ไดแก: Librarian, Book,

Member, Catalogue, Student_Member, Faculty_Member, Reference_Book,

Issueable_Book, Alert

จากคานามดงกลาวอาจมบางสวนทขาดหายไป

● ขาดขอมลทเกยวของกบคานามเหลานน หรอเปนคานามทไมมบทบาท

ภายในระบบ เชน ไมมฟงกชนหรอการกระทาทเกยวของอยางชดเจน

● คานามทสามารถเปนออปเจคหรอคลาสไดจะตองประกอบไปดวยแอททร

บวตและการทางานทเกดขนกบขอมลนน ๆ

Ways to identify Classes

Page 64: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

ขนตอนตอไปเปนการกาหนดความสมพนธระหวางคลาสทอยในรปของ

generalization/ inheritance/ specialization, composition / containership,

aggregation/ collections และ associations. ซงการกาหนดความสมพนธเหลานอาจ

ใชคาถามดงตอไปนชวยในการออกแบบได เชน

คลาสมการสงผานคาระหวางกนดวยวธการอยางไรบาง

คลาสใดบางทมคลาสอนเปนสวนประกอบ

คลาสใดบางททาหนาทในการสรางออปเจคจากคลาสอน

คลาสใดบางททระบบตองการเรยกใชเพอชวยอานวยความสะดวกในการ

ทางาน

Association : Consideration

Page 65: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

generalizations/inheritence: เรมตนจากการตรวจสอบความสมพนธใหอยในรป

ของการสบทอด ซงในความตองการของระบบอาจประกอบไปดวย “type of”

หรอ “can be”

● ในกรณนไดแก คลาส BOOK , REFERENCE_BOOK และ

ISSUEABLE_BOOK จะพบวา book สามารถเปน reference book หรอ issue

able book ในทานองเดยวกน reference books และ issue able books ถอเปน

type หนงของ book โดยทงสามคลาสมการใชคณสมบตบางสวนรวมกน

● นอกจากนนยงมคลาส Member, Student_Member และ Faculty_Member

สามารถกาหนดความสมพนธแบบสบทอดไดเชนเดยวกบคลาส Book

Resolving the relationships between classes

Page 66: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

ความสมพนธแบบเปนสวนหนง หรอ “Part of” หรอ “Part Whole” ความสมพนธ

แบบนอาจพบไดใน Problem statement เชน “has a” ซงอาจอยในรปของ

ความสมพนธแบบ Composition หรอ Aggregation

● จากความตองการของระบบการจดการหองสมดจะพบวามความสมพนธแบบ

part whole เพยงกรณเดยวไดแก catalogue ทประกอบไปดวยรายละเอยดของ

books ภายในระบบ ซง catalogue จะยงคงอยแมวา book จะถกลบออกไปกตาม

ซงในกรณนความสมพนธจงถกกาหนดใหเปนแบบ aggregation

Part-Whole Relationships:

Page 67: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

เกดขนเมอคลาสหนง ๆ อาจมการเรยกใชบรการจากคลาสอน ๆ ผานเมธอดท

กาหนดไวจะกอใหเกดความสมพนธระหวางกนทเรยกวา Association ซง

ความสมพนธดงกลาวจะพบในความตองการของระบบทมการระบคาวา “makes

use of” หรอ “uses” เปนตน

การทางานในลกษณะนจะพบไดในตวอยางระบบจดการหองสมดไดในกรณของ

บรรณารกษใชขอมลจากหนงสอและนกศกษาในการพจารณาสงซอ จาเปนตอง

อางองไปยงหนงสอเพอเพมหรอแกไขขอมลในแคตาลอก

● A librarian uses the book and student information to issue a book. The

catalogue needs a reference of the book to add or update itself.

Association Relationships:

Page 68: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Exercise First Draft

ReferenceBook IssueableBook

FacultyMember Student

Catalogue

Member

Libralian

Book Alert

Page 69: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Add more attributes : Relationship

Page 70: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Example : How to find Classes

การคนหาคลาสจากยสเคส สามารถทาไดโดยใช เทคนคทเรยกวาการ

วเคราะหคานาม (Noun Analysis)

โดยมขนตอนดงตอไปน

● สรางรายการของคลาสคแขง

● ทบทวนรายการของออปเจคคแขง

● กาหนดขอบเขตการทางานของคลาส

● กาหนดความสมพนธระหวางคลาส

● นาผลลพธทไดมาสรางแบบจาลองคลาสไดอาแกรม

Page 71: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Use Case Diagram

Browse ProductUser

RegisterLogin

Add Shopping Cart

View Order StatusCard

Authorization

Customer

Check Out Check Inventory Status

View Shopping CartUpdate Shopping Cart

<<extend>>

<<extend>>

Page 72: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Use Case : Login

ระบบแสดงหนาจอลอกอนโดยอตโนมตเมอผใชเขาสระบบสงซอออนไลน โดยหนาจอดงกลาวจะประกอบดวยฟลดชอผใช(Username) และรหสผาน(Password) ระบบจะรบคาชอผใชและรหสผานเพอตรวจสอบกบขอมลผใชทอยภายในฐานขอมลของระบบ ในกรณทผใชใสคาชอผใชและรหสผานทตรวจสอบแลวไมมอยในระบบฐานขอมล(Database) ระบบจะแสดงขอความชอผใชและรหสผานไมถกตอง (Invalid Password and/or User ID)

ในกรณทผใชยงไมมชอผใช รหสผาน และขอมลทจาเปนในการสงซอ ระบบจะยอมใหผใชสามารถลงทะเบยนโดยผานฟงกชนลงทะเบยน(Register) ทอยดานลางของหนาจอลอกอน หลงจากขนตอนการลอกอนแลวระบบจะยอมใหผใชเขาถงการทางานของการสงซอ (Place Order) การยกเลกการสงซอ (Cancel Order) และการแกไขรายการสงซอ (Update Order)

Page 73: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Use Case : Browse Product

Browse Product ใชสาหรบแสดงรายละเอยดสนคาตามประเภททผใชเลอก

โดยเรมตนจากระบบแสดงประเภท (Category) ของสนคาทงหมด เมอผใช

(User) หรอลกคา (Customer) เลอกประเภทสนคาทตองการ รายละเอยดของ

รายการสนคา (Product) ทงหมดจะถกนาเสนอบนหนาจอ โดยประกอบไป

ดวย รหสสนคา (productId) ชอสนคา (productName) ราคาตอหนวย

(pricePerUnit) เปนตน

Page 74: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Use Case : Add Shopping Cart

ขนตอนนเรมตนเมอลกคาเลอกการสงซอโดยเลอกรายการสนคา (Order Item)

ทตองการแลว ระบบจะแสดงรายละเอยดสนคา (Product) ทประกอบดวย

รหสสนคา (ProductID) ชอสนคา (ProductName) ราคาตอหนวย (Unit Price)

โดยผใชกรอกจานวนหนวย (Unit) ทตองการ จากนนระบบทาการคานวณ

ราคารวมทงหมด (Total Price) ทละรายการ จนกวาผใชจะเลอกการจายเงน

(Payment) โดยการกรอกขอมลบตรเครดต (Credit Card) ทประกอบไปดวย

หมายเลขบตร (Credit card Number) และวนหมดอาย (Expire Date) จากนน

ลกคาสามารถสงรายการสงซอ (Order) เขาสระบบ เพอใหระบบทาการ

ตรวจสอบ บนทก และสงขอมลตอไปยงระบบบญช (accounting system)

เพอยนยนการสงซอ ระบบจะคนคารหสรายการสงซอ (OrderID) กลบไปยง

ลกคา และสนสดการทางานของยสเคส

Page 75: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

ลอกอน (Login) ชอผใช(Username) รหสผาน(Password)

ลกคา (Customer) ชอ (Name) นามสกล (Lastname)

วนเดอนปเกด (DateOfBirth) อาชพ (Occupation) ทอย (Address)

บานเลขท (Number) ถนน (Street) ตาบล (Sub-District)

อาเภอ (District) จงหวด (Province) รหสไปรษณย (ZipCode)

สถานทจดสงสนคา (Delivery

Address)

ยนยนการกรอกรหสผาน (Confirm

Password)

รหสลกคา (CustomerID)

รายการสงซอ (Order) วนทสงซอ (OrderDate) รายการสนคา (Order Item)

จานวนหนวย (Unit) สนคา (Product) รหสสนคา (ProductID)

ชอสนคา (ProductName) ราคาตอหนวย (Unit Price) ราคารวมทงหมด (Total Price)

การจายเงน (Payment) บตรเครดต (Credit Card) หมายเลขบตร (Credit card

Number)

วนหมดอาย (Expire Date) สถานะบตรเครดต (Status) ระบบบญช (accounting system)

รหสรายการสงซอ (OrderID) สถานะการสงซอ (OrderStatus) จดสง (Deliverly)

ขนตอนท 1: สรางรายการของคลาสคแขง

Page 76: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

ขนตอนท 2: ตรวจสอบรายการของคลาสคแขง

คลาสทมความหมายซ ากน (Redundant Classes) เชน คลาสผใช (User) หรอคลาสลกคา (Customer)

คลาสทไมตรงประเดน (Irrelevant Classes) ซงจะหมายถงคลาสทไมมอะไรเกยวของโดยตรงกบระบบ เชน ลกคารองเรยน (Customer complaint)

คลาสทมความหมายคลมเครอ (Vague Classes) เปนคลาสทมความหมายไมชดเจนตอการพฒนาระบบ เชน system และ software เปนตน

คานามทอยในรปของแอททรบวต เปนคานามทไมสามารถเปนคลาสไดโดยตรงเนองจากมลกษณะเปนรายละเอยดประกอบในคลาสอน ๆ เปนหลก เชน ชอลกคา (Customer’s name)

คานามทเปนการทางานไมสามารถกาหนดเปนคลาสไดโดยตรง เชน การคนหาสนคา (Product Search)

คานามทเปนคลาสคแขงทมลกษณะเปนความสมพนธมากกวาทจะเปนคลาส

Page 77: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

การกาหนดคลาสจากคลาสคแขง

รายการคลาสคแขง คลาส เหตผล

ลอกอน (Login) / - กาหนดเปนคลาสสาหรบการลอกอน

ชอผใช(Username) - - กาหนดเปนแอททรบวตของคลาสลอกอน

รหสผาน(Password) - - กาหนดเปนแอททรบวตของคลาสลอกอน

ลกคา (Customer) / -กาหนดเปนคลาสลกคา

ชอ (Name) - - กาหนดเปนแอททรบวตของคลาสลกคา

นามสกล (Lastname) - - กาหนดเปนแอททรบวตของคลาสลกคา

วนเดอนปเกด (Date of Birth) - - กาหนดเปนแอททรบวตของคลาสลกคา

อาชพ (Occupation) - - กาหนดเปนแอททรบวตของคลาสลกคา

Page 78: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Class Diagram : First Draft

OrderDetail Product

Category

Address

CheckOut Order

Login Customer has

place

pay

contains consists of

refer to

Page 79: Object Oriented Development with UML Class DiagramClass Diagram Object Oriented Development with UML By-Assoc. Prof. Rangsit Sirirangsi What is a Class Diagram? ใช ส าหร

Address- no- street- subDistrict- district- province- zipcode- telNo

Login- username- password

CheckOut- typeCreditCard- creditID- creditName- expirationDate

Customer- name- lastName- gender- birthday- occupation- customerid

n 1n 1 1 11 1

Category- categoryId- categoryName- categoryDesc

Order- orderId- totalPrice- tax- orderDate- deliveryDate- statusDelivery

11 11

1

1..n

1

1..n

Product- productId- productName- pricePerUnit- qtyInStock

n

1

n

1

OrderDetail- OrderDetailId- qty- sum

1..n

1

1..n

1

1..n 11..n 1

has

place

contains

refer to

consists of

pay

Class Diagram: Conceptual Level