Title: Object-Oriented Programming and Design
1Object-Oriented Programming and Design
- Ralph Johnson johnson_at_cs.uiuc.edu
2 Goals to learn
- Principles
- Patterns
- Practices
- Object think
- Design for reuse
3History of Objects
- Simula67
- Smalltalk 72,74,76,78,80
- 1985 C (Objective C, Object Pascal)
- 1986 OOPSLA
- 1990 COM, CORBA
- 1994 Design Patterns
- 1995 Java
- 2001 .NET
4Design Principles
- Encapsulate what varies
- Code to an interace rather than to an
implementation - Open-closed Principle Classes should be open to
extension and closed to modification - Dont Repeat Yourself (DRY)
- Single Responsibility Principle
- Liskov Substitution Principle Subtypes must be
substitutable for their base types
5Smalltalk
- Extreme object-oriented language
- Live objects
- Readable library
- Influencial, unknown
- Fun!
6From Smalltalk
- WIMP - Windows, Mice, Pointing
- Model/View/Controller
- Browsers
- CRC cards
- eXtreme Programming
- Automated refactoring tools
7Before objects
- Computer system consists of data and programs.
- Programs manipulate data.
- Programs organized by
- functional decomposition
- dataflow
- modules
8Object Paradigm
- Computer system consists of a set of objects.
- Objects are responsible for knowing and doing
certain things. - Objects collaborate to carry out their
responsibilities. - Programs organized by classes, inheritance
hierarchies and subsystems
9What is an object, anyway?
- Mystical view
- Computing systems are made up of objects that
communicate only by sending messages between each
other. All computation is message sending.
10What is an object, anyway?
- Scandinavian view
- A program is a simulation. Each entity in the
system being simulated is represented by an
entity in the program.
11What is an object, anyway?
- Programming language view
- An object-oriented system is characterized by
- data abstraction
- inheritance
- polymorphism by late-binding of procedure calls
12Heart of Object-Oriented Programming
- Don't make a new language, extend your old one.
- Objects should be abstractions of problem domain.
13Modeling
- All phases of software life-cycle are modeling
- analysis - modeling of problem
- design - modeling of solution
- implementation - making model run on a computer
- maintenance - fixing/extending your model
14Assumption about Modeling
- Basing system design on structure of problem
makes system - more reusable
- more extensible
15Modeling
- Claim people model the world with "objects"
- objects
- classes
- relationships between objects
- relationships between classes
16Modeling
- Advantages of object-oriented software
development - more natural - matches the way people think
- single notation - makes it easy to move between
software phases
17Objects and Relationships
- John is Mary's father. Mary is John's
daughter. - Bob is Mary's dog. Mary is Bob's owner.
- Ann is John's employer. John is Ann's employee.
18Objects and Attributes
- John's name is "John Patrick O'Brian".
- John's age is 27.
- John's address is 987 N. Oak St, Champaign IL
61820 - What about John's job? John's wife?
- What is an attribute, and what is a relationship?
19Objects and Behavior
- John goes on a trip.
- John makes reservations.
- John buys tickets.
- John travels by airplane.
- John checks into hotel.
20What Really is an Object?
- Reservation -- a promise to give service to a
customer - Ticket -- proof that customer has paid for
service in advance - Flight
- Payment -- an event (transaction?) in which money
is exchanged
21What Really is an Object?
- Anything we can talk about can be an object,
including relationships ("the husband of the
first party", "first-born son"). - What are we trying to model?
- Models should be as simple as possible, but no
simpler.
22Classification
- We naturally put objects into classes that have
similar characteristics. - John is a man.
- Mary is a woman.
- Bob is a dog.
- All women are people.
- All people are mammals.
Mammals
Dogs
People
Women
Men
Bob
Mary
John
23Classification
- John is an employee.
- John is a father.
- John is a sky-diver.
- John is under 30.
- Is John an instance of Employee, Father,
Sky-diver, and Under30?
24Summary
- Objects
- have identity
- have attributes
- have behavior
- have relationships with other objects
25Summary
- Classes
- describes the attributes, behavior, and
relationships of a set of objects - subclasses/superclasses form graph of
generalizations
26The class
- First month is Smalltalk
- Homework, often several per week
- Second half of the semester is group project
- See http//swiki.cs.uiuc.edu/cs598rej/MP0
- Make a page for yourself
- Download Squeak and play with it
27Smalltalk in a Nutshell
- Objects classes
- Messages methods
- Inheritance metaclasses
28Smalltalk Everything is an Object
- Application objects customer, inventory
- GUI objects button, text editor
- Foundation string, set, numbers, booleans
- Tools browser, debugger, compiler, GUI builder
- Language implementation class, method, context,
29Communicating with an Object
- All computation is done by objects.
- The only way to interact with an object is to
"send a message" to it. - Smalltalk has three kinds of syntax for sending a
message. - All messages have same implementation.
30Three Kinds of Message Syntax
- Unary messages
- aSalaryChange date
- Keyword messages
- earned at date add money
- Binary messages
- (worked - 40) max 0
31Sending a Message
- Object responds to message by looking in its
class for a method with the same selector. - If it doesn't find the method, it looks in its
superclass. - Repeat until it finds the method. If it never
does, there is an error.
32Smalltalk in a nutshell
- Classes have methods and variables.
- Methods are a sequence of messages, assignments,
returns. - Variables, literals, psuedovariables
- Blocks
- Metaclasses - classes have classes
33Message Lookup and Inheritance
- EmployeeTransaction has subclasses Paycheck,
SalaryChange, and Timecard. - EmployeeTransaction defines the instance variable
date and the method - date
- date
34EmployeeTransaction date
check1 07/09/95
Paycheck
aPaycheck date
35Smalltalk Expression Syntax
- Literals
- 3.675, 14, 'hello', weight, d,
- ( foo 'bar' 92)
- Assignments and variables
- v v 1
- Messages
36Smalltalk Expression Syntax
- Sequences. Blocks.
- x lt y ifTrue z x ifFalse z y.
- paychecks do each each post
- Cascades
- aSet add blue add red
37Smalltalk Method Syntax
- Returns
- socialSecurity federalTaxes stateTaxes
38Variables in Smalltalk
- blockArguments and temporaries
- methodArguments and temporaries
- instanceVariables
- Can be accessed only by the object's methods.
- Globals, class variables
- Any method in the scope can access it
- Variable is object of class Association
39Using Variables
printOnCheckStream aStream aStream cr
cr. aStream next 40 put (Character
space). DateFormat print date on
aStream. aStream cr. ...
40More variables
- test
- "PayrollSystem test"
- payroll day1 ralph
- day1 Date newDay 5 year 1996.
- payroll self new.
- ralph Employee new name
- 'Ralph Johnson'.
-
41(continued)
- ralph changeSalaryFor day1 to 20.
- payroll addEmployee ralph.
- self
- employee ralph
- hours self aLittleOvertime
- starting day1.
- payroll
42Initializing an Object
- Every object needs to be initialized.
Uninitialized variables are nil. - The initialization method often defines the type
of a variable. - Two methods one class and one instance.
43Class Methods
- Class is an object. It can have methods, too.
- For class Date class
- newDay dayInteger year yearInteger
- self new day dayInteger year yearInteger
44Instance initializing method
- For class Date
- day dayInteger year yearInteger
- day dayInteger.
- year yearInteger
45Creating a Date
Date newDay 3 year 1995 Date new day 3 year
1995
day
day 3 year 1995
year
3
1995
46Complete Smalltalk
- Expressions (method definition)
- message, assignment, sequence, block, return
- Variables, literals
- Classes, inheritance
- Class methods / instance methods
- Pseudovariables
- nil, true, false
- self, super, thisContext
47Smalltalk (the language) is trivial
- Complexity is class library.
- New language extensions fit in as well as numbers
and control structures. - Language extension gt core language is trivial
48Implications
- class library language extension
- gt
- must know class library
- must standardize class library
- merging class libraries is like merging language
extensions - hard to make class libraries
49Applications
- No programs, just objects
- No main routine
- Most applications create new classes, reuse a lot
of existing ones