principle of object-oriented programming · 2020. 1. 14. · principle to keep in mind… 1...

26
Principle of Object-Oriented Programming Phachaya Chaiwchan (Lecturer)

Upload: others

Post on 16-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Principle ofObject-Oriented Programming

Phachaya Chaiwchan (Lecturer)

Page 2: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Page 3: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Page 4: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

EXAMPLE

Page 5: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Principle to keep in mind…

1 Encapsulate with varies

2 Code to the Interface, not to the implementation.

3 Favor composition over inheritance

4 Strive for loosely coupled designs between objects that interact.

5 Classes should be open for extension but closed for modifications.

Page 6: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Principle to keep in mind…

6 Depend on abstractions. Do not depend on concrete classes.

7 A class should have only one reason to change.

Benefits if you follow these principles

Modular , Flexible , Adaptable , Maintainable CODE.

Page 7: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

OOP

Class

Data Abstraction

Encapsulation

Information Hiding

InheritancePolymorphism

Object

Method

Attribute

Page 8: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Page 9: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Abstraction

Page 10: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Encapsulation

Page 11: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Encapsulation

Page 12: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Encapsulation

Page 13: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Inheritance

Page 14: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Polymorphism

Object-Oriented Programming: OOP

Page 15: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Polymorphism

Object-Oriented Programming: OOP

Page 16: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Polymorphism

Compile Time Run Time

FunctionOverloading

OperatorOverloading

OperatorOverloading

Page 17: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Polymorphism Example

Shape

Circle

Rectangle

Polygon

FilledCircle

Dot Square FilledPolygon

Page 18: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Page 19: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object-Oriented Programming: OOP

Page 20: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Class Diagram

Page 21: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Object Relationship

Associations

Inheritance

Realize/Implementation

Dependency

Aggregation

Composition

Page 22: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

• Dependency Relationship

“Dependency ใช้เพ่ืออธิบายว่าของสองสิง่มีความสัมพันธ์กันแบบขึน้ต่อกันหรือมีอทิธิพลต่อกัน (การเปล่ียนแปลงในสิง่หน่ึงจะส่งผลกระทบต่ออีกสิง่หน่ึง)”

ความสมัพนัธ์แบบดีเพนเดนซีคือความสมัพนัธ์ท่ีขึน้ตอ่กนัท่ีอีกคลาสหนึง่เรียกใช้อีกคลาส หนึง่โดยก าหนดให้เป็นแอตทริบิวต์ภายในเมธอดยอ่ย หรืออาจเป็นพารามิเตอร์ หรือคา่ท่ีคืนก็ได้ ซึง่ใช้สญัลกัษณ์คือ

Object Relationship

Page 23: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

• Aggregation Relationship

“Aggregation หมายถงึ การที่วัตถุย่อยหลายๆวัตถุถูกน ามาประกอบเข้ากับวัตถุหลักหน่ึงตัวเพ่ือสามารถท างานบางอย่างได้”

แอคกรีเกชนั เป็นความสมัพนัธ์ has-a ซึง่หมายถงึ มี โดยท่ีระหวา่งสองคลาสจะมีคลาสหนึง่มีแอตทริบิวต์เป็นของอีกคลาสหนึง่อยู ่เช่น คลาส class A has a class B หมายถึง คลาส A จะมีแอตทริบิวต์อยา่งน้อยหนึง่แอตทริบิวต์เป็นคลาส B ใช้สญัลกัษณ์ A B

ความสมัพนัธ์แบบดีเพนเดนซีคือความสมัพนัธ์ท่ีขึน้ตอ่กนัท่ีอีกคลาสหนึง่เรียกใช้อีกคลาส หนึง่โดยก าหนดให้เป็นแอตทริบิวต์ภายในเมธอดยอ่ย หรืออาจเป็นพารามิเตอร์ หรือคา่ท่ีคืนก็ได้ ซึง่ใช้สญัลกัษณ์คือ

Object Relationship

Page 24: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

• Composition Relationship

“เป็นความสัมพันธ์เชงิส่วนประกอบเช่นเดียวกันกับความสัมพันธ์แบบ แอคกรีเกชัน แต่มีความแตกต่างกันตรงที่การพจิารณาความมีชีวติ หรือช่วงชีวติของวัตถุที่น ามาสร้างเป็นส่วนประกอบว่ามีอสิระต่อตัวที่น าไปประกอบได้หรือไม่”

หากคลาสท่ีสร้างขึน้มาเกิดจากการประกอบกนัหลาย ๆ คลาส เม่ือวตัถหุลกัท่ีเกิดจากการนา เอาคลาสหลาย ๆ คลาสมาเป็นองค์ประกอบถกูท าลายไป จะท าให้วตัถยุอ่ยท่ีเป็นตวัองค์ประกอบถกูท าลายไปด้วย

Object Relationship

Page 25: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

• Composition Relationship

เช่น คลาสรถยนต์ต้องมีคลาสล้อ คลาสตวัถงัรถยนต์ คลาสเคร่ืองยนต์ เป็นต้น หากคลาสรถยนต์ถกูท าลายไปคลาสท่ีเป็นองค์ประกอบก็จะไม่สามารถท างานได้ และถกูท าลายไปด้วยทนัทีหรือ คลาสคอมพิวเตอร์ ต้องมีคลาสซีพีย ูคลาสเมนบอร์ด คลาสแรม หากวตัถหุลกัคือคอมพิวเตอร์ถกูท าลาย สว่นประกอบตา่ง ๆ ก็จะถกูท าลายไปด้วย ความสมัพนัธ์แบบนีมี้สญัลกัษณ์ คือ

Object Relationship

Page 26: Principle of Object-Oriented Programming · 2020. 1. 14. · Principle to keep in mind… 1 Encapsulate with varies 2 Code to the Interface, not to the implementation. 3 Favor composition

Q & A