CS 535 Object-Oriented Programming & Design Spring Semester, 1999 Intro Lecture |
||
---|---|---|
© 1999, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 26-Jan-99 |
Introduction
History and Languages
1967 Simula
1970 to 1983 Smalltalk
1979 Common LISP Object System
1980 Stroustrup starts on C++
1981 Byte Smalltalk issue
1983 Objective C
1986 C++
1987 Actor, Eiffel
1991 C++ release 3.0
199x Volume of OO books/articles
deforests Oregon
1995 Design Patterns
1996 Java
1983 to 1989 Language books with OO concepts
1989 to 1992 Object-oriented design books
1992 to present Object-oriented methodology books
Other Languages
Java
Self
Python
Perl
Prograph
Modula 3
Oberon
Scheme
Smalltalk Venders
ParcPlace, IBM, Quasar, Disney
Prolog++
Ada 95
Object Pascal (Delphi)
Object X, X = Fortran, Cobol, etc.
Methodologies
Approach to developing software
Methodologies encompass
Goal of the Course
Producing good objects
Meyer's Criteria for Evaluating for Modularity
Decomposability
Decompose problem into smaller subproblems
that can be solved separately
Example: Top-Down Design
Counter-example: Initialization Module
Meyer's Criteria for Evaluating for Modularity Composability
Freely combine modules to produce new systems
Examples: Math libraries
Unix command & pipes
Meyer's Criteria for Evaluating for Modularity Understandability
Individual modules understandable by human reader
Counter-example: Sequential Dependencies
Meyer's Criteria for Evaluating for Modularity Continuity
Small change in specification results in:
Changes in only a few modules
Does not affect the architecture
Example: Symbolic Constants
const MaxSize = 100
Meyer's Criteria for Evaluating for Modularity Protection
Effects of an abnormal run-time condition is confined to a few modules
Example: Validating input at source
Principles for Software Development
KISS Keep it simple, stupid
Supports:
Small is Beautiful See page 185 of Object-Oriented Software Development: A Practical Guide more information about these guidelines.
Upper bound for average size of an operation
Language | Lines of Code |
Smalltalk | 8 |
C++ | 24 |
Applications of Principles
First program:
class HelloWorldExample { public static void main( String args[] ) { System.out.println( "Hello World" ); } }
Objects and Other Basics
Major Concepts
Abstraction
“Extracting the essential details about an item or group of items, while ignoring the unessential details.”
Edward Berard
“The process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use.”
Richard Gabriel
Example
Pattern: Priority queue Essential Details: length items in queue operations to add/remove/find item Variation: link list vs. array implementation stack, queue
Encapsulation
Enclosing all parts of an abstraction within a container
Information Hiding
Hiding parts of the abstraction
Class & Object
Class
Class & Object Example class Stack { private float[] elements; private int topOfStack = -1; public Stack( int stackSize ) { elements = new float[ stackSize ]; } public void push( float item ) { elements[ ++topOfStack ] = item; } public float pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } public boolean isFull() { if ( topOfStack >= elements.length ) return true; else return false; } }Objects Stack me = new Stack( 20 ); Stack you = new Stack( 200 ); me.push( 5 ); you.push( 12 ); System.out.println( me.pop() );
Design (Conceptual) and Language Level Classes
Each object-oriented language has a class language construct
Features of this construct vary among languages
Examples
Language |
|
|
|
Methods |
Fields |
C++ |
Public,
protected, private
|
Public,
protected, private
|
Java |
Public,
protected, private, package
|
Public,
protected, private, package
|
Smalltalk |
Public |
Private |
Example of the Class Construct without Abstraction
class StackData { private float[] elements = new float[100]; private int topOfStack = -1; public int getTopOfStack() { return topOfStack; } public void setTopOfStack( int newTop ) { topOfStack = newTop; } public float getElement( int elementIndex ) { return elements[ elementIndex ]; } public void setElement( int elementIndex, float element ) { elements[ elementIndex ] = element; } }
Polymorphism
Polymorphism is the ability of instances of two or more classes to respond to the same message
In languages like Java and C++, inheritance (or implementation of interfaces) is required to use polymorphism
Example public class Parent{ public void foo() { System.out.println( "Parent foo" ); bar(); } public void bar(){ System.out.println( "Parent bar" ); } } public class ChildFoo extends Parent{ public void foo(){ System.out.println( "ChildFoo foo" ); bar(); } } public class ChildBar extends Parent{ public void bar(){ System.out.println( "ChildBar bar" ); } }What Output is Possible Here? Parent test = getAParentOrChildObject(); test.foo();
Polymorphism and C++
See http://www.eli.sdsu.edu/courses/fall95/cs596_1/notes/Poly/Poly.html for a more complete example of polymorphism and C++
#include <iostream.h> class Parent { public: void virtual foo(); void virtual bar(); }; void Parent::foo() { cout << "Parent foo" << endl; bar(); } void Parent::bar() { cout << "Parent bar" << endl; } class ChildFoo : public Parent { void virtual foo(); }; void ChildFoo::foo() { cout << "ChildFoo foo" << endl; bar(); } class ChildBar : public Parent { void virtual bar(); }; void ChildBar::bar() { cout << "ChildBar bar" << endl; }What Output is Possible Here?
Parent* test = getAPointerToAParentOrChildObject(); test->foo();
Simplistic Example
Last Federal Virtual Bank wants its entire banking software redone in Java, so it can shut down its branch offices and perform all its business via the WWW.
Customer records include the customers name, address, personal information, passwords, etc. and accounts.
The bank offers various types of accounts: checking, savings, CD, Junior savings accounts.
Bank officers will introduce new types of accounts from time to time.
Checking account charge 12 cents/check unless the average account balance for the month is greater than $1,000. The first three ATM transactions per month are free. Additional ATM transactions cost 50 cents.
All transactions over $1,000 dollars must be reported to the federal government.
Junior savings accounts are for children under the age of 13. Children can deposit any amount into the account. Deposits of over $100 are reported to their parents. Children can withdrawal up to $10 from the account. Individual withdrawals over $10 must be verified by a parent. Total withdrawals over $100 per month are reported to the parents.
Transactions include time & date, amount, location, type of transaction, account number and teller.
Banking Classes
Customer
Transaction
Currency
Account
abstract class account { public static final boolean VALID = true; protected Customer accountOwner; protected Currency balance; protected Stack accountHistory; // Other fields left out public void processTransaction( Transaction accountActivity ) { if ( validateTransaction( accountActivity ) == VALID ) commitTransaction( accountActivity ); else // report the problem. Code not shown } protected abstract boolean validateTransaction( Transaction accountActivity ); protected abstract void commitTransaction( Transaction accountActivity ); // other methods left out }
Account Class Inheritance
Polymorphism
Account newCustomer; newCustomer = magicFunctionToCreateNewAccount() newCustomer.processTransaction( amount );Which processTransaction is called?
Adding new types of accounts to program requires:
Modular Design Rule
Avoid Case (and if) Statements
switch ( newCustomer.accountType ) { case SAVINGS: // Code to process Savings account case CD: // Code to process CD account case CHECKING: // Code to process Checking account // etc. }
newCustomer.processTransaction( amount );
|
|
Stages of Object-Oriented Expertise
From Object-Oriented Software Development: A Practical Guide
Novice
Duration: 3 to 6 months
"Novices spend their time trying to get their function-oriented heads turned around, so they can see the application object classes. They have a hard time finding the classes and write a lot of function-oriented code."
Apprentice
Duration: 3 to 6 months
"Apprentices begin to understand what object-orientation really means. They still prefer to work with others, who help foster an environment of discovery. Apprentices begin turning out good designs, although not consistently. Gurus and journeymen still need to keep an eye on apprentices."
Stages of Object-Oriented Expertise Journeyman
Duration: varies greatly, a least a year
"Journeyman have internalized the OO paradigm and work independently. They can provide key inputs during the design reviews. They still have mental roadblocks that may require a guru to break through."
Guru
Duration: lifetime
"For gurus, OO development comes naturally. A guru automatically sees things in an OO perspective, moving quickly through the phases of development during rapid iterations. A guru can move the project over mental hurdles, causing others to see something clearly that eluded them before."
"There are very few gurus in the world today. The rest of us will have to rely on proven processes and methodologies."
"I've yet to see someone successfully make it past the novice stage and even consider thinking about software in other than OO terms."
Copyright © 1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.
    visitors since 26-Jan-99    Next