440 likes | 459 Views
Learn about component-based design, OOP concepts, performance issues, and LSMearth example software. Explore robust, re-usable, and concurrent development. Transition from component-based to object-oriented. Understand classes, inheritance, polymorphism, and encapsulation.
E N D
Object Oriented Programming D. Place QUAKES, The University of Queensland
Content • Introduction: component based design • Object Oriented Programming Concepts • Performance issues • An example: LSMearth
Software Requirements • Robust • Availability • Maintainable • Re-usable • Concurrent development
Component based architecture • Minimize component dependency • Reduce complexity • Reusability • Concurrent development • “Simple” components • Robust • Hierarchical architecture • Easy to maintain
Basic design • From general to specific • Base components • Simple • Robust • Re-usable
LSM modules • Build on independent modules • Separate hardware dependence modules • Hidden complexity • Model development does not require knowledge of MPI…
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
Programming Language • Simula (1967) • Smalltalk • “Pure” object oriented programming • C++, Java • Limited functionality • Eiffel, ADA(~), Objective C, CLOS…
Object Oriented programming • Object • Class • Inheritance • Polymorphism
Q move_to(Q) Object • An object has • Properties (Variables / Data ) • Behaviour (Methods / Operators / Functions…) r P
Object & Class • An object is an instance of a class • For example: • Real a -> a is an object, Real is a class • Sphere s
Class • Specification of structure (instance variables), behaviour (methods), and inheritance (hierarchy). • Type of object • Classify objects by their properties and/or behaviour
Classes (example) Class Particle { Vector3D Position; double radius; } ; Particle(Vector3D &P,real r) ; ~Particle() ; Particle & move_to(Vector3D &Q) ;
Instances of Class Vector3D A(0,0,0), B(1,2,2); Particle P1(A,1.0),*P2 ; P2 = newParticle(B,2.0) ; P1.move_to(B) ; P1.radius = 2.0 ; P2->radius = 3.0 ; delete P2 ;
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)
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”
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 = 2 b.l = 3 ; b.function() ;
Multiple Inheritance R,G,B + Q r r r’ Q Inheritance • Simple Inheritance • Dynamic Inheritance
Inheritance (example) class A ; class B ; class C : A { ... } ; // class C { // class A a; // ... } ; class D : A, B { ... } ;
Dynamic Inheritance class A ; class B ; class C : A, B { … }; class D { class A *a ; … }; • C++ Limitation • Dynamic Inheritance is done through dynamic binding.
Inheritance (example) 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 ; class A { int i; } ; class B { int j; } ; class C : A, B { int k ; }; class D { class A *a ; int l ; };
Object Encapsulation • Protect or hide objects • Hide objects that do not contribute to the essential characteristics • Public • Protected • Private
Encapsulation (example) //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 } ; class A { private: int i; protected: int j ; public: int k ; } ;// all members can be accessed inside A class B : public A { public: int function(); } ;
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
r r Volume() Volume() r’ 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)
Overriding & Multi-Methods (example) A a1; B b1; a1.function(); // call function() in A b1.function(); // call function() in B b1.function(2);// function(int) in A class A { public: virtual int function(); int function(int i); } ; class B : public A { public: virtual int function(); } ;
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++
Polymorphism (example) A a1; A *pa; B b1; a1.function(); // call function() in A b1.function(); // call function() in B pa = & a1 ; pa->function();// call function in A ... pa = & b1 ; pa->function();// call function in B class A { public: virtual int function(); } ; class B : public A { public: virtual int function(); } ;
Polymorphism (C++ limitation) class A { public: virtual int function(); } ; class B : public A { public: double function(); // not allowed } ;
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) AnObject Volume() • w,h • Volume(s) h w
Lattice AnObject AnObject Volume() r 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.
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
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
Parameterised class (example) Lattice<Particle> L; Lattice<Molecule> L2; particle p ; Molecule m ; p = L.get_object(100) ; M = L2.get_object(10) ; template<class T> class Lattice { protected: Collection<T> allparticle ; public: T & get_object(int n) ; } ; Class Particle; Class Molecule;
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…
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
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 ; ... } ;
Concluding remarks • Object Oriented Programming • Emphasis on modelling the real-world • Enable incremental, iterative, evolutionary and concurrent development