I. the object-oriented paradigm A. What is a paradigm? (in design/programming) ------------------------------------------ THE OBJECT-ORIENTED (OO) PARADIGM A paradigm is: - how to go about design, coding how to approach problems in design how to *organize* programs (style) - a set of tools variables, procedures, loops, ... - a set of questions how to evaluate a design/program? what makes a good/bad program? ------------------------------------------ B. Stepwise refinement (top-down design) 1. what it is ------------------------------------------ STEPWISE REFINEMENT (Wirth, 1971) How to approach problems in design: - starts with specification - refines algorithms and data - done when all instructions expressed in programming language How to organize programs: refinement of algorithms: procedures (nesting in Pascal) refinement of data: data structuring primitives (arrays, records in Pascal) ------------------------------------------ 2. limitations: change ------------------------------------------ PRACTICAL LIMITATIONS OF STEPWISE REFINEMENT Not designed to handle change: requirements -> specs -> code -> test -> deliver change in requirements ==> redesign ------------------------------------------ C. why is change important? ------------------------------------------ CHANGE IS IMPORTANT Because: ------------------------------------------ D. the vision ------------------------------------------ THE VISION OF OO DESIGN METHODS Want software that: - can - has LIMITATIONS/COMPARISON OO | Stepwise refinement =================================== ------------------------------------------ The big question for us is, what are the limits of the OO paradigm? II. concepts of OOP A. Objects = hidden data + operations (or data structure + algorithm) ------------------------------------------ OBJECTS Object = instance = object Examples: rep | ops ======================================== Integer Stack Person Record ------------------------------------------ ------------------------------------------ A STACK OBJECT ______________________ | | | _______ push: | | | | | |_____| | | | 5 | pop | |_____| | | | 4 | | | |_____| top | | 1 | | | elems |_____| | | _______ new | top | 3 | | | |_____| | |______________________| state = instance variables operations (methods) = code that responds to messages ------------------------------------------ B. Message Passing (generic invocation, dynamic binding) 1. message = operation name + arguments ------------------------------------------ MESSAGE PASSING ___________ ____________ | | | \ / | __ | | | \ / | | push: | \/ | __ | | | push: 1 | | | |__________| | pop \ \ \ | | | | | top | | |___________| object ^ message ^ message = message send = ------------------------------------------ 2. Message Passing and binding times (omit if discussed already) ------------------------------------------ MESSAGE PASSING AND BINDING TIME static (early) binding: call bound to procedure at compile time specific implementation selected at link time dynamic (late, delayed) binding: call bound to procedure at EXAMPLE IN SMALLTALK (MessageBox confirm: 'use Stack?') ifTrue: [myStack := Stack new] ifFalse: [myStack := LStack new]. myStack push: 1 ------------------------------------------ How is dynamic binding different than overload resolution, as in Haskell? How can a language deal with different representations of objects all in the same way? C. Classes ------------------------------------------ REPRESENTATION OF OBJECTS class = class object = ------------------------------------------ ------------------------------------------ // a class, in C++, for Stacks. #include "List.h" template class Stack { private: List *elems; public: Stack() : elems(new List) {} virtual void push(Elem anElement) { elems->addFirst(anElement); } virtual void pop() { elems->removeFirst(); } virtual Elem top() { elems->first(); } }; // instance creation Stack *aStack = new Stack(); ------------------------------------------ ------------------------------------------ "a class, in Smalltalk, for stacks" Collection subclass: #Stack instanceVariableNames: 'elems' classVariableNames: '' poolDictionaries: '' category: 'Examples-541' "class methods" new ^ self basicNew initialize "instance methods" initialize elems := OrderedCollection new push: anElement elems addFirst: anElement pop elems removeFirst top ^ elems first "instance creation" |aStack| aStack := Stack new ------------------------------------------