Introduction to Object-oriented Programming

Download Report

Transcript Introduction to Object-oriented Programming

Introduction to Object-oriented
Programming
CSIS 3701: Advanced Object Oriented Programming
The “Software Crisis”
Most “real world” programs
• 10,000 – 10,000,000 lines long
• Take months or years to develop
• Created by dozens or hundreds of programmers
– Many are added to or leave the project throughout
development
• Modified over time as customer needs change
Abstracton
• Ability to use tool without having to understand
how it works
– Example: can drive car without understanding physics
of internal combustion, electronics, etc.
• Functional abstraction:
y = sqrt(x);
– Do you know how C computes square root?
– Do you need to?
Objects
• Object-oriented classes are abstractions
Object
“Client
programmer”:
Programmer who
uses class in their
own code
Methods to
access state
of object
Only has to
understand how to
call methods, not how
they work
Current state
of object
Does not have to
understand internal
representation of
object state
Abstract Data Types as Objects
• Example: Stack abstract data type
Stack
Programmer
who uses
Stack class
top
void push(int)
int pop()
boolean isEmpty()
contents
Only has to
understand how these
affect abstract
concept of a “stack”
Internal representation
could be array, linked
list, etc.
Examples of Objects
• GUI components:
– Attributes: width, font, text, location, etc.
– Methods: setText, getText, show, hide, etc.
– Can use without knowing how drawn by OS
• Problem domain classes:
– Example: Order class for financial system
– Attributes: order#, item, quantity, totalCost, …
– Methods: getNewOrderNumber, setItem, setQuantity…
Constructors and Abstraction
• User should not have to understand internal
representation to create an object
• Constructor: code executed automatically at
object startup to define initial state
– Default values
– Parameters passed to constructors
– Values read from file
Objects vs. Classes
• Class defines:
– Attribute types (int top, String[] contents)
– Code for methods (push, pop, isEmpty)
• Object is an instance of a class
– Constructed from class
– Each may have different attribute values
stoogesStack
top: 3
contents:
[“Larry”,
“Curley”,
“Moe”]
bedrockStack
top: 2
contents:
[“Fred”,
“Barney”]
Large-Scale Programming
• Abstraction key to large scale programming
– No individual can understand entire system
– Need to understand your subsystem
– Need to know how to use methods in other modules it
interacts with
Other
module
methods
Your
module
Other
module
methods
Other
module
methods
3-Tier Architecture
User Interface
Business Logic
Data Access
Order
Database
Product
Database
UI developers just
need to know UI
design and how to
call business logic
methods
Business logic
developers just need
to know business
model, how will be
called by UI, and how
to call data access
methods
Data access
developers just need
to know SQL and
database design and
how will be called by
business logic
Abstraction in Design
• UML: Universal Modeling Language
– Common representation for design at abstract level
– Class types and relationships
Design Patterns
• Abstract designs that can be reused in different
systems
• Class design level:
– Typical types of classes
– Typical methods within those classes
• System architecture level:
– Typical ways to organize classes
– Example: 3-Tier model
User Interface
Business Logic
Data Access