object oriented programming d. place quakes, the university of queensland

44
Object Oriented Programming D. Place QUAKES, The University of Queensland

Upload: augustine-gibbs

Post on 19-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programming D. Place QUAKES, The University of Queensland

Object Oriented Programming

D. Place

QUAKES, The University of Queensland

Page 2: Object Oriented Programming D. Place QUAKES, The University of Queensland

Content

Introduction: component based des

ign

Object Oriented Programming Conc

epts

Performance issues

An example: LSMearth

Page 3: Object Oriented Programming D. Place QUAKES, The University of Queensland

Introduction

Page 4: Object Oriented Programming D. Place QUAKES, The University of Queensland

Software Requirements

RobustAvailability MaintainableRe-usableConcurrent development

Page 5: Object Oriented Programming D. Place QUAKES, The University of Queensland

Component based architecture

Minimize component dependency– Reduce complexity– Reusability– Concurrent development

“Simple” components– Robust

Hierarchical architecture– Easy to maintain

Page 6: Object Oriented Programming D. Place QUAKES, The University of Queensland

Basic design

From general to specific Base components

– Simple– Robust– Re-usable

Page 7: Object Oriented Programming D. Place QUAKES, The University of Queensland

LSM modules

Build on independent modules– Separate hardware dependence modules– Hidden complexity

• Model development does not require knowledge of MPI…

Page 8: Object Oriented Programming D. Place QUAKES, The University of Queensland

From Component Based to Object Oriented

Many languages (Fortran…) allow component based programming

OOP provides the same functionality but at a deeper level– Provides a hierarchy – No separation between data &

algorithms: provide the data with the functions to manipulate the data

Page 9: Object Oriented Programming D. Place QUAKES, The University of Queensland

Programming Language

Simula (1967)Smalltalk

– “Pure” object oriented programmingC++, Java

– Limited functionalityEiffel, ADA(~), Objective C, CLOS…

Page 10: Object Oriented Programming D. Place QUAKES, The University of Queensland

Object Oriented Programming Concepts

Page 11: Object Oriented Programming D. Place QUAKES, The University of Queensland

Object Oriented programming

Object ClassInheritance Polymorphism

Page 12: Object Oriented Programming D. Place QUAKES, The University of Queensland

Object

An object has– Properties (Variables / Data )

rP

move_to(Q)

Q

– Behaviour (Methods / Operators / Functions…)

Page 13: Object Oriented Programming D. Place QUAKES, The University of Queensland

Object & Class

An object is an instance of a class– For example:

•Real a -> a is an object, Real is a class

•Sphere s

Page 14: Object Oriented Programming D. Place QUAKES, The University of Queensland

Class

Specification of structure (instance variables), behaviour (methods), and inheritance (hierarchy).– Type of object– Classify objects by their

properties and/or behaviour

Page 15: Object Oriented Programming D. Place QUAKES, The University of Queensland

Classes (example)

Class Particle { Vector3D Position; double radius;

} ;

Class Particle { Vector3D Position; double radius;

} ;

Particle & move_to(Vector3D &Q) ;

Particle(Vector3D &P,real r) ; ~Particle() ;

Page 16: Object Oriented Programming D. Place QUAKES, The University of Queensland

Instances of Class

Vector3D A(0,0,0), B(1,2,2);

Particle P1(A,1.0),*P2 ;

P2 = new Particle(B,2.0) ;P1.move_to(B) ;P1.radius = 2.0 ;P2->radius = 3.0 ;

delete P2 ;

Vector3D A(0,0,0), B(1,2,2);

Particle P1(A,1.0),*P2 ;

P2 = new Particle(B,2.0) ;P1.move_to(B) ;P1.radius = 2.0 ;P2->radius = 3.0 ;

delete P2 ;

Page 17: Object Oriented Programming D. Place QUAKES, The University of Queensland

Is a Class an Object ? 1 level system (single hierarchy)

– Classes are objects and objects are classes 2 level system (classical view)

– Class & Object distinction (class are not object)

3 level system– Classes are instance of meta-class– Meta-classes are instance of themselves

5 level system (object, class, class class, meta-class, meta-class class)

Page 18: Object Oriented Programming D. Place QUAKES, The University of Queensland

Is a Class an Object?

In C++, classes are not objects– They are not instance of class

However classes may contain– Properties (Variables/data)– Methods– “static”

Page 19: Object Oriented Programming D. Place QUAKES, The University of Queensland

Class (example)class A {public: Static int i,j,k; int l,b ;

static void classfn() ; void function() ; } ;

A a,b ;A::classfn() ;a.i = 2 ; // same as A::i = 2 ; or b.i = 2b.l = 3 ;b.function() ;

class A {public: Static int i,j,k; int l,b ;

static void classfn() ; void function() ; } ;

A a,b ;A::classfn() ;a.i = 2 ; // same as A::i = 2 ; or b.i = 2b.l = 3 ;b.function() ;

Page 20: Object Oriented Programming D. Place QUAKES, The University of Queensland

Inheritance

Simple Inheritance

r r

r’

QR,G,B +

Q

Multiple InheritanceDynamic

