ซอฟต์แวร์เชิงวัตถุ ( object-oriented software) ·...

32
O1 O3 O2 O4 m1 m2 m4 m3 m5 Program Environment ซอฟต์แวร์เชิงวัตถุ ( Object - Oriented Software) คือ กลุ ่ม หรือชุด(Collection) ของวัตถุที ่มีหน้าที ่รับผิดชอบต ่อ การจัดการข้อมูลของตนเอง และติดต่อสื ่อสารกับวัตถุอื ่นๆ โดยส่งข้อความ (Message) ให้แก่กันและกัน Object - Oriented Software

Upload: others

Post on 31-Dec-2019

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

O1

O3

O2

O4

m1

m2m4m3

m5

Program

Environment

• ซอฟตแวรเชงวตถ (Object-Oriented Software)คอ กลม หรอชด(Collection) ของวตถทมหนาทรบผดชอบตอการจดการขอมลของตนเอง และตดตอสอสารกบวตถอนๆ โดยสงขอความ (Message) ใหแกกนและกน

Object-Oriented Software

Page 2: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

What is an object?

• วตถ (object)เปน กลมหรอชด (collection) ของสงตอไปน • แอททรบวต (Attribute) ของวตถจะบอกถงขอมล (Data)คณลกษณะเฉพาะหรอ สถานะของวตถ • เมทธอด (Method) จะบอกถงหนาท (functionality) หรอ พฤตกรรม (behavior) ตางๆของวตถนนๆ

• โดยม แอสโซซเอชน (Association) อนไดแก สงทบอกถงความสมพนธ (Relationship) ของวตถนน กบวตถอนๆ รวมทงปฏสมพนธ (Interactions) ระหวางวตถกบวตถอนๆ

?

Page 3: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Identify Problem Domain

• ระบ ขอบเขตของปญหา (Problem Domain) เพอสรางแบบจ าลองวตถ (Modeling Objects)

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

Page 4: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Objects = Attributes, Methods, Associations

AttributesAgeGenderName….

AssociationsmyLeftArmmyLeftLegmyRightArm....

Methods/OperationsJump..Talk...Dance

Ask FriendJumpWalkJumpShake myHeadShake myBody

A working OO system:a conversation between networksof objects

Tim

head

body

armarm

leg leg

Page 5: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Objects = Attributes, Methods, Associations

AttributesModelCompanyColor….

AssociationsBrakeLeftFrontWheelGear....

Methods/OperationsStart..Go...Stop..

Go ForwardGo BakcwardTurn LeftTurn RightApply BrakeChange Gear….

A working OO system:a conversation between networksof objects

รถยนต Toyota สแดง

body

Brake

wheelwheel

wheel

wheel Engine

Page 6: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

• ก าหนดคณลกษณะ (Attribute) ใหกบวตถo วตถ Tim มคณลกษณะส าคญคอ อาย (25 ป) เพศ (ชาย) ชอ (Tim) o วตถ รถยนตคนหนง เปน รถสปอรต สแดง ยหอ Toyota

• ก าหนดพฤตกรรม/วธการกระท า (Method) ใหกบวตถo วตถ Tim สามารถ เดน ยน นง และนอน ไดo วตถ รถยนตคนหนง สามารถ วง เลยวซาย และเลยวขวาได

• ก าหนดความสมพนธ/ปฏสมพนธกบวตถอน (Association)o วตถ Tim ประกอบดวย ล าตว(body) แขน (arm) 2 แขน ขา(leg) 2 ขาo วตถ รถยนตคนหนง ประกอบดวย ตวถง(body) ลอ (wheel) 4 ลอo วตถ Tim สามารถสรางกจกรรม ใหวตถ “รถยนตคนหนง” เกดการเปลยนแปลงสถานะได เชน การขบรถ

Objects Abstraction

Page 7: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

• วตถทม Attribute, Method และ Association ของวตถคลายๆ กนจะถกจดอยในกลมหรอ คลาส (Class) เดยวกน

Group of Objects

Tim John Don

Page 8: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

• คา Attribute, Method และ Association ของวตถ จะถกก าหนดไวใน “คลาส” (Class)• คลาส (Class)

o เปนสงทน าเสนอ Abstract Data Types (ADTs) ของวตถ (Object) ทเราตองการจ าลองo ก าหนดรายละเอยดเกยวกบ Attribute, Method และAssociation ของวตถ (Object)o เปนพมพเขยว (Blueprint) ของวตถ (Object)

Defining Class

Page 9: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

• คลาส (Class) เปนพมพเขยว (Blueprint) ของวตถ (Object)o ไมสามารถน ามาใชไดโดยตรง o ก าหนดคณลกษณะรวมกนในรปของ Attributeo ก าหนดวธการกระท า/พฤตกรรม รวมกนของวตถ ในรปของ Method o ก าหนดความสมพนธ /ปฏสมพนธกบวตถอนในรปของ Association

