multiple dispatch(oops concepts)

15

Upload: sumitra22

Post on 25-May-2015

157 views

Category:

Education


0 download

DESCRIPTION

the ppt describes a slight idea about the concept and working area of mulitple dispatch

TRANSCRIPT

Page 1: multiple dispatch(OOPs concepts)
Page 2: multiple dispatch(OOPs concepts)

• Introduction• Understanding• Datatype and practical use• Dynamic dispatch study• Multiple dispatch examples• Pythons• Languages supporting dispatches• Multimethods in other languages via extentions

Page 3: multiple dispatch(OOPs concepts)

• Multiple dispatch or multimethods or function overloading is the feature of object-oriented programming languages

• which a function or method can be dynamically dispatched based on the run time (dynamic) type of more than one of its arguments.

• It is an extension of single dispatch polymorphism• In which method call is dynamically dispatched based on the actual

derived type of the object on which the method has been called.

• Multiple dispatch generalizes the dynamic dispatching to work with a

combination of two or more objects.

Page 4: multiple dispatch(OOPs concepts)

When Several functions having same name in such cases, the name at the function call site is not sufficient for identifying the code to be executed .Here the dispatchment arrises.

• single-dispatch OOP languages when invoking a method (“function calling”) one of its arguments is treated specially and used to determine which of the methods of that name is to be called.

FOR EG:-special .method (other, arguments, here);

Page 5: multiple dispatch(OOPs concepts)

• By contrast, in languages with multiple dispatch, the selected method is the one whose arguments match the number and function call type.

• There is no "special" argument that "owns" the function/method carried out in a particular call.

• Such as Common Lisp Object System (CLOS) is latest example of multiple dispatch.

Page 6: multiple dispatch(OOPs concepts)

• programming languages that defer data type identification until run-time,

• the selection among alternative functions must occur at run-time, based on the dynamically determined types of function arguments.

• Functions whose alternative implementations are selected in this manner are referred to most generally as multimethods.

• there is some run-time cost associated with dynamically dispatching function call.

Page 7: multiple dispatch(OOPs concepts)

• the distinction between overloading and multimethods can be blurred,

• the compiler determining whether compile-time selection can be applied to a given function call, or whether slower run-time dispatch is needed.

Page 8: multiple dispatch(OOPs concepts)

• dynamic dispatch in mostly compilers languages was studied and the results stated that

• 13%-32% of generic functions utilize the dynamic type of a single argument,

• while 2.7%-6.5% of them utilize the dynamic type of multiple arguments.

• remaining 65%-93% of generic functions have a single concrete method (overrider), and therefore are not considered to use the dynamic types of their arguments.

• In addition, the study reports that 2%-20% of generic functions had two and 3%-6% had three concrete function implementations.

Page 9: multiple dispatch(OOPs concepts)

• In the presence of multiple dispatch, the traditional idea of methods as being defined in classes and contained in objects becomes less appealing—

• each collide-with method there is attached to two different classes, not one.

• Hence, the special syntax for method invocation generally disappears, so that method invocation looks exactly like ordinary function invocation

• methods are grouped not in classes but in generic functions. • AS IN multiple dispatch, such as Common Lisp, it

Page 10: multiple dispatch(OOPs concepts)

• In languages that do not support multiple dispatch at the language definition or syntactic level, it is often possible to add multiple dispatch using a library extension.

• For example, the module multimethods.py provides CLOS-style multimethods for Python

without changing the underlying syntax or keywords of the language. • Multiple dispatch or multimethods or function overloading is the feature of some

object-oriented langages in which a function or method can be dynamically dispatched based on the run time (dynamic) type of more than one of its arguments.

• This is an extension of single dispatch polymorphism where a method call is dynamically dispatched based on the actual derived type of the object on which the method has been called. Multiple dispatch generalizes the dynamic dispatching to work with a combination of two or more objects.

Page 11: multiple dispatch(OOPs concepts)

EXAMPLE OF MULTIPLE DISPATCH• C does not have dynamic dispatch, Often an enum is used to identify the

subtype of an object. Dynamic dispatch can be done by looking up this value in a function pointer branch table.

• EX:-typedef enum { asteroid = 0, spaceship } Thing;

• C++ is being considered,[3] currently C++ only supports single dispatch natively. The methods of working around this limitation are analogous; either use the visitor pattern, double dispatch, dynamic cast:

• it can be approximately implemented using double dispatch or a type based lookup table as outlined in the C/C++

• JAVA supports only single dispatch and code like visitor pattern helps to solve this problem of its implementatiopn

Page 14: multiple dispatch(OOPs concepts)
Page 15: multiple dispatch(OOPs concepts)