Inheritance

Page 21: Object Oriented Programming D. Place QUAKES, The University of Queensland

Inheritance (example)

class A ;class B ;class C : A {... } ;// class C { // class A a; // ... } ;class D : A, B {... } ;

class A ;class B ;class C : A {... } ;// class C { // class A a; // ... } ;class D : A, B {... } ;

Page 22: Object Oriented Programming D. Place QUAKES, The University of Queensland

Dynamic Inheritance

C++ Limitation– Dynamic Inheritance is done through dynamic binding.

class A ;class B ;class C : A, B { …};

class D { class A *a ; …};

class A ;class B ;class C : A, B { …};

class D { class A *a ; …};

Page 23: Object Oriented Programming D. Place QUAKES, The University of Queensland

Inheritance (example)

class A { int i;} ;class B { int j;} ;class C : A, B { int k ;};class D { class A *a ; int l ;};

class A { int i;} ;class B { int j;} ;class C : A, B { int k ;};class D { class A *a ; int l ;};

C c1; D d1;

c1.i = 2 ;c1.j = 2 ;c1.k = 2 ;

d1.l = 2 ;

d1.a = &c1 ;d1.a->i = 2 ;((C *)(d1.a))->j = 2 ;

C c1; D d1;

c1.i = 2 ;c1.j = 2 ;c1.k = 2 ;

d1.l = 2 ;

d1.a = &c1 ;d1.a->i = 2 ;((C *)(d1.a))->j = 2 ;

Page 24: Object Oriented Programming D. Place QUAKES, The University of Queensland

Object Encapsulation

Protect or hide objects – Hide objects that do not contribute to

the essential characteristics• Public• Protected• Private

Page 25: Object Oriented Programming D. Place QUAKES, The University of Queensland

Encapsulation (example)

class A {private: int i;protected:

int j ;public: int k ;} ;// all members can be

accessed inside Aclass B : public A {public: int function();} ;

class A {private: int i;protected:

int j ;public: int k ;} ;// all members can be

accessed inside Aclass B : public A {public: int function();} ;

//inside the class B…int B::function { k = 1 ; // public ok j = 1 ; // protected ok i = 1 ; // private denied} ;

// outside the class…int main() { B b1; B.k = 1; // public ok B.j = 1; // protected denied} ;

//inside the class B…int B::function { k = 1 ; // public ok j = 1 ; // protected ok i = 1 ; // private denied} ;

// outside the class…int main() { B b1; B.k = 1; // public ok B.j = 1; // protected denied} ;

Page 26: Object Oriented Programming D. Place QUAKES, The University of Queensland

Polymorphism

Having, assuming, or passing through many or various forms

At run time, objects can change of class– Such as a window becoming an icon

The same operation may behave differently for– Different classes, or– Different parameters

Page 27: Object Oriented Programming D. Place QUAKES, The University of Queensland

Overriding & Multi-methods

Overriding (Polymorphism)– Behaviour/methods can

be overloaded (ie. re-defined)

Multi-methods (or multiple-polymorphism)– more than one parameter can be used in

the selection of a method (usually only the type of the object is used to select a method)

– For instance• int operator + (int)• int operator + (double)

r rr’

Volume()Volume()

Page 28: Object Oriented Programming D. Place QUAKES, The University of Queensland

Overriding & Multi-Methods (example)

class A {public: virtual int function(); int function(int i);} ;

class B : public A {public: virtual int function();} ;

class A {public: virtual int function(); int function(int i);} ;

class B : public A {public: virtual int function();} ;

A a1;B b1;

a1.function(); // call function() in Ab1.function(); // call function() in B

b1.function(2); // function(int) in A

A a1;B b1;

a1.function(); // call function() in Ab1.function(); // call function() in B

b1.function(2); // function(int) in A

Page 29: Object Oriented Programming D. Place QUAKES, The University of Queensland

Dynamic Typing

Dynamic Typing is used to allow polymorphism

The type of objects are determined at execution time (not at compilation time)

Limitation in C++, Java (strong typing) Run Time Type Identification (RTTI)

provide a limited dynamic typing in C++

Page 30: Object Oriented Programming D. Place QUAKES, The University of Queensland

Polymorphism (example)

class A {public: virtual int function();} ;

class B : public A {public: virtual int function();} ;

class A {public: virtual int function();} ;

class B : public A {public: virtual int function();} ;

A a1;A *pa; B b1;

a1.function(); // call function() in Ab1.function(); // call function() in Bpa = & a1 ;pa->function(); // call function in A...pa = & b1 ;pa->function(); // call function in B

A a1;A *pa; B b1;

a1.function(); // call function() in Ab1.function(); // call function() in Bpa = & a1 ;pa->function(); // call function in A...pa = & b1 ;pa->function(); // call function in B

Page 31: Object Oriented Programming D. Place QUAKES, The University of Queensland

Polymorphism (C++ limitation)

class A {public: virtual int function();} ;