• เรยกไดวา วตถ (Object) เปนสงทสรางขน หรอเปน อนแสตนซ (Instance) ของคลาส (Class)

Class

Page 10: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Classes & Instances

Person

Tim Don

• เรยกไดวา คลาส (Class) เปน พมพเขยว/แบบแปลน (Blueprint) ของ วตถ (Object)• เรยกไดวา วตถ (Object) เปน อนแสตนซ (Instance) ของคลาส (Class)c

Page 11: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Class & Object

Class

PERSON

Age

Gender

Name

myArms

myLegs

Jump()

Walk()

Talk()

Dance()

ShakeMyArms()

Object “Tim”

Age = 25

Gender = Male

Name = Tim

myArms = Arms

myLegs = Legs

can Jump

can Walk

can Talk

can Dance

can ShakeMyArms

Page 12: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Class & Object

Class

PERSON

Age

Gender

Name

myArms

myLegs

Jump()

Walk()

Talk()

Dance()

ShakeMyArms()Object “Don”

Age = 25

Gender = Male

Name = Don

myArms = Arms

myLegs = Legs

can Jump

can Walk

can Talk

can Dance

can ShakeMyArms

Page 13: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Classes & Instances

• เรยกไดวา คลาส (Class) เปน พมพเขยว/แบบแปลน (Blueprint) ของ วตถ (Object)• เรยกไดวา วตถ (Object) เปน อนแสตนซ (Instance) ของคลาส (Class)

Page 14: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Class & Object

Class

CAR

Model

Color

Company

theGear

theBrake

GoForward()

GoBackward()

TurnLeft()

TurnRight()

ApplyBrake()

Object “aCar”

Model = Celica

Color = Red

Company = Toyota

theGear = Gear

theBrake = Brake

can GoForward

can GoBackward

can TurnLeft

can TurnRight

can ApplyBrake

Page 15: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Class & Object

Class คอ ตนแบบขอมลทมไวเพอสราง object ทมโครงสรางพนฐานพฤตกรรมเดยวกน ในแนวคดเชงวตถกอนทสราง object จะตองสราง class ขนมากอน

Object ทกตวจะตองอยใน class ซง class กบobject เปนสงทคกนเสมอ สามารถทราบรายละเอยดและคณสมบต ของ Object ไดดวยการดท Class

Page 16: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

ClassClass จะประกอบดวยชอ Class, Attribute และ Method ตาง ๆ ซงเปนตวอธบายรายละเอยด และหนาทตาง ๆ โดยแสดงในลกษณะ diagram ดงน

Name

Attributes

Method

Page 17: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Class

ตวอยาง Class ของ StudentStudent

{abstract}

StudentIDNumber

FirstName

LastName

Address

City

State

RegisterForCourse

DropCourse

ChangeAddress

Page 18: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Class

ใน C++

•การสราง Class ท าไดโดยการประกาศ class ซงมสวนประกอบดงน • class CRectangle {• int x, y;• public:• void set_values (int,int);• int area (void);• } rect;

attribute

Method

ชอ object สรางจากคลาส

ชอ Class

Page 19: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Class• // classes example

• #include <iostream>

• using namespace std;

• class CRectangle {

• int x, y;

• public:• void set_values (int,int);• int area () {return (x*y);}• };

• void CRectangle::set_values (int a, int b) {• x = a;

• y = b;

• }

• int main () {• CRectangle rect;

• rect.set_values (3,4);• cout << "area: " << rect.area();• return 0;

• }

Method

รายละเอยด Method น ามาเขยนดานลาง Class

การสราง object จาก class

การเรยกใช method

Page 20: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Message Passing (1)• Message Passing

o คอการท วตถตดตอกนดวยการสง ขอความ (message) ถงกนและกน

o มผลท าให วตถทรบขอความ (received object) เปลยนแปลงสถานะ หรอ กระท าการอยางใดอยางหนง โดยจะท าการประมวลผลตามความตองการของวตถทสงขอความ หรอ ปฏเสธค ารองขอ

Page 21: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Tim

ถาวตถ Tim ตองการขบรถ aCar จะตองท าอยางไร?

aCar

ขบรถ

Example of Message Passing (1)

Page 22: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Message passing (2)

• การสง Message ไปยงวตถใดๆ แลววตถนนกระท าการอยางใดอยางหนง

o เปนการเรยก Method ทถกก าหนดไวในวตถนนมาท างานo เปรยบเทยบไดกบการเรยก function call หรอ procedure call ทมใน Procedural Programming โดยผาน Interface

o แตละวตถ ตองรบผดชอบ พฤตกรรมของตนเอง โดยผานการเรยกใช Method

