Top Banner
Object-Orientation Concepts Lukito Edi Nugroho Department of Electrical Engineering Gadjah Mada University
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lukito Edi Nugroho - Object Orientation Concepts

Object-Orientation Concepts

Lukito Edi Nugroho

Department of Electrical Engineering

Gadjah Mada University

Page 2: Lukito Edi Nugroho - Object Orientation Concepts

Software Complexity

• Why software is inherently complex ?– The complexity of problem domain

• “Impedance mismatch” between users and developers: each group lacks of expertise in the domain of the other

• Software requirements often change during development– The difficulty of managing the development process

• Software engineers have to “engineer the illusion of simplicity”

– Flexibility through software• Software is a reflection of a mental product, it allows any kind

of abstraction imaginable by its creator

Page 3: Lukito Edi Nugroho - Object Orientation Concepts

Software Complexity

Page 4: Lukito Edi Nugroho - Object Orientation Concepts

Attacking Complexity

• “Divide et impera” principle decomposition– Breaking a complex problem into smaller and manageable parts– The size of a subpart must not exceed the capacity of human

cognition• Algorithmic decomposition

– Decomposition is based on process • A module denotes an activity• Smaller parts compose a larger process

• Object-oriented decomposition– Decomposition is based on key abstractions in the problem

domain– The world is viewed as a set of autonomous agents that

collaborate to perform some higher level behaviour

Page 5: Lukito Edi Nugroho - Object Orientation Concepts

Algorithmic Decomposition

Page 6: Lukito Edi Nugroho - Object Orientation Concepts

Object-Oriented Decomposition

Page 7: Lukito Edi Nugroho - Object Orientation Concepts

The Object Model

• Characteristics of object-orientation– Abstraction– Encapsulation– Modularity– Hierarchy

Page 8: Lukito Edi Nugroho - Object Orientation Concepts

The Object Model

• Abstraction– Denotes the essential characteristics of an object that distinguish

it from all other kinds of objects– Provides crisp definition of conceptual boundaries, relative to the

perspective of the viewer – Focuses on the outside view of an object, separating its

essential behaviour from its implementation– The most important issue in design is to decide upon the right

set of abstractions for a given problem domain

Page 9: Lukito Edi Nugroho - Object Orientation Concepts

Abstraction

Page 10: Lukito Edi Nugroho - Object Orientation Concepts

The Object Model

• Encapsulation– The process of hiding all details of an object that do not

contribute to its essential characteristics– Abstraction and encapsulation are complementary

• Abstraction focuses on the outside view• Encapsulation prevents clients from seeing its inside view

where the behaviour of the abstraction is implemented– Encapsulation provides explicit barriers among different

abstractions limiting the size of cognitive entities

Page 11: Lukito Edi Nugroho - Object Orientation Concepts

Encapsulation

Page 12: Lukito Edi Nugroho - Object Orientation Concepts

The Object Model

• Modularity– Modularity packages abstractions into discrete units– Modularity is the property of a system that has been

decomposed into a set of cohesive and loosely coupled modules• How modules are defined, i.e., how a system is partitioned• How a module sees another module

– In design, we strive for high cohesive modules and low coupling between modules

Page 13: Lukito Edi Nugroho - Object Orientation Concepts

Modularity

Page 14: Lukito Edi Nugroho - Object Orientation Concepts

The Object Model

• Hierarchy– Hierarchy is a ranking or ordering of abstractions– Two important hierarchies: the “kind-of” (class hierarchy) and

“part-of” (object hierarchy) relationships

Page 15: Lukito Edi Nugroho - Object Orientation Concepts

Hierarchy

Page 16: Lukito Edi Nugroho - Object Orientation Concepts

Objects

• The nature of an object– A tangible and/or visible thing, or may be

perceived intellectually– Has three essential properties

• State• Behaviour• Identity

Page 17: Lukito Edi Nugroho - Object Orientation Concepts

Objects

Page 18: Lukito Edi Nugroho - Object Orientation Concepts

Object State

• The state of an object encompasses:– The (static) properties of the object– Current (dynamic) values of each of these properties

• A property is an inherent or distinctive characteristics or feature that contributes to making an object unique

• A value could be simple or denote another object– Simple values are non-temporal, unchangeable, and non-

instantiated– Object values exist in time, are changeable, instantiated, and

can be created, destroyed, and shared

Page 19: Lukito Edi Nugroho - Object Orientation Concepts

Object State

• Example of object stateclass Circle { int radius; Color color; Circle(int r, Color c) { radius = r; color = c; } public setRadius(int r) { radius = r; } public setColor(Color c) { color = c; } } Circle circle = new(10,blue); circle.setColor(red);

Page 20: Lukito Edi Nugroho - Object Orientation Concepts

Object Behaviour

• No objects live in isolation– Objects are acted upon, and they act upon other objects

• Behaviour is how an object acts and reacts, in terms of state changes and message passing– Behaviour of an object is completely defined by its actions– At the programming level, behaviour is implemen-ted by

methods or member functions

Page 21: Lukito Edi Nugroho - Object Orientation Concepts

Object Behaviour

• Different kinds of operations– Modifier: alters the state of an object– Selector: accesses, but does not alter, the state of an

object– Iterator: permits all parts of an object to be accessed

in a well-defined order– Constructor: creates an object and initializes its state– Destructor: frees the state of an object and destroys

the object

Page 22: Lukito Edi Nugroho - Object Orientation Concepts

Object Behaviour

class Sensor { Tank tank; int alarm; void alarmOn() { tank.fill(maxvol); } } class Tank { int volume; void fill(int v) { for (int i=0; i<v; i++) v++; } }

sensor

alarmOn()

tank

fill(int v)

alarmOn()

fill(maxvol)

Page 23: Lukito Edi Nugroho - Object Orientation Concepts

Object Identity

• Identity is a property of an object which distinguishes it from all other objects

• It denotes a reference associated with the object it identifies– Identity is a property that shows that an object “exists”

• in real world (as an abstraction)• in memory (computer representation)

– An identity exists as long as the object it represents exists• Object creation always creates an identity• Object deletion destroys its identity

Page 24: Lukito Edi Nugroho - Object Orientation Concepts

Object Identityclass Circle { int radius; Color color; Circle(int r, Color c) { radius = r; color = c; } public setRadius(int r) { radius = r; } public setColor(Color c) { color = c; } } Circle circle1 = new(15,green); Circle circle2 = new(10,red); Circle circle3 = new(5,blue);

circle3

circle2

circle1

Page 25: Lukito Edi Nugroho - Object Orientation Concepts

Relationships Among Objects

• Types of relationships– “Using” relationship– “Containing” relationship

• “Using” relationship– Two objects have equal position in a hierarchy– Unidirectional message passing– Each object may play one of the following roles

• Actor: an object that can access other objects, but cannot be accessed by other objects

• Server: an object that can only be accessed by other objects• Agent: an object that can both access or be accessed by

other objects– Example: see the Sensor and Tank object

Page 26: Lukito Edi Nugroho - Object Orientation Concepts

Relationships Among Objects

• “Containing” relationship

class Machine { Carburetor carb = new Carburetor(); Plug plug = new Plug(); Piston piston = new Piston(); ... }

Machine

Carburetor

PlugPiston