View
260
Download
0
Category
Tags:
Preview:
Citation preview
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?
Large-Scale Programming
• Abstraction key to large scale programming– No individual can understand entire system– Just need to understand your subsystem– Need to know how to use methods in other modules it
interacts with
Yourmodule
Othermodulemethods
Othermodulemethods
Othermodulemethods
Data Access
3-Tier ArchitectureUser Interface Business Logic
OrderDatabase
ProductDatabase
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
Objects
• Object-oriented classes are abstractions
“Client programmer”:
Programmer who uses class in their own code
Methods toaccess state
of object
Current stateof object
Object
Only has to understand how to call methods, not how they work
Does not have to understand internal representation of object state
Abstract Data Types as Objects
• Example: Stack abstract data type
Programmer who uses Stack class
void push(int)int pop()boolean isEmpty()
Stack
Only has to understand how these affect abstract concept of a “stack”
Internal representation could be array, linked list, etc.
top
contents
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…
Objects vs. Classes
• Class defines:– Attribute types (int top, String[] contents)– Code for methods (push, pop, isEmpty)– Created by developer and added to “library”
• Object is an instance of a class– Constructed from class by “client programmer”– Each may have different attribute values
top: 3contents: [“Larry”, “Curley”, “Moe”]
top: 2contents: [“Fred”, “Barney”]
stoogesStack bedrockStack
Constructors and Abstraction
• Other programmers 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
Reuse
• Should never have to rewrite code from scratch• Reuse in maintenance
– Must be able to add abilities to new version without causing faults in existing code
– Otherwise, will have to redesign/retest entire program for each modification!
Existing code from previous version
New code to addnew abilities andfeatures
New code affects existing code
Reuse
• Reuse in version design– Multiple products may share common features– If those features changed, must propagate to all products– Ideally do with single change
Common featuresof Office products(file handling, etc.)
Word Excel PowerPoint
Change to file handling
Java Programming
• Language specifically designed for object-oriented programming
• Abstraction:– Everything is an object– Encapsulation of internal state– Polymorphism/interfaces for abstract containers
• Reuse:– Inheritance of properties between classes
Abstraction in Design
• UML: Universal Modeling Language– Common representation for design at abstract level– Class types and relationships
Reuse in Design
• Abstract design ideas reused in different systems• Design patterns:
– Classes used to solve common problems– Example: Adaptor pattern to map to another protocol
Reuse in Design
• Class design level:
– Typical types of classes
– Typical methods within those classes• Methods to set values of member variables (with validation)• Methods to access (without changing) member variable
values• Methods to display state of object as a string• …
Recommended