Page 23: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Tim

ถาวตถ Tim ตองการขบรถ aCar

aCaraCar.GoForward(100)aCar.goBackward()

aCar.TurnLeft()

aCar.TurnRight()

aCar.ApplyBrake()

Example of Message Passing (2)

Page 24: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Object-Oriented Software

m1

m2

m4m3

m5

Software

Environment

Page 25: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Characteristics of an Object

Identity

Information Hiding

Encapsulation

Inheritance

Polymorphism

Page 26: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Identity• วตถ (Object) จะตองมลกษณะเฉพาะประจ าตว (Identity)

o วตถใดๆ สามารถมคณลกษณะ (Attribute) เหมอนๆ กนได เชน วตถ“Tim” และ“Don” เปนเพศชาย และอาย 25 ป เหมอนกน

o วตถใดๆ สามารถมพฤตกรรม (Behavior/Method) เหมอนๆ กนได เชน วตถ“Tim” และ“Don” สามารถกน เดน นอน นง ไดเหมอนกน

o วตถใดๆ สามารถความสมพนธเกยวของ (Association) กบวตถอนๆเหมอนๆ กนได เชน วตถ“Tim” และ“Don” ประกอบดวย 2 แขนและ 2 ขา เหมอนๆ กน

o วตถ “Tim” และ “Don” สรางจากคลาส (Class) เดยวกนคอ “Person”

• แตเราถอวา วตถ“Tim” และวตถ “Don” เปนคนละวตถกน

Page 27: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Encapsulation

• พจารณาวตถ (Object) ไดเสมอน กลองด า (Black Box)

o วตถใด ตอง “ร” วธหรอขนตอนการท างานภายใน (Method) ของตนเอง

o วตถอน ไมจ าเปนตองรวาวธ หรอขนตอนการท างานภายในเปนอยางไรo วตถอนตองการเรยกใช Method ของวตถนได โดยสง Message

มาโดยท Message จะตองอยในรปแบบทตกลงกนไวกอน

black box

Object

IncomingMessage Outgoing Message

Page 28: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Tim

Example of Encapsulation

Tim.jump()

Tim.walk()

Tim.talk()

Tim.dance()

Tim.shakeMyArms()

Page 29: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Information Hiding• พจารณาวตถ (Object)

o สามารถก าหนดขอบเขตการเขาถง Attribute และ Method

o ก าหนดวาใครสามารถเหนหรอใช Attribute และ Method ของวตถนนไดบาง

anObject

talk()walk()jump()

NameAge

ObjectDiagram A.

anObject

talk()walk()jump()

NameAge

Object Diagram B.

anObject

talk()walk()Jump()

NameAge

Object Diagram C.

Page 30: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Information Hiding and Encapsulation

• Q: จะค านวณคา “Age” จาก PERSON ไดอยางไร?

o เกบคา วน-เดอน-ปเกด ไว แลวค านวณคา “Age” เมอม request?o เกบคาอายปจจบน (current age) และupdate ทกๆ ป?

• A: don’t care!!!o ตราบใดทเราสามารถ maintain “getAge()” interface

PERSON

Age

Gender

Name

myArms

myLegs

Jump()

Walk()

Talk()

Dance()

ShakeMyArms()

getAge()

Page 31: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Advantages of Information Hiding and Encapsulation• Maintainability

o เปนการพจารณาโครงสรางซอฟแวรในระดบนามธรรม (Abstraction) หรอในระดบสง (High-Level) โดยไมค านงถงรายละเอยด วธการ หรอภาษาทจะน าไปพฒนา (Implementation)

• Flexibility

oท าใหการพฒนาซอฟแวรเชงวตถมความยดหยน (Flexibility) สามารถเปลยนแปลงไดงาย (Ease of Change)

oระบบ Software ใดๆ สามารถปฏบตงานรวมกนไดอยางสะดวก และมประสทธภาพ เนองจากไมจ าเปนตอง “ร” รายละเอยดการท างานภายในของระบบ

Page 32: ซอฟต์แวร์เชิงวัตถุ ( Object-Oriented Software) · 2017-09-13 · •Nell Dale, Chip Weems and Mark Headington. Introduction to java and software

Reference

• http://www.cplusplus.com/doc/tutorial/

• Nell Dale, Chip Weems and Mark Headington. Introduction to java and software design. ISBN 0-7637-1064-4

• ผศ. ธวชชย งามสนตวงศ. การเขยนโปรแกรมเชงวตถ. กรงเทพฯ: สถาบนเทคโนโลยพระจอมเกลาพระนครเหนอ. 2545

• รศ.ธรวฒน ประกอบพล. Java OOP. กรงเทพฯ: ซมพลฟาย, 2554.