class B : public A {public: double function(); // not allowed} ;

class A {public: virtual int function();} ;

class B : public A {public: double function(); // not allowed} ;

Page 32: Object Oriented Programming D. Place QUAKES, The University of Queensland

Abstract & Incomplete Class

Used to specify an Interface.– Partially defined behaviour. – Specify how the objects/classes are used– To create an instance all behaviour must be

defined.

r •r•Volume(s)

•w,h•Volume(s)

w

h

Volume()

AnObject

Page 33: Object Oriented Programming D. Place QUAKES, The University of Queensland

Abstract class Pure abstract (base) classes are

only defined by their behaviour.– Allow the refinement of classes without

altering the dependent classes– Instances of the derived class must

comply to the specifications.

rVolume()

AnObject

LatticeAnObject

Page 34: Object Oriented Programming D. Place QUAKES, The University of Queensland

Abstract & incomplete class (example)

class AParticle {public: virtual int calcforce() = 0 ;} ;class ElasticParticle : public AParticle {public: virtual int calcforce() ;} ;

Int ElasticParticle::calcforce() { …} ;...AParticle P // forbidden, AParticle is incomplete

class AParticle {public: virtual int calcforce() = 0 ;} ;class ElasticParticle : public AParticle {public: virtual int calcforce() ;} ;

Int ElasticParticle::calcforce() { …} ;...AParticle P // forbidden, AParticle is incomplete

Page 35: Object Oriented Programming D. Place QUAKES, The University of Queensland

Parameterised polymorphism

Ability to parameterise classes and functions with classes or types– For example, use to specify the

type of the elements of a matrix•Matrix<float>•Matrix<double>

– Minimize duplication of code

Page 36: Object Oriented Programming D. Place QUAKES, The University of Queensland

Parameterised class (example)

template<class T> class Lattice{protected:

Collection<T> allparticle ;public: T & get_object(int n) ;} ;

Class Particle;Class Molecule;

template<class T> class Lattice{protected:

Collection<T> allparticle ;public: T & get_object(int n) ;} ;

Class Particle;Class Molecule;

Lattice<Particle> L;Lattice<Molecule> L2;particle p ;Molecule m ;

p = L.get_object(100) ;M = L2.get_object(10) ;

Lattice<Particle> L;Lattice<Molecule> L2;particle p ;Molecule m ;

p = L.get_object(100) ;M = L2.get_object(10) ;

Page 37: Object Oriented Programming D. Place QUAKES, The University of Queensland

Performance

Page 38: Object Oriented Programming D. Place QUAKES, The University of Queensland

Dynamic binding…

Dynamic binding costs time & memory– Objects contain information about their

classes.– Automatic optimisation (such as

inlining) cannot be performed since the compiler cannot determinate which functions are called.

– Using “virtual” only when needed…

Page 39: Object Oriented Programming D. Place QUAKES, The University of Queensland

Function calls

Function calls cannot be optimised unless the compiler has access to the source code.– Inline functions (similar to macros)

• Uses a simplified function call mechanisms

Not suited for automatic parallelisation – Use of directive

Page 40: Object Oriented Programming D. Place QUAKES, The University of Queensland

An example: LSMearth

Page 41: Object Oriented Programming D. Place QUAKES, The University of Queensland

Particle hierarchy

Interacting Unit Dynamic Objet

Grain

Frictionless Particle

Wall (driving plates) Particle Unit

Pos, Vel, Acc, FrcNeighborhood Table

Pos, Orientation

Spring Constant

Coefficient of Friction

Page 42: Object Oriented Programming D. Place QUAKES, The University of Queensland

The particle class…Class DynObject : virtual public AnObject {protected: Vector Vel, Acc, Frc ;... } ;

Class InteractingUnit {protected:

list<AnObject> neighbors ;public: virtual int nbNeighbours() ; virtual AnObjet * getNeighbour() ;... } ;

Class AParticle : virtual public DynObject, virtual public InteractingUnit {public: void calcforces() = 0 ; void time_integrate(AnIntegrationScheme &TI) = 0 ;... } ;

Class DynObject : virtual public AnObject {protected: Vector Vel, Acc, Frc ;... } ;

Class InteractingUnit {protected:

list<AnObject> neighbors ;public: virtual int nbNeighbours() ; virtual AnObjet * getNeighbour() ;... } ;

Class AParticle : virtual public DynObject, virtual public InteractingUnit {public: void calcforces() = 0 ; void time_integrate(AnIntegrationScheme &TI) = 0 ;... } ;

Page 43: Object Oriented Programming D. Place QUAKES, The University of Queensland

Model & Lattice classes

M odel

A la tticeA tim e in teg ra tionA se t o f pa ram ete rsA d isp la y in te rfa ceA p a rs e r in te r fa ce (sc rip tin g )

M odel w ithout fric tion

Fm odel

A la ttice F la ttice

G ra inC om p lex o b jec t w ith fr ic tion

M odel w ith fric tion

Tem plate la ttice

Lattice

F ric tion le ss P a rtic le sC om p lex o b jec t (w a lls )

Page 44: Object Oriented Programming D. Place QUAKES, The University of Queensland

Concluding remarks

Object Oriented Programming– Emphasis on modelling the real-

world – Enable incremental, iterative,

evolutionary and concurrent development