Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

16
Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming

Transcript of Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

Page 1: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

Introduction to Object-oriented Programming

CSIS 3701: Advanced Object Oriented Programming

Page 2: 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

Page 3: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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?

Page 4: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 5: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 6: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 7: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 8: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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…

Page 9: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 10: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 11: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 12: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 13: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 14: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

Abstraction in Design

• UML: Universal Modeling Language– Common representation for design at abstract level– Class types and relationships

Page 15: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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

Page 16: Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.

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• …