Introduction

Download Report

Transcript Introduction

Object Orientation Review

Object Orientation

• The most important properties of object orientation are: – Encapsulation – Information/implementation hiding – Polymorphism (typically refers to run-time poly) – Inheritance – Classes – State memory – Messages (method invocations) – Genericity © 2002, Bruce M. Reynolds & Cliff Green 1

Object Orientation Review

Object Orientation

• A language is object-oriented if it supports all or most of these • The C++ programming language supports all of these • What object-oriented programming is NOT: – C code recompiled with a C++ compiler – C code with some objects – Programming in an object-aware or object-oriented language © 2002, Bruce M. Reynolds & Cliff Green 2

Object Orientation Review

Polymorphism Can Have Multiple Definitions

• Polymorphism is a term used in multiple contexts: – Dynamic / Run-time polymorphism (traditional definition) – Function polymorphism (function overloading, evaluated at compile time) – Expression polymorphism (expression operands determine operation type, evaluated at compile time) – Generic polymorphism (new types or functions created at compile time, e.g. C++ templates) • This course uses polymorphism usually in the traditional sense (run-time selection of operations based on derived type) © 2002, Bruce M. Reynolds & Cliff Green 3

Object Orientation Review

Encapsulation

• Packaging together of related entities – Treat collections of packaged items collectively – Usually the nouns in a problem domain – These are the objects • Encapsulation is a design approach which – Closely binds the implementation of a class to its data representation – Provides a public interface to the data type’s behavior © 2002, Bruce M. Reynolds & Cliff Green 4

Object Orientation Review

Information Hiding

• Information hiding – Make unnecessary details inaccessible – Details are “unnecessary” if they are not intrinsic to the essence of the object • Information hiding reduces complexity – the user of the object doesn’t have to worry about how objects are implemented – the designer of the system does not have to worry about how other objects interact with hidden members and methods © 2002, Bruce M. Reynolds & Cliff Green 5

Object Orientation Review

Information Hiding

• The public interface is how users interact with the object – Must be well-defined – Must be easy to understand – Must be well-thought out • Users may only access the object through its public interface – No access to internals – Internals are a black box • As long as the public interface stays the same, the object can be changed internally while users of the object will be unaffected!

© 2002, Bruce M. Reynolds & Cliff Green 6

Object Orientation Review

Polymorphism

• Definition of polymorphism – The ability of different kinds of things (objects,

functions) to respond differently to the same message. Overloaded functions are a simple form of compile-time polymorphism.

Alternatively, the ability to use an object (or function)

without having to know exactly what type of object is being used (or which function is being called).

© 2002, Bruce M. Reynolds & Cliff Green 7

Object Orientation Review

Polymorphism

• Polymorphism allows a derived class object to be passed to a function as if it was an object of the base class. The user of the class may be unaware that the derived class exists • Users can use an object without really knowing the details of exactly to which type of object they are sending messages • Objects of a derived class can be substituted for objects of a base class without any changes to the functions that use those objects © 2002, Bruce M. Reynolds & Cliff Green 8

Object Orientation Review

Polymorphism - example

• “Steering” is different for various vehicles, but the purpose is the same.

• “Steering” a vehicle – Turn the handlebars of a bicycle – Turn the steering wheel of an automobile – Move the tiller of a boat • Users can “steer” an object without knowing the type of the object they are “steering”.

• Leave the implementation of “steering” to the object.

© 2002, Bruce M. Reynolds & Cliff Green 9

Object Orientation Review

Inheritance

• Ability to derive new objects from existing objects • Ability to extend and enhance systems of objects – Does not affect the existing hierarchy – Do not have to change existing user code • Allows sharing of operations among related objects – Objects of the derived class may use methods defined in the base class – All of the data structures and services belonging to the base class automatically become a part of the derived class © 2002, Bruce M. Reynolds & Cliff Green 10

Object Orientation Review

Classes

• Classes – A class is a stencil from which objects are created (instantiated). – Each object has the same structure and behavior of the object from which it is instantiated.

– Template classes in C++ provide the ability to generate new classes at compile-time.

© 2002, Bruce M. Reynolds & Cliff Green 11

Object Orientation Review

State Memory

• State Memory – State persists for the lifetime of the object.

– Methods (member functions) can change the state.

© 2002, Bruce M. Reynolds & Cliff Green 12

Object Orientation Review

Messages, Genericity

• Messages – A message is the way a sender object / entity conveys to a target object TargObj a demand for object TargObj to apply one of its methods – Don’t confuse method invocations with inter-process or network messaging.

• Genericity – The construction of a class so that one or more of the data types it uses internally is supplied at compile-time – Allows container classes (set, array, stack, queue) to enforce type safety – Templates in C++ © 2002, Bruce M. Reynolds & Cliff Green 13

Object Orientation Review

Characteristics of good objects - abstraction

Abstraction is a simplified view of an object in the users’ own nomenclature.

• Abstraction – Focus on essential details – Ignore the non-essential – Coalesce many underlying details into one simpler higher-level entity • Abstract Data Types – encapsulate characteristics – define methods, which define the behavior of the type © 2002, Bruce M. Reynolds & Cliff Green 14

Object Orientation Review

Object Relationships

• Common relationships between objects include – Dependency (an object uses and depends on another object, e.g. in a method parameter list) – Association (an object attribute is related to another object) – Aggregation (whole-part relationship, lifetime independent) – Composition (whole-part, lifetime of part managed by whole) – Inheritance (super / sub class, tightest relationship) © 2002, Bruce M. Reynolds & Cliff Green 15

Object Orientation Review

Models and UML

• Model describes a problem or provides a description of the solution • UML is becoming a commonly used modeling language and notation • Methodology includes a language for modeling (e.g. UML), meta model (describes modeling language), and a process • Three Amigos and others promoted and standardized UML © 2002, Bruce M. Reynolds & Cliff Green 16

Object Orientation Review

OO Goals

• Correct (conforms to requirements) • Reliable (doesn't crash, not brittle) • Robust (handle unexpected events) • Extensible and maintainable (add new features easily, easy to understand) • Re-usable (including frameworks, design patterns, component objects) • Efficient • Portable • Delivered on time and budget © 2002, Bruce M. Reynolds & Cliff Green 17

Object Orientation Review

Summary

• Objects – collect together groups of inherently related characteristics, attributes, and operations – Objects present simple and natural public interfaces to users, hide complexity inside, and protect the user from confusing and unnecessary details.

• Object-oriented approaches – are more robust and reusable because objects model the real-world. The problem specification may change, but the real-world tends to stay the same.

– manage data complexity better than other traditional approaches © 2002, Bruce M. Reynolds & Cliff Green 18