Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.

Similar presentations


Presentation on theme: "CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙."— Presentation transcript:

1 CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙

2 2 Elements of Object Oriented Programming(I) n Object oriented programs correspond to another perception of the world: entities(objects) which appear to be categorized into groups(classes) that behave similarly, but with individual differences bases on internal state. n Message passing: Objects are loosely coupled; they communicate by sending messages to one another n Object oriented approaches are useful for construction of systems of many objects to model or simulate behavior of real or imaginary worlds.

3 3 Elements of Object Oriented Programming(II) n Additional means are needed for managing complexity in OOP systems, such as inheritance or delegation. n In scheme, we use environments to capture local state. n We use message passing to give access to this local state. n This allows the callee to decide what code gets executed!

4 4 A Simple Object (define (cons x y) (lambda (msg) (cond ((eq? msg ’CAR) x) ((eq? msg ’CDR) y) ((eq? msg ’PAIR?) #t) (else (error ”Cons cells cannnot” msg))))) (define (car p) (p ’CAR)) (define (cdr p) (p ’CDR)) (define (pair? p) (p ’PAIR?))

5 5 An environment diagram :Example (define foo (cons 1 2)) (car foo) (define bar (cons 3 4)) (car bar)

6 6 Taking Advantage of Names as Places (define (cons x y) (lambda (msg) (cond ((eq? msg ’CAR) x) ((eq? msg ’CDR) y) ((eq? msg ’PAIR?) #t) ((eq? msg ’SET-CAR!) (lambda (new-car) (set! x new-car))) ((eq? msg ’SET-CDR!) (lambda (new-cdr) (set! y new-cdr))) (else (error ”Cons cells cannnot” msg)))))

7 7 An environment diagram illustrating: (define (set-car! p new-car) ((p ’SET-CAR!) new-car)) (define (set-cdr! p new-cdr) ((p ’SET-CDR!) new-cdr)) (define p (cons 1 4)) (set-car! p 3)

8 8 Dot Notation to Supply Method Parameters (define (cons x y) (define (do-set-car! new-car) (set! x new-car)) (define (do-set-cdr! new-cdr) (set! y new-cdr)) (define (dispatch msg. args) (cond ((eq? msg ’CAR) x) ((eq? msg ’CDR) y) ((eq? msg ’PAIR?) #t)

9 9 ((eq? msg ’SET-CAR!) (do-set-car! (car args))) ((eq? msg ’SET-CDR!) (do-set-cdr! (car args))) (else (error ”Cons cells cannnot” msg)))) dispatch) (define (set-car! p new-car) (p ’SET-CAR! new-car)) (define (set-cdr! p new-cdr) (p ’SET-CDR! new-cdr))

10 10 Managing Large Systems n Operation-Centric: –Generic operation –Dispatch on type –Data-directed Programming Type (spaceship)(planet) move count-down Operation Data Directed Object-Oriented n Type/Object-Centric: –Message passing –Object-oriented Programming

11 11 Data-Directed Ship Implementation (define (install-ship-package) ;; Internal representation (define (make-ship x y time) (list x y time)) (define (ship-x ship) (car ship)) (define (set-ship-x! ship new-x) (set-car! ship new-x))......... (define (move ship dx dy) (set-ship-x! (+ (ship-x ship) dx)) (set-ship-y! (+ (ship-y ship) dy)) (list (ship-x ship) (ship-y ship))) my-ship

12 12 (define (count-down ship) (let ((time (ship-time-left ship))) (set-time-left! (- time 1)) (if (<= time 0)’blast-off time) ;; External representation - tagged object (define (tag x) (attach-tag ’spaceship x)) (put ’make ’spaceship (lambda (x y t) (tag (make-ship x y t)))) (put ’move ’spaceship (lambda (s dx dy) (tag (move s dx dy)))) (put ’count-down ’spaceship count-down) ’done) (define (move obj dx dy) (apply-generic ’move obj dx dy))

13 13 Message-Passing Ship Implementation x-pos: 0 y-pos: 0 time-left: 2 move: count-down: dispatch: my-ship

14 14 (define (make-spaceship x-pos y-pos time-left) (define (move dx dy) (set! x-pos (+ x-pos dx)) (set! y-pos (+ y-pos dy)) (list x-pos y-pos)) (define (count-down) (set! time-left (- time-left 1)) (if (<= time-left 0) ’blast-off time-left)) (define (dispatch message) (cond ((eq? message ’move) move) ((eq? message ’count-down) count-down) (else (error ”No method” message)))) dispatch) (define enterprise (make-spaceship 0 0 2)) ((enterprise ’move) 1 2) ==> (1 2)

15 15 Object-Oriented Programming n 1960’s : Simula n 1970’s : Smalltalk n 1980’s : C++ n 1990’s : Java Individual entities or objects which are categorized into groups or classes that behave similarly, but with individual differences based on internal state of each instance.

16 16 Properties of an Object n Instances have Identity: in sense of eq? –Objects instances as Scheme message-passing procedures –Classes as Scheme “make- ” procedure n Private State n Private State: gives each object (each instance of a class) the ability to behave differently –Local environment n Methods for responding to messages –Scheme procedures (take method-dependent arguments)

17 17 n An Inheritance Rule telling what method to use if no specific method is defined for a given message –Need to add conventions on messages & methods

18 18 Object-Oriented System - Version 1 ;OOP System - Version 1: Methods (define (make-speaker name) (lambda (message) (cond ((eq? message ’NAME) (lambda () name)) ((eq? message ’CHANGE-NAME) (lambda (new-name) (set! name new-name))) ((eq? message ’SAY) (lambda (list-of-stuff) (if (not (null? list-of-stuff)) (display-message list-of-stuff)) ’NUF-SAID))) (else (no-method)))))

19 19 With Case ;OOP System - Version 1: with CASE (define (make-speaker name) (lambda (message) (case message ((NAME) (lambda () name)) ((CHANGE-NAME) (lambda (new-name) (set! name new-name))) ((SAY) (lambda (list-of-stuff) (display-message list-of-stuff) 'NUF-SAID)) (else (no-method)))))

20 20 OO System -Version 1 Cont’d n Abstract our retrieval of method from the object (given the message)... (define (get-method message object) (object message)) n... and the combined retrieval and application of that method to the arguments: (define (ask object message. args) (let ((method (get-method message object))) (if (method? method) (apply method args) (error "No method for message" message))))

21 21 Detection of methods (missing method) ;Detection of methods (missing methods) (define no-method (let ((tag (list 'NO-METHOD))) (lambda () tag))) (define (method? x) (cond ((procedure? x) #t) ((eq? x (no-method)) #f) (else (error "Object returned non-message" x)))) (define (display-message words) (if (null? words) (newline) (begin (display (car words)) (display " ") (display-message (cdr words)))))

22 22 Example (define p (make-speaker ’George)) (ask p ’NAME) George (ask p ’SAY ’(I cannot tell a lie)) I cannot tell a lie nuf-said

23 23 OOP System - Version 2 n Inheritance by Delegation –We need our objects to have access to “themselves” (as a variable that can be used in methods). –We need a way to inherit (or gain use of) the structure and methods of a superclass. The self variable –problem : we want an object to call its own method –no access to the “object” itself –solution: to require that all methods take self as their first parameter, and always pass the “object” as the first argument.

24 24 Self Variable (define (make-speaker name) (lambda (message) (case message ((NAME) (lambda (self) name)) ((CHANGE-NAME) (lambda (self new-name) (set! name new-name) (ask self 'SAY (list 'call 'me name)))) ((SAY) (lambda (self list-of-stuff) (display-message list-of-stuff) 'NUF-SAID)) (else (no-method)))))

25 25 Extension to ask (define (ask object message. args) (let ((method (get-method message object))) (if (method? method) (apply method object args) (error "No method for message" message)))) Example: (define p (make-speaker ’GEoGE) (ask p ’CHANGE-NAME ’fred) call me fred nuf-said

26 26 A Specialized Speaker (Subclass) n Want lecturers to be a kind of speaker - that inherit the behavior of speakers but add to that behavior: Approach: Inheritance using delegation : –Inherit speaker behavior by adding an “internal” speaker object Get internal object to act on behalf of object by delegation –If message is not recognized, pass the buck –Can change or specialize behavior: Add new methods Change operation of methods

27 27 inheritance by Delegation ;define lecturer inherits from speaker: (define (make-lecturer name) (let ((speaker (make-speaker name))) (lambda (message) (case message ((LECTURE) (lambda (self stuff) (delegate speaker self 'SAY (append '(therefore) stuff)))) (else (get-method message speaker)))))) (define d (make-lecturer 'Duane)) (ask d 'LECTURE '(the sky is blue)) therefore the sky is blue

28 28 Delegation (define (delegate to from message. args) (let ((method (get-method message to))) (if (method? method) (apply method from args) (error "No method" message)))) (define (ask object message. args) (apply delegate object object message args)) A chain of inheritance is thus possible. For example, consider an “Arrogant Lecturer” that changes the basic way of talking: he/she appends “obviously” to *everything* he/she says...

29 29 Another Subclass (define (make-arrogant-lecturer name) (let ((lecturer (make-lecturer name))) (lambda (message) (case message ((SAY) (lambda (self stuff) (delegate lecturer self 'SAY (append stuff '(obviously))))) (else (get-method message lecturer))))))

30 30 Example (define b (make-arrogant-lecturer 'Bill)) ; (ask b 'SAY '(the sky is blue)) the sky is blue obviously nuf-said (ask b 'LECTURE '(the sky is blue)) therefore the sky is blue ;;; BUG ;;........................ obviously

31 31 Fixing the bug: ask vs delegate (define (make-lecturer name) (let ((speaker (make-speaker name))) (lambda (message) (case message ((LECTURE) (lambda (self stuff) ; (delegate speaker self 'SAY ; (append '(therefor) stuff)))) (ask self 'SAY (append '(therefor) stuff)))) (else (get-method message speaker))))))

32 32 Example (define b (make-arrogant-lecturer 'Bill)) (ask b 'SAY '(the sky is blue)) the sky is blue obviously nuf-said (ask b 'LECTURE '(the sky is blue)) therefore the sky is blue obviously nuf-said

33 33 Multiple Inheritance n Can have object that inherit methods from more than one type. Suppose rather than a named speaker we have an anonymous comic: (define (make-comic) (lambda (message) (case message ((SAY) (lambda (self stuff) (display-message (append stuff '(ha ha))))) ((JOKE) (lambda (self) (display-message '(A duck walks into a bar)) 'nuf-said)) (else (no-method)))))

34 34 example : create funny lecturer: (define eric (let ((comic (make-comic)) (lecturer (make-arrogant-lecturer 'Eric))) (lambda (message) (get-method message lecturer comic)))) (ask eric 'JOKE) a duck walks int a bar nuf-said (ask eric 'LECTURE '(the sky is blue)) therefor the sky is blue obviously nuf-said (ask eric 'SAY '(the sky is blue)) the sky is blue obviously preferred type secondary type

35 35 OO System - version 3 (define (get-method message preferred. others) (define (loop objs) (let ((method (get-method-from-object message (car objs))) (rest (cdr objs))) (if (or (method? method) (null? rest)) method (loop rest)))) (loop (cons preferred others))) (define (get-method-from-object message object) (object message))

36 36 Alternative Multiple Inheritance n We have lots of flexibility - suppose we want to pass the message on to multiple internal objects (not just some “preferred” one). (define eric (let ((comic (make-comic)) (lecturer (make-arrogant-lecturer 'Eric))) (lambda (message) (lambda (self. args) (apply delegate-to-all (list lecturer comic) self message args)))))

37 37 delegate-to-all (ask eric 'SAY '(the sky is blue)) the sky is blue obviously the sky is blue ha ha (define (delegate-to-all to-list from message. args) (foreach (lambda (to-whom) (apply delegate to-whom from message args)) to-list))


Download ppt "CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙."

Similar presentations


Ads by Google