1 / 29

บทที่ 4 Aggregation Abstraction

บทที่ 4 Aggregation Abstraction. ในบทที่แล้วได้พูดถึง Classification Abstraction ไปแล้ว ซึ่งเป็นกระบวนการสร้าง Class จาก Object ต่าง ๆ ที่มีอยู่ใน Problem Domain จนได้ Class แต่ยังขาดความสัมพันธ์ระหว่าง Class ต่าง ๆ ที่เราสร้างขึ้น

portia
Download Presentation

บทที่ 4 Aggregation Abstraction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. บทที่ 4Aggregation Abstraction • ในบทที่แล้วได้พูดถึง Classification Abstraction ไปแล้ว ซึ่งเป็นกระบวนการสร้าง Class จาก Object ต่าง ๆ ที่มีอยู่ใน ProblemDomain จนได้ Class แต่ยังขาดความสัมพันธ์ระหว่าง Class ต่าง ๆ ที่เราสร้างขึ้น • จากสภาพความเป็นจริงในโลกนี้จะพบว่ายังมีวัตถุหลาย ๆ อย่างที่เกิดจากการรวมตัวของวัตถุอื่น ๆ เช่น คนเกิดจากการวมกันของ ลำตัว แขน ขา หัว หรือ คอมพิวเตอร์เกิดจากการรวมของ Case , main board , monitor , Drive หรือแม้แต่โลกเราก็ยังประกอบไปด้วย ดิน น้ำ อากาศ และแร่ธาตุ ต่าง ๆ เป็นต้น

  2. ตัวอย่าง 4.1 • “ห้องเรียนประกอบไปด้วย กระดานดำ 1 กระดาน มีเก้าอี้และโต๊ะจำนวนหนึ่ง มีนักศึกษา มีอาจารย์” • จากข้อความข้างต้น สามารถสรุปได้ว่า class กระดานดำ class โต๊ะ class เก้าอี้ class นักศึกษา class อาจารย์ เมื่อนำมารวมกันจะได้ class ใหม่ คือ class ห้องเรียน

  3. ห้องเรียน นักศึกษา กระดานดำ โต๊ะ เก้าอี้ อาจารย์

  4. ถ้ามองในทางกลับกันจะได้ว่าถ้ามองในทางกลับกันจะได้ว่า Class ห้องเรียนสามารถแบ่งออกได้เป็น Class กระดานดำ Class โต๊ะ Class เก้าอี้ Class นักศึกษา Class อาจารย์ มองในมุมของ Decomposition ห้องเรียน นักศึกษา กระดานดำ โต๊ะ เก้าอี้ อาจารย์

  5. Aggregation Abstraction • คือ การ พยายามตอบคำถามที่ว่า มี class ใดเป็นส่วนประกอบ (Is part of) ของ class อื่นหรือไม่ และที่สำคัญ “การประกอบกันของ class ต้องทำให้เกิด class ใหม่ ซึ่งมี concept ใหม่ด้วย” • ในทาง object orientation นั้น การแสดงสัญลักษณ์เพื่อแสดง Aggregation Abstraction ของ class นั้น ทำได้โดยการโยงลูกศรเป็นสี่เหลี่ยมขนมเปียกปูน จาก class ย่อยหรือ class ที่เป็นส่วนประกอบ (Composite class) ไปยัง Class หลัก (Main Class) ดังแสดงในรูป

  6. การเป็นส่วนหนึ่ง (Is part of) สามารถเขียนแทนด้วย สัญลักษณ์ ห้องเรียน นักศึกษา กระดานดำ โต๊ะ เก้าอี้ อาจารย์

  7. บางครั้งอาจเกิดกรณีที่ class หนึ่งสามารถเป็น class ย่อยของ class หนึ่งได้พร้อมทั้งยังเป็น class ย่อยของอีก class หนึ่งได้เช่นกัน ห้องเรียน บุคลากร นักศึกษา กระดานดำ โต๊ะ เก้าอี้ อาจารย์

  8. บางครั้งอาจเกิดกรณีที่ class หนึ่งสามารถเป็น class ย่อยของ class หนึ่งได้พร้อมทั้งยังเป็น class ย่อยของอีก class หนึ่งได้เช่นกัน รถยนต์ เรือยนต์ เครื่องยนต์ ที่นั่ง ล้อ ประตู ไฟ

  9. บางครั้งอาจเกิดกรณีที่ class หนึ่งสามารถเป็น class ย่อยของ class หนึ่งได้พร้อมทั้งยังเป็น class ย่อยของอีก class หนึ่งได้เช่นกัน Computer ? ? ? ? ? ? ?

  10. Cardinality , Required Component ,Optional Component สำหรับ Aggregation Abstraction การประกอบกันของ class หรือความสัมพันธ์เชิง is part of นั้นอาจจะเกิดกรณี ที่ class หลัก (main class) ประกอบไปด้วย class ย่อย (Composite class ) ชนิดที่หรึ่ง เพียงชิ้นเดียว แต่ประกอบด้วย class ชนิดที่สอง จำนวน 4 ชิ้นขึ้นไป และอาจจะประกอบไปด้วยชนิดที่สาม ไม่จำกัดจำนวน หรืออาจไม่มีเลยก็ได้ ซึ่งสิ่งที่ใช้ในการแสดงความสัมพันธ์ ดังกล่าวนี้เรียกว่า Cardinality

  11. Cardinality , Required Component ,Optional Component • ในทาง OO เราเรียก Class ย่อย ว่า Component นั่นเอง โดย • เราเรียกส่วนประกอบที่จำเป็นต้องมีว่า Require Component • เช่น รถยนต์ต้องมีเครื่องยนต์ ถ้าไม่มีเครื่องยนต์ รถยนต์ก็ ไม่สามารถวิ่งได้ นั่นเอง • เราเรียกส่วนประกอบที่ไม่จำเป็นต้องมีว่า Optional Component • เช่น เครื่องปรับอากาศในรถยนต์ไม่จำเป็นต้องมีก็ได้ หมายถึงไม่มีเครื่องปรับอากาศรถยนต์ก็ยังสามารถวิ่งได้

  12. Cardinality , Required Component ,Optional Component ห้องเรียน นักศึกษา mandatory กระดานดำ โต๊ะ เก้าอี้ อาจารย์ mandatory mandatory mandatory mandatory

  13. Cardinality , Required Component ,Optional Component รถเก๋ง 0..1 เครื่องปรับอากาศ 1..2 1..1 2..4 4..4 กระโปรงรถ เครื่องยนต์ ประตู ล้อ

  14. Cardinality , Required Component ,Optional Component หนังสือ 1..1 บรรณานุกรม 2..2 1..1 1..1 1..N ปก คำนำ สารบัญ บท

  15. หนังสือ 1..n 1..1 2..2 1..n 1..1 ปก คำนำ สารบัญ บทของเนื้อหา บรรณานุกรม ต.ย. การเขียน cardinality ของ class หนังสือ

  16. บทของเนื้อหา 1..n หน้าหนังสือ

  17. หน้าหนังสือ 0..n 0..n ตัวหนังสือ รูปภาพ ต.ย. การเขียน cardinality ของ class หน้าหนังสือ

  18. หนังสือ 1..n 1..1 2..2 1..n 1..1 ปก คำนำ สารบัญ บทของเนื้อหา บรรณานุกรม 1..n หน้าหนังสือ 0..n 0..n ตัวหนังสือ รูปภาพ ต.ย. การเขียน cardinality ของ class หนังสือ

  19. หนังสือ - รหัสหนังสือ +ชื่อหนังสือ + อ่าน + พิมพ์ 1..n 1..1 2..2 1..n 1..1 ปกหนังสือ + ประเภทของปก + พลิก • คำนำ • เนื้อความ • ชื่อผู้แต่ง • + อ่าน • สารบัญ • เนื้อความ • + อ่าน • บทของเนื้อหา • เนื้อหาแต่ละบท • + อ่าน บรรณานุกรม - เนื้อความ + อ่าน 1..n • หน้าหนังสือ • เนื้อหาแต่ละหน้า • พลิก • อ่าน 0..n 0..n • ตัวหนังสือ • อักขระ • + สะกด รูปภาพ - ภาพ + ดู ต.ย. การเขียน cardinality ของ class หนังสือ

  20. ASSIGNMENT • จงเขียน class diagram ที่แสดง Aggregation Abstration ของ Computer PC • จงเขียน class diagram ที่แสดง Aggregation Abstration ของ โทรศัพท์มือถือ (Mobile) • จงเขียน class diagram ที่แสดง Aggregation Abstration ของสมุดธนาคาร (Bank Account)

  21. Aggregation A unfilled diamond shows aggregation. This relationship indicates that one is PART of the other. An Employee is PART of a division. But a division can stand on its own without Employees.

  22. The Person Class

  23. Composite Aggregation A filled diamond shows Composite Aggregation. This relationship indicates that one is PART of the other. An OrderDetail is PART of an order. But an Order CANNOT stand on its own without Order Details.

  24. บทที่ 5 Generalization Abstraction • เพื่อให้ผู้อ่านเข้าใจหลักการของ Generalization Abstraction และการ Inheritance • เพื่อให้ผู้อ่านเข้าใจวัตถุประสงค์ กลไก และวิธีการทำ inheritance • เพื่อให้ผู้อ่านสามารถแยกความแตกต่างและสร้างความสัมพันธ์ระหว่าง Super class และ Sub class ได้

  25. Polymorphism in VB.NET Public Class Shape Public Overridable Sub draw() MsgBox("Draw Shape") End Sub End Class Public Class Rectangle Inherits Shape Public Overrides Sub draw() MsgBox("Draw Rec ") End Sub End Class Public Class Circle Inherits Rectangle Public Overrides Sub draw() MsgBox("Draw Circle ") End Sub End Class • Prefix Overridable before Base Method • Prefic Overrides before derived class

  26. Polymorphism in VB.NET Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim s As Shape = New Shape s.draw() Dim s1 As Rectangle = New Rectangle s1.draw() Dim s3 As New Circle s3.draw() End Sub

  27. Polymorphism in VB.NET Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim s As Shape = New Shape s.draw() Dim s1 As Rectangle = New Rectangle s1.draw() Dim s3 As New Circle s1 = s3 s1.draw() End Sub

  28. No polymorphism Public Class Shape Public Overridable Sub draw() MsgBox("Draw Shape") End Sub End Class Public Class Rectangle Inherits Shape Public Overrides Sub draw() MsgBox("Draw Rec ") End Sub End Class Public Class Circle Inherits Rectangle Public Overrides Sub draw() MsgBox("Draw Circle ") End Sub End Class Public Class Triangle Inherits Shape Public Shadows Sub draw() MsgBox("Draw Circle ") End Sub End Class Dim s As Shape = New Shape s.draw() Dim s1 As Rectangle = New Rectangle s1.draw() Dim s3 As New Circle s1 = s3 s1.draw()

  29. Calling Base Method Public Class Shape Public Overridable Sub draw() MsgBox("Draw Shape") End Sub End Class Public Class Rectangle Inherits Shape Public Overrides Sub draw() MsgBox("Draw Rec ") End Sub End Class Public Class Circle Inherits Rectangle Public Overrides Sub draw() MsgBox("Draw Circle ") MyBase.draw() End Sub End Class Mybase

More Related