Object Oriented Design
Advanced Programming
SUT • Spring 2019
Outline
-
Object Oriented Programming
-
Characteristics of objects
-
Interface
-
Encapsulation
Abstraction
Abstraction
-
Machine language
-
Assembly: an abstraction of the machine language
-
Many languages are abstraction of assembly language
-
Fortran, Basic, C
-
-
Big improvement
-
But they still require you to think in terms of the structure of the computer
-
Rather than the structure of the problem
Different Contexts
-
Problem Space
-
the place where the problem exists
-
such as a business
-
-
Solution Space
-
the place where you’re implementing that solution such as a computer
-
-
The effort required to perform this mapping
Library Problem
-
Suppose you want to write a library program
-
What are the elements of your program?
-
We think about functions and variables…
Object Oriented Approach
-
A step further
-
Represent problem space elements
-
This representation is general enough
-
Is not constrained to any particular type of problem.
-
The elements in the problem space and their representations in the solution space are referred to as “objects”
Object Oriented Principle
-
The program is allowed to adapt itself to the lingo of the problem
-
by adding new types of objects
-
when you read the code, you’re reading words that also express the problem.
-
This is a more flexible and powerful language abstraction
Object Oriented Languages
-
Smalltalk
-
The first successful object-oriented language
-
One of the languages upon which Java is based
-
-
Java
-
C++
-
C##
OOP vs. Procedural Approach
-
Elements of OOP
-
Objects
-
Message passing between objects
-
-
Elements of procedural programming
-
Functions
-
Variables
-
Function invocation
-
-
The way of thinking
-
Thinking about objects and relations
-
Thinking about functions and computer structure
-
OOP Characteristics
-
basic characteristics of Smalltalk:
-
Everything is an object
-
A program is a bunch of objects telling each other what to do
-
by sending messages
-
-
Each object has its own memory
-
made up of other objects
-
-
Every object has a type
-
All objects of a particular type can receive the same messages
-
Booch’s description of an Object
-
An object has state, behavior and identity
-
Booch added identity to the description
-
An object (may) have internal data
-
-
which gives it state
-
An object (may) have methods
-
to produce behavior
-
-
And each object can be uniquely distinguished from every other object
-
Each object has a unique address in memory
-
Interface
-
Each object can satisfy only certain requests
-
The requests you can make of an object are defined by its interface
-
The type is what determines the interface
Representation of a light bulb
public class Main {
public static void main(String[] args) {
// Turn On the Light
Light lt = new Light();
lt.on();
}
}
Person in an Education System
public class Main {
public static void main(String[] args) {
// Person in Education System
Person person = new Person();
person.setName("Taghi Taghavi");
person.setPhoneNumber(66166601L);
person.showInformation();
}
}
New names in OOP
-
Function: Method, Service
-
Variable: Property, State
Encapsulation
-
Commercial products are encapsulated
-
Remote control
-
TV
-
Cell phone
-
-
They are Black Boxes
-
Hidden Implementations
-
Public interface
Why Encapsulation?
-
Simplified use
-
Even for the producer
-
Open implementation: bad use
-
Hiding the implementation reduces bugs
-
It is more beautiful!
Object Encapsulation
-
Encapsulation of a problem-space concept into a class of objects
-
Define interface
-
Hide the implementation
-
Black box
-
The client may see the implementation
-
But can not use it directly
-
This is better even for the producer (programmer)
Access Control
-
Access to some parts of the class is restricted
-
Public and Private area of the class
-
The client of a class can use only public area
-
Public area = class interface
-
Public methods
-
Public variables
Example: Rectangle
-
Lets encapsulate a rectangle
-
What is a rectangle?
-
An object
-
Which has length and width (properties)
-
Lets you specify its length and width
-
Can calculate its area and perimeter
-
Class Declaration
public class Rectangle {
private int width, length;
public void setWidth(int w) {
width = w;
}
public void setLength(int l) {
length = l;
}
public int calculateArea() {
return width * length;
}
public int calculatePerimeter() {
return (width + length) * 2;
}
}
How to Use Rectangle?
public class Main {
public static void main(String[] args) {
// Rectangle Usage
Rectangle rect = new Rectangle();
rect.setWidth(2);
rect.setLength(7);
System.out.println(rect.calculateArea());
System.out.println(rect.calculatePerimeter());
}
}
Title Text
Object Oriented Design
By Behnam Hatami
Object Oriented Design
Object Oriented Design / Advanced Programming Course @ SUT, Spring 2019
- 1,185