Anda di halaman 1dari 40

Object Oriented Design Basics

Traditional System Development

Requirements

Design

Implementation

Test
Problems with Traditional Systems

Long projects - requirements change

System does not correspond to specification

No results until the end

Difficult/Expensive to manage large project

Hard/Expensive to change requirements during project

Limited reuse

Difficult/Expensive to adapt new technology

Large systems become too complex


What is Object Orientation?
Structuring technique

Centers around objects


 Analysis and design should center around objects instead of
functions. The objects correspond to the real-world objects that are
processed by the system
 Objects answer the question of what the system processes instead
of, as in traditional systems, answering the question of how
something should be processed
 Objects are more stable than functions. We are less likely to
change the concepts the system works with than we are to change
the algorithms the work is done with
Features of OO

Modeling
 An OO model is easier to understand than a traditional model. It
also has a better correspondence with both the reality it models
and the code produced
Modularization
 In a function-oriented system all functions are operations on
common data. An OO system consists of a number of objects,
each of which is a module of its own. Each object has its own data
and operations to process it
Productivity
 By reusing code and creating class libraries, productivity can be
increased
Key Features of OO Software
Development listed:
OO focuses on objects as the unit of computation and
abstraction

Objects are often derived from their real-world


counterparts

Objects communicate with each other using messages

Messages result in the invocation of methods

Methods perform the necessary actions for a system to


respond

Objects are grouped into classes for specification

Classes form the common building blocks


Why OO?
Change
 The dependencies between objects and between an object’s data
and its operations are few and simple. This encapsulation
philosophy makes the system easier to change since data is
encapsulated in the objects and can only be accessed through
their operations
Extension
 It’s easy to add new operations or new objects
Terminology
 Using the same terminology in every phase of development makes
the system easier to understand
What Is an Object ?

Philosophical definition: an entity that you can recognize

In object technology terms: an abstraction of a ”real


world” physical or conceptual object.

In business terms: an entity relevant to the business


domain

In software terms: a data structure and associated


operations
What Is an Object ?

An object represents something tangible, something that can be seen or


touched, or something that can be thought about.

Object-oriented programs consist of hundreds or thousands of objects.


These objects communicate with each other by sending messages from
one object to another. In a true object-oriented program, that is all you
have- a group of communicating objects. New objects are created when
needed, old ones are deleted, and existing objects carry out operations
by sending messages.
Object

 An object is an entity that collects data and code to


operate on the data
 Objects usually correspond to objects or concepts in
the real world
 Components of an object
State
Behavior
Identity
Object - Representation

Attributes
 Store the data in an object
 E.g. Id, Name, Description
Methods
 Represent the functionality
 Perform operations on the object’s attribute and call other objects
methods
 Methods represents the object’s behavior
 e.g. Create, Modify, Remove, GetAmount
Objects Perform Operations

Objects have behavior


 An object exists in order to provide behavior (functionality) to the
system
 Each distinct behavior is called an operation

Object : my blue pen Operation : Write


Object : Acme Bank ATM Operation : withdraw

An object can perform many operations. However it is important to


implement only the operations that are necessary for the system
being implemented.
Objects remember Values

Objects have knowledge about their current state


 Objects hold all knowledge within the system
 Each piece of knowledge is called an attribute
 Each attribute has a name, a type, and a value
 An object’s state is defined by its attribute values

Object my blue pen Attribute : ink amount


Object Acme Bank ATM Attribute : cash available
Object Modeling

When modeling an object, we only need to model the operations


and attributes which are important to the problem domain.

Consider the example of the ”my blue pen” example :


Real world operations we may not wish to model
Point with
Stir coffee
Play with

Real world attributes we may not wish to model


Length
Age
Manufacturer
Encapsulation
Encapsulation hides how things work and what they know
behind an interface-the object’s operations.
 Details of object implementation are hidden
 Objects expose an interface intended for use by client objects
 An object’s attributes are accessed via its methods
Acme Bank ATM is an object that gives its users cash:

- The ATM encapsulates this for its users

- Bypassing the encapsulation is bank robbery

Bypassing encapsulation in object-oriented programming is


impossible.
Benefits of Encapsulation

You can ignore the low level details of how things works
internally and think at a higher level of abstraction. This means
you can comprehend more objects, and thus understand more
complex systems.

Real world examples : Using the ATM, airplanes, microwave


oven

Implementation of the operations may change, and yet you


should still be able to use them in the usual manner (Example
change the software for the ATM etc)
Example objects

The customer Tricia McMillan

The invoice with no 992344

The order with no 93343

The customer Arthur Dent (another customer)

The product red sport car

The inventory called A1

...
Exercise: Spot the operations and
Attributes of the objects

My Pencil

My Ball Point Pen

My Cd Writer

My Bank Account

My car
How objects Interact - Messages

