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

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
CS1 Final Exam Review By Rebecca Schulman December 4, 2002.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
Object System I. OO System in Scheme class private variables methods NAMED-OBJECT self: name: sicp instance x root self: TYPE IS-A NAMED-OBJECT name:
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Data directed programming Message passing Section 2.4, pages ,2.5.2 pages (but with.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
Programming Languages I LISP
OOP! POO! Spelled backwards. Intro to OOP What is OOP?  Stands for Object Oriented Programming  Create different types of objects which can do multiple.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 Programming Languages (CS 550) Lecture 5 Summary Object Oriented Programming and Implementation* Jeremy R. Johnson *Lecture based on notes from SICP.
BCS 2143 Introduction to Object Oriented and Software Development.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
1/21 4/6/2004 OOPS – One more example Goal: See an example that distinguishes between –“is-a” or inheritance relationships –“has-a” or local variable relationships.
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Salman Marvasti Sharif University of Technology Winter 2015.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
OOP Basics Classes & Methods (c) IDMS/SQL News
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object Oriented Programming Systems
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
Class 22: Inheritance CS150: Computer Science University of Virginia
The role of abstractions
Lecture 21: Inheritance CS200: Computer Science University of Virginia
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Advanced Programming Behnam Hatami Fall 2017.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
topics three representations entity relationship diagram object model
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Lecture 12: Message passing The Environment Model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
6.001 SICP Interpretation Parts of an interpreter
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Presentation transcript:

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

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 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 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 An environment diagram :Example (define foo (cons 1 2)) (car foo) (define bar (cons 3 4)) (car bar)

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 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 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 ((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 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 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 (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 Message-Passing Ship Implementation x-pos: 0 y-pos: 0 time-left: 2 move: count-down: dispatch: my-ship

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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))