elements of oo abstraction encapsulation modularity hierarchy: inheritance & aggregation 4...

27
Elements and Principles of Object Orientation

Upload: chester-denis-black

Post on 05-Jan-2016

247 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Elements and Principles of

Object Orientation

Page 2: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Elements of OOAbstractionEncapsulationModularityHierarchy: Inheritance & Aggregation 4 major/essential elements 3 minor/helpful elements

TypingConcurrencyPersistence

Page 3: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

collaboration

contract

AbstractionAn abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provides crisply defined conceptual boundaries, relative to the perspective of the user.

Focuses on outside view of an object:Behavior/implementation separation

Specifies responsibilities of objects of the same kind

Abstraction

characteristicsresponsibilities

Defines a contract on which other objects may dependAbstractio

ncharacteristicsresponsibilities

clientserver

Every object collaborates with other objects to achieve some behavior

Page 4: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Abstraction

perspective of the user

essential characteristics

kinds of objects

crisply defined conceptual boundaries

Page 5: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Encapsulation Encapsulation is the process of

compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.

Works hand-in-hand with abstraction:For abstraction to work, implementations must be encapsulated.Intelligent encapsulation localizes

design decisions that are likely to change.

Page 6: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Encapsulation

So its client doesn’t need to care about its inner works.

A client will not notice it even if its Dog Taunter is replaced.

Page 7: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

ModularityModularity is the property of a system

that has been decomposed into a set of cohesive and loosely coupled modules.

The essential method that we use to manage complexity is “divide and conquer”

The OOA process leads to a set of abstractions used in the domain.

The OOD process leads to a set of program modules that can be compiled separately, but which have connections with other modules.

Modules: - Conceptual breakdown of a system: subsystems, components, objects- Physical containers of program: packages, classes, etc

Objects are too tiny and can

hardly be used alone

Objects within the same

component are closely related

Objects within the same

component are loosely related

Page 8: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Modularity

Page 10: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Cohesion & Coupling

Page 11: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Hierarchy Hierarchy is a ranking or ordering of abstractions.

The two most important hierarchies in a complex system areits class structure (the “is-a” hierarchy)andIts object structure (the “part-of” hierarchy)

Class structure (generalization/specialization)

Object structure (aggregation/composition)

Page 12: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Generalization/Specialization

Page 13: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Aggregation/Composition

Page 14: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Typing Typing is the enforcement of the class of an

object, such that objects of different types may not be interchanged, or at the most, they may be interchanged only in very restricted ways.

An object-oriented language can be strongly typed, weakly typed, or even untyped.

An object-oriented language can support dynamic typing (or binding) or only static typing (or binding).

Benefits of strong typing:-Prevents programs from “crashing” mysteriously-Early error detection is extremely valuable-Helps to document programs-Helps compilers generating more efficient object code

Polymorphism, made possible by late binding, is the most powerful feature of OOP languages next to their support for abstraction; and is a central concept in OOD.

Page 15: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Typing

Page 16: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Concurrency Concurrency is the property that distinguishes an active object from one that is inactive.

Physical concurrency – using multiple CPUs . Vs. Illusionary concurrency – using a single CPU with time-slicing.

Heavyweight concurrency – using multiple processes. Vs. Lightweight concurrency – using multiple threads within a single O/S process.

Whereas OOP focuses on data abstraction, encapsulation, and inheritance, concurrency focuses on process abstraction and synchronization.

Each object (drawn from an abstraction of the real world) that represents a separate thread of control is called active.

Page 17: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Concurrency

Page 18: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Persistence Persistence is the property of an object through which its existence transcends time (i.e., the object continues to exist after its creator ceases to exist) and/or space (i.e., the object’s location moves from the address space in which it is created).

Page 19: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Persistence

Page 20: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Principles of OODThe Single-Responsibility Principle (SRP)The Open-Closed Principle (OCP)The Liskov Substitution Principle (LSP)The Dependency-Inversion Principle (DIP)The Interface-Segregation Principle (ISP)

Page 21: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Design SmellsPoor design smells, like a plastic sack of 12-day

old kitchen garbageRigidity = Hard-to-change: changing one place cause

never-ending succession of changes.Fragility = A change to one part of the system causes

it to break in many other, completely unrelated, parts.Immobility = Hard to disentangle the system into

units (or components) that can be reused in other systems

Viscosity = The development environment is held together with duct tape and library paste. It takes forever to go around the edit-compile-test loop.

Page 22: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

Design Smells (cont’d)Needless Complexity = There are a lot of very

clever code structures that are not actually necessary right now, but could be very useful one day.

Needless Repetition = The code looks like it was written by two programmers named Cut and Paste.

Opacity = Elucidation of the originator’s intent presents certain difficulties related to convolution of expression.

Mismanaged dependencies

Spaghetti code

Page 23: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

The Single Responsibility PrincipleSRP = A class should have only one reason

to change.

Violation of SRP

Not cohesive

Use cohesive classes, each

focuses on a single topic

Page 24: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

The Open-Closed PrincipleOCP = Software entities (classes, modules,

functions, etc) should be open for extension, but closed for modification.

Page 25: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

The Liskov Substitution PrincipleLSP = subtypes must be substitutable for

their base types.

Page 26: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

The Dependency Inversion PrincipleDIP =

High-level modules should not depend upon low-level modules. Both should depend upon abstractions.

Abstractions should not depend upon details. Details should depend upon abstractions.

Page 27: Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency

The Interface Segregation PrincipleISP = client should not depend upon

methods that they do not use.