Programming Style - Duck Type
Click here to load reader
-
Upload
sentinel-solutions-ltd -
Category
Technology
-
view
2.448 -
download
2
Transcript of Programming Style - Duck Type
WHAT IS -
duck typing is a style of dynamic typing in which an object's current set of methods
and properties determines the valid semantics,
rather than its inheritance from - a particular class or implementation of a
specific interface.PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G
Presented by Abdun Nur Tomal
DEFINE -
may be phrased as follows:"When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.“
In duck typing, one is concerned with just those aspects of an object that are • used, • rather than with the type of the object itself.
PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G
FOR EXAMPLE :- in a non-duck-typed language, one can create a function that takes an object of type Duck and calls that object's -walk and -quack methods.
In a duck-typed language, > the equivalent function would take an
object of any type and > call that object's walk and quack
methods. > If the object does not have the methods that
are called then the function signals a run-time error.
> It is this action of any object having the correct walk and quack methods being accepted by the function that evokes the quotation and hence the name of this form of typing.
PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G
EXAMPLE :- Duck typing is aided by habitually not testing for the type of arguments in method and function bodies,
relying on documentation, clear code, and testing to ensure correct use.
pseudo-code for a duck typed language:
function calculate(a, b, c) => return (a+b)*c
example1 = calculate (1, 2, 3)
example2 = calculate ([1, 2, 3], [4, 5, 6], 2)
example3 = calculate ('apples ', 'and oranges, ', 3)
print to_string example1
print to_string example2
print to_string example3
the result of the code would be:9
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
apples and oranges, apples and oranges, apples and oranges
PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G
Thus, duck typing allows polymorphism without inheritance.
The only requirement that function calculate needs in its variables is having the "+" and the "*" methods.
The duck test can be seen in the following example:As far as the function in_the_forest is concerned, the Person object is a
duck:
class Duck: def quack(self):print("Quaaaaaack!") def feathers(self): print("The duck has white and gray feathers.")
class Person: def quack(self): print("The person imitates a duck.") def feathers(self): print("The person takes a feather from the ground and shows it.") def name(self): print("John Smith") def in_the_forest(duck): duck.quack() duck.feathers()
def game(): donald = Duck() john = Person() in_the_forest(donald) in_the_forest(john)
game()
PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G
This way software can be developed by extending partially working duck typed code.
In JavaScript
var Duck = function () {};
Duck.prototype = {
quack: function () { alert("Quaaaaaack!"); },
feathers: function () { alert("The duck has white and gray feathers."); }
};
var Person = function () {};
Person.prototype = {
quack: function () { alert("The person imitates a duck."); },
feathers: function () { alert("The person takes a feather from the ground and shows it.");},
name: function () { alert("John Smith"); }
};
var in_the_forest = function (duck)
{
duck.quack();
duck.feathers();
};
var game = function ()
{
var donald = new Duck();
var john = new Person();
in_the_forest(donald)
in_the_forest(john)
};
game();
PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G
Resources –Wikipediahttp://en.wikipedia.org/wiki/Duck_typing#Concept_examples
Dr. Dobb’s
http://drdobbs.com/cpp/184401971
http://drdobbs.com/open-source
Big Dingus
http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/
Apache Commons
http://commons.apache.org/proxy/
PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G
Resources –Wikipediahttp://en.wikipedia.org/wiki/Duck_typing#Concept_examples
Dr. Dobb’s
http://drdobbs.com/cpp/184401971
http://drdobbs.com/open-source
Big Dingus
http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/
Apache Commons
http://commons.apache.org/proxy/
PR
OG
RA
MM
ING
S
TY
LE
DU
CK
TY
PIN
G