Objects communicate with each other by sending Messages

The message sender is requesting that the receiver perform the


named operation

The receiver executes a corresponding Method

This is similar to calling a procedure, except that the binding to


the actual method to be executed is more flexible.
The Donut Diagram

getAddress getAge()

 Name getAge Client, or


getName
Sender
 Address
 Birth date

setAddress setBirthday

Person Object
Objects Associate with Each Other
For on object to send a message to another, it must have
”visibility” to that object.

Often, objects achieve this through a link.

A link is simply a relationship between objects. In programming


terms, it may be implemented in various ways; for example, a
variable in one object containing a reference to the other
object.

Links are initially treated as bi-directional. (object a can send


message to object b and vice versa).

An object can associate with more than one objects.


Object Composition

Objects may be made up of (composed of) other objects

Objects may be part of other objects

This is known as aggregation.

A bank maybe as object

A bank may have an ATM, which may be an object

An ATM may have a key board, a card reader and a case drawer,
all of which may be objects.
More on composition
Analogous to real world objects

a car is composed of many other objects (such as engine,


transmission, suspension).

each of these objects is composed of other objects.

”part-of ” hierarchy.
Where Objects come from - Classes

A class is similar to a programmers own defined data type.

A class is a template from which instances (objects) are


created.

An object is said to be an instance of a class or to instantiate


the class

An object is always an instance of one class

It is possible to create any number of instances of objects


from a class and they will all be created with the same
properties
Classes

Analogy

An ”object factory” producing objects with identical internal


structure, implementation and external interface.

A cookie-cutter. Each time it is used, it creates an object that


has exactly the same structure, and can carry out exactly the
same operations.
Why do we need Classes ?

A class definition specifies the operations and attributes for


all instances of that class

A class describes a type. We can define the behavior and


structure of a group of objects, which all have the same
behavior and structure (only the values of their attributes may
vary)
Object versus Class
A class is similar to a programmer defined data type

An object is similar to a variable of this data type

There can be any number of objects created from one class

An object can only be created from one class

Classes are static definitions, which contain the specification


(attributes and operations) for a particular type of object.

Objects are the dynamic entities which are created at


runtime.
How do you identify a Class ?

Identify the common behavior and structure for a group of


objects.

Recognize a single coherent concept.

My Blue Pen ops: write, refill

attribs : ink amount, color of ink

Your Blue Pen ops: write, refill

attribs : ink amount, color of ink

Both these objects belong to the class pen


Exercise: What classes could these
objects belong to ?
My pencil

My ball-point pen

My bank account

My House

Me
Classification and inheritance

Classes do not stand alone – they are related to other classes


through inheritance

Classes are arranged in a classification hierarchy

Classification is a very old idea


 Its a very efficient way to represent knowledge
 Example : the classification system for planets and animals
Subclasses and Superclasses

In an inheritance hierarchy

- classes below are called Subclasses

- classes above are called Superclasses

Subclasses inherit (that is, automatically get) the:

- instance variable definition (attributes)

- method implementation (behaviors)

From their super class.

Superclasses are more generalized while sub classes will be


more specific.
Inheritance

There may be commonality between different classes.

Define the common properties in a superclass.

Example

Account

Savings account Checking account

The subclasses use inheritance to include those properties.


Example Class Hierarchy

Bank Account

Savings Account Checking Account Deposit Account

InterestCheckingAccount
More on Inheritance

Inheritance is one way of ”reusing” code

- Find an existing class that is almost what you need, and


specialize or extend it by creating a subclass.

More importantly, inheritance helps manage complexity and


reduce redundancy

- classification hierarchy makes system easier to


understand, by grouping similar classes

- redundant code can be ”factored out” into superclass


The “is-a-kind-of” relationship

A subclass object ”is a kind of” super class object

Subclass must have all the behavior of the superclass.

Inheritance is often called the ”is-a-kind-of” relationship. This is


because an object of the subclass is a kind of object of the
super class.
Polymorphism

Polymorphism means that the same operation exists in many


classes.

Each operation has the same meaning but carries out the
operation is its own unique way.

- Send ”Speak” to instances of Dog, Cat, and Child.

- Each has its own ”Speak” method, implemented


differently

The client (sender) object does not need to know what type of
object he is sending ”speak” to.

Benefits : Simplicity, flexibility


Polymorphism

Polymorphism means the same named operation exists in many


classes

- That is, each of several classes has a method by the


same name

Each class can carry out the operation is its own unique way

However, each implementation should have the same


semantics.

Ship Train Air Plane

“Load Passengers”
Example of Polymorphism

Sending ”calculateInterest()” to instance of:

- SavingsAccount

- DepositAccount

- CheckingAccount

In java:
Account.calculateInterest();

No need to test for a ”type” field, create a case statement, and so on.

Anda mungkin juga menyukai