Programming Style - Duck Type


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Programming Style - Duck Type

  1. 1. What is -<br />duck typing is a style of dynamic typing in which <br /><ul><li>an object's current set of methods </li></ul>and <br /><ul><li>propertiesdetermines the valid semantics, </li></ul>rather than its inheritance from - <br /><ul><li>a particular class or implementation of a </li></ul>specific interface.<br />DUCK TYPING<br />PROGRAMMING STYLE<br />Presented by<br />Abdun Nur Tomal<br />
  2. 2. Define -<br />may be phrased as follows:<br />"When I see a bird that walks like a duckand swims like a duck and quacks like a duck, I call that bird a duck.“<br />In duck typing, one is concerned with just those aspects of an object that are <br /><ul><li>used,
  3. 3. rather than with the type of the object itself.</li></ul> <br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  4. 4. For example :-<br /><ul><li>in a non-duck-typed language,</li></ul>one can create a function that takes an object of type Duck and calls that object's -walk and -quack methods. <br /><ul><li>In a duck-typed language, </li></ul>>the equivalent function would take an object of any type and <br />>call that object's walk and quack methods. <br />>If the object does not have the methods that are called then the function signals a run-time error. <br />>It is this action of any object having the correct walk and quack methods being accepted by the function that evokes<br />the quotation and hence the name of this form of typing.<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  5. 5. example :-<br /><ul><li>Duck typing is aided by habitually not testing for the type of arguments in method and function bodies,
  6. 6. relying on documentation, clear code, and testing to ensure correct use.
  7. 7. pseudo-code for a duck typed language:</li></ul>function calculate(a, b, c) => return (a+b)*c <br />example1 = calculate (1, 2, 3)<br />example2 = calculate ([1, 2, 3], [4, 5, 6], 2) <br />example3 = calculate ('apples ', 'and oranges, ', 3) <br />printto_string example1 <br />printto_string example2 <br />printto_string example3<br /><ul><li>the result of the code would be:</li></ul>9<br />[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] <br />apples and oranges, apples and oranges, apples and oranges<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  8. 8. <ul><li>Thus, duck typing allows polymorphismwithout inheritance.
  9. 9. The only requirement thatfunction calculate needs in its variables is having the "+" and the "*" methods.
  10. 10. The duck test can be seen in the following example:</li></ul>As far as the function in_the_forest is concerned, the Person object is a duck:<br />class Duck: <br />def quack(self):<br />print("Quaaaaaack!") <br />def feathers(self): <br />print("The duck has white and gray feathers.") <br />class Person: <br />def quack(self): <br />print("The person imitates a duck.") <br />def feathers(self): <br />print("The person takes a feather from the ground and shows it.") <br />def name(self): <br />print("John Smith") <br />defin_the_forest(duck): <br />duck.quack() <br />duck.feathers() <br />def game(): <br />donald = Duck() <br />john = Person() <br />in_the_forest(donald) <br />in_the_forest(john) <br />game()<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  11. 11. This way software can be developed by extending partially working duck typed code.<br /><ul><li>In JavaScript</li></ul>var Duck = function () {}; <br />Duck.prototype = { <br />quack: function (){ alert("Quaaaaaack!"); }, <br />feathers: function () { alert("The duck has white and gray feathers."); } <br />}; <br />var Person = function () {}; <br />Person.prototype = { <br />quack: function () { alert("The person imitates a duck."); }, <br />feathers: function () { alert("The person takes a feather from the ground and shows it.");}, <br />name: function () { alert("John Smith"); } <br />}; <br />varin_the_forest = function (duck)<br />{ <br />duck.quack(); <br />duck.feathers(); <br />}; <br />var game = function () <br />{ <br />vardonald = new Duck(); <br />var john = new Person(); <br />in_the_forest(donald) <br />in_the_forest(john) <br />};<br />game();<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  12. 12. Resources –<br />Wikipedia<br /><br />Dr. Dobb’s<br /><br /><br />Big Dingus<br /><br />Apache Commons<br /><br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  13. 13. Resources –<br />Wikipedia<br /><br />Dr. Dobb’s<br /><br /><br />Big Dingus<br /><br />Apache Commons<br /><br />DUCK TYPING<br />PROGRAMMING STYLE<br />