Objects, Objects Everywhere    Mike Pack     @zombidev
Let’s Talk Objects  Behavior     +   State
Let’s Talk Objects         BehaviorI’m a person, what can I do?            Walk            Talk            Eat Could also ...
Let’s Talk Objects                 BehaviorLevel of abstraction matters!  Behavioral abstraction varies per domain.
Let’s Talk Objects              StateI’m a person, who am I currently?            Mike Pack              Male             ...
Let’s Talk Objects              State     States changeWhen I’m 70, my attributes might be:            Mike Pack          ...
Let’s Talk Objects Behavior changes too!I just learned to jump, now my behavior is:                  Walk                 ...
Let’s Talk Objects  Behavior     +   State
HTML<div id=”name”>  Mike</div>  Is this an object?
Attribute       HTML         Name<div id=”name”>  Mike</div>  Is this an object?
Attribute       HTML         Name<div id=”name”>  Mike           State</div>  Is this an object?
Attribute        HTML         Name<div id=”name”>  Mike the beef?Where’s    State</div>   Is this an object?
Attribute         HTML         Name  <div id=”name”>    Mike behavior?Where’s the State  </div>    Is this an object?
HTML<div id=”name”   onclick=”...”>  Mike</div>  Is this an object?
Attribute       HTML         Name<div id=”name”   onclick=”...”>  Mike      Behavior!</div> State  Is this an object?
HTML               HTML is object oriented                                (albeit, not for purist)HTML does not exhibit ot...
HTML         Don’t use onclick=”...”However, without onclick, HTML does not exhibit behavior.
CSSa{  color: red;} Is this an object?
CSS        Attributea{       Name  color: red;} Is this an object?
CSS        Attributea{       Name  color: red;}             State Is this an object?
CSS          Attribute a{        Name   color:the beef?Where’s   red; }             State   Is this an object?
CSSa{  color: red;  &:hover {    color: blue;  }}Is this an object?
Attribute     CSS Name a {          color: red; Behavior!          &:hover {            color: blue;          }           ...
CSS       a{Behavior color: red;     State         &:hover {      Change           color: blue;         }       }   Behavi...
CSS                  CSS is object oriented                                      (not for purist)CSS does not exhibit othe...
JavaScriptvar book = { title: “The Art of         War”};   Is this an object?
Attribute Name            JavaScript    var book = {     title: “The Art of             War”    };        Is this an object?
Attribute Name            JavaScript    var book = {     title: “The Art of             War” State    };        Is this an...
Attribute Name            JavaScript   var book = {    title: “The Art of   Where’s the beef?            War” State   };  ...
Attribute Name            JavaScript   var book = {    title: “The Art of   Where’s the beef?          It’sWar”           ...
JavaScriptvar book = { title: “The Art of War”, constructor: function() {...}, hasOwnProperty: function() {...}, ...};    ...
JavaScriptJavaScript is object oriented           (of course)
JavaScriptIt’s not entirely object oriented    JavaScript has primitives, too.
JavaScriptIf it’s an object, typeof knows   typeof {} === ‘object’ //=> true
JavaScriptIntegers are not objectstypeof 1 === ‘object’ //=> false
JavaScript  undefined is not an objecttypeof undefined === ‘object’ //=> false
JavaScriptLiterals are not always primitives     typeof [] === ‘object’ //=> true
JavaScript  typeof might not return object     typeof true === ‘object’ //=> false    typeof true === ‘boolean’ //=> true ...
JavaScriptCareful, typeof can bitetypeof null === ‘object’ //=> true     new Null() //=> error
Ruby       1Is this an object?
Ruby        State              (accessor)     11.real #=> 1Is this an object?
Ruby        State              (accessor)     11.real #=> 11 + 1 #=> 2Is this an object?
Ruby        State              1     (accessor)       1.real #=> 1Behavior 1 + 1 #=> 2        1.+(1) #=> 2      Is this an...
RubyRuby is object oriented
RubyEverything is an object
RubyLiteral arrays are new objects  [].object_id #=> 70318249770140  [].object_id #=> 70318249594160
RubyLiteral strings are new objects‘slurpy’.object_id #=> 70318249561400‘slurpy’.object_id #=> 70318249500340
RubyLiteral hashes are new objects   {}.object_id #=> 7019206373870  {}.object_id #=> 70192063701240
RubyLiteral regexs are new objects  //.object_id #=> 70192063385520  //.object_id #=> 70192067965040
RubyLiteral ranges are new objects (1..2).object_id #=> 70192067946460 (1..2).object_id #=> 70192067921120
Ruby Not everything creates a new            objectRuby has singleton objects (objects instantiated once).
Rubynil is a singleton nil.object_id #=> 4 nil.object_id #=> 4
Rubybooleans are singletons   false.object_id #=> 0   false.object_id #=> 0    true.object_id #=> 2    true.object_id #=> 2
RubyNumbers are singletons    1.object_id #=> 3    1.object_id #=> 3
Ruby     um s  xnFi Numbers are singletons        1.object_id #=> 3        1.object_id #=> 3
RubyBignums are not singletons4611686018427387904.object_id #=>        701920637307404611686018427387904.object_id #=>    ...
Ruby  Bignums are not singletons 4611686018427387904.object_id #=>         70192063730740 4611686018427387904.object_id #=...
bytes to store               Ruby       integerbytes = 0.size #=> 8
bytes to store                Ruby          integerbytes = 0.size #=> 8                          8 bits per bytebits = byt...
bytes to store                Ruby          integerbytes = 0.size #=> 8                          8 bits per bytebits = byt...
Ruby 4611686018427387904         Bignum     Different objects.4611686018427387904 - 1          Fixnum      Singleton object.
RubyWhy does nil have object_id of 4?     1 have object_id of 3?    true have object_id of 2?
RubyBecause Matz says so  false.object_id #=> 0      0.object_id #=> 1   true.object_id #=> 2      1.object_id #=> 3     n...
Ruby   Find by object_idObjectSpace._id2ref(0) #=> falseObjectSpace._id2ref(1) #=> 0ObjectSpace._id2ref(2) #=> trueObjectS...
RubyNegative object_ids  -1.object_id #=> -1  -2.object_id #=> -3  -3.object_id #=> -5
Objects.useful? # => true
Objects.useful? # => trueConsistent modeling across the stack.
Objects.useful? # => trueHelpful in representing the real world.
Objects.useful? # => true     Fun and expressive.
Thanks!@zombidev
Upcoming SlideShare
Loading in...5
×

Objects, Objects Everywhere

534

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
534
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Objects, Objects Everywhere

  1. 1. Objects, Objects Everywhere Mike Pack @zombidev
  2. 2. Let’s Talk Objects Behavior + State
  3. 3. Let’s Talk Objects BehaviorI’m a person, what can I do? Walk Talk Eat Could also be defined as: Move Legs Move Lips Ingest Food
  4. 4. Let’s Talk Objects BehaviorLevel of abstraction matters! Behavioral abstraction varies per domain.
  5. 5. Let’s Talk Objects StateI’m a person, who am I currently? Mike Pack Male Brown Therefore, I have attributes: Name Gender Hair Color
  6. 6. Let’s Talk Objects State States changeWhen I’m 70, my attributes might be: Mike Pack Male Grey
  7. 7. Let’s Talk Objects Behavior changes too!I just learned to jump, now my behavior is: Walk Talk Eat Jump
  8. 8. Let’s Talk Objects Behavior + State
  9. 9. HTML<div id=”name”> Mike</div> Is this an object?
  10. 10. Attribute HTML Name<div id=”name”> Mike</div> Is this an object?
  11. 11. Attribute HTML Name<div id=”name”> Mike State</div> Is this an object?
  12. 12. Attribute HTML Name<div id=”name”> Mike the beef?Where’s State</div> Is this an object?
  13. 13. Attribute HTML Name <div id=”name”> Mike behavior?Where’s the State </div> Is this an object?
  14. 14. HTML<div id=”name” onclick=”...”> Mike</div> Is this an object?
  15. 15. Attribute HTML Name<div id=”name” onclick=”...”> Mike Behavior!</div> State Is this an object?
  16. 16. HTML HTML is object oriented (albeit, not for purist)HTML does not exhibit other properties of an OO systems:- Inheritance- Polymorphism- etc
  17. 17. HTML Don’t use onclick=”...”However, without onclick, HTML does not exhibit behavior.
  18. 18. CSSa{ color: red;} Is this an object?
  19. 19. CSS Attributea{ Name color: red;} Is this an object?
  20. 20. CSS Attributea{ Name color: red;} State Is this an object?
  21. 21. CSS Attribute a{ Name color:the beef?Where’s red; } State Is this an object?
  22. 22. CSSa{ color: red; &:hover { color: blue; }}Is this an object?
  23. 23. Attribute CSS Name a { color: red; Behavior! &:hover { color: blue; } State } Is this an object?
  24. 24. CSS a{Behavior color: red; State &:hover { Change color: blue; } } Behavior changes state.
  25. 25. CSS CSS is object oriented (not for purist)CSS does not exhibit other properties of an OO systems:- Inheritance- Polymorphism- etc
  26. 26. JavaScriptvar book = { title: “The Art of War”}; Is this an object?
  27. 27. Attribute Name JavaScript var book = { title: “The Art of War” }; Is this an object?
  28. 28. Attribute Name JavaScript var book = { title: “The Art of War” State }; Is this an object?
  29. 29. Attribute Name JavaScript var book = { title: “The Art of Where’s the beef? War” State }; Is this an object?
  30. 30. Attribute Name JavaScript var book = { title: “The Art of Where’s the beef? It’sWar” implicit! State }; Is this an object?
  31. 31. JavaScriptvar book = { title: “The Art of War”, constructor: function() {...}, hasOwnProperty: function() {...}, ...}; Implicit behavior.
  32. 32. JavaScriptJavaScript is object oriented (of course)
  33. 33. JavaScriptIt’s not entirely object oriented JavaScript has primitives, too.
  34. 34. JavaScriptIf it’s an object, typeof knows typeof {} === ‘object’ //=> true
  35. 35. JavaScriptIntegers are not objectstypeof 1 === ‘object’ //=> false
  36. 36. JavaScript undefined is not an objecttypeof undefined === ‘object’ //=> false
  37. 37. JavaScriptLiterals are not always primitives typeof [] === ‘object’ //=> true
  38. 38. JavaScript typeof might not return object typeof true === ‘object’ //=> false typeof true === ‘boolean’ //=> true But booleans are objects!new Boolean(1).valueOf() === true //=> true
  39. 39. JavaScriptCareful, typeof can bitetypeof null === ‘object’ //=> true new Null() //=> error
  40. 40. Ruby 1Is this an object?
  41. 41. Ruby State (accessor) 11.real #=> 1Is this an object?
  42. 42. Ruby State (accessor) 11.real #=> 11 + 1 #=> 2Is this an object?
  43. 43. Ruby State 1 (accessor) 1.real #=> 1Behavior 1 + 1 #=> 2 1.+(1) #=> 2 Is this an object?
  44. 44. RubyRuby is object oriented
  45. 45. RubyEverything is an object
  46. 46. RubyLiteral arrays are new objects [].object_id #=> 70318249770140 [].object_id #=> 70318249594160
  47. 47. RubyLiteral strings are new objects‘slurpy’.object_id #=> 70318249561400‘slurpy’.object_id #=> 70318249500340
  48. 48. RubyLiteral hashes are new objects {}.object_id #=> 7019206373870 {}.object_id #=> 70192063701240
  49. 49. RubyLiteral regexs are new objects //.object_id #=> 70192063385520 //.object_id #=> 70192067965040
  50. 50. RubyLiteral ranges are new objects (1..2).object_id #=> 70192067946460 (1..2).object_id #=> 70192067921120
  51. 51. Ruby Not everything creates a new objectRuby has singleton objects (objects instantiated once).
  52. 52. Rubynil is a singleton nil.object_id #=> 4 nil.object_id #=> 4
  53. 53. Rubybooleans are singletons false.object_id #=> 0 false.object_id #=> 0 true.object_id #=> 2 true.object_id #=> 2
  54. 54. RubyNumbers are singletons 1.object_id #=> 3 1.object_id #=> 3
  55. 55. Ruby um s xnFi Numbers are singletons 1.object_id #=> 3 1.object_id #=> 3
  56. 56. RubyBignums are not singletons4611686018427387904.object_id #=> 701920637307404611686018427387904.object_id #=> 70192063508580
  57. 57. Ruby Bignums are not singletons 4611686018427387904.object_id #=> 70192063730740 4611686018427387904.object_id #=> 70192063508580Why 4611686018427387904?
  58. 58. bytes to store Ruby integerbytes = 0.size #=> 8
  59. 59. bytes to store Ruby integerbytes = 0.size #=> 8 8 bits per bytebits = bytes * 8 #=> 64 64 bit machine
  60. 60. bytes to store Ruby integerbytes = 0.size #=> 8 8 bits per bytebits = bytes * 8 #=> 64 64 bit machine 1 bit for sign + 1 bit for Ruby2 ** (bits - 2) #=>4611686018427387904
  61. 61. Ruby 4611686018427387904 Bignum Different objects.4611686018427387904 - 1 Fixnum Singleton object.
  62. 62. RubyWhy does nil have object_id of 4? 1 have object_id of 3? true have object_id of 2?
  63. 63. RubyBecause Matz says so false.object_id #=> 0 0.object_id #=> 1 true.object_id #=> 2 1.object_id #=> 3 nil.object_id #=> 4 2.object_id #=> 5
  64. 64. Ruby Find by object_idObjectSpace._id2ref(0) #=> falseObjectSpace._id2ref(1) #=> 0ObjectSpace._id2ref(2) #=> trueObjectSpace._id2ref(3) #=> 1ObjectSpace._id2ref(4) #=> nilObjectSpace._id2ref(5) #=> 2ObjectSpace._id2ref(6) #=> errorObjectSpace._id2ref(7) #=> 3ObjectSpace._id2ref(8) #=> error
  65. 65. RubyNegative object_ids -1.object_id #=> -1 -2.object_id #=> -3 -3.object_id #=> -5
  66. 66. Objects.useful? # => true
  67. 67. Objects.useful? # => trueConsistent modeling across the stack.
  68. 68. Objects.useful? # => trueHelpful in representing the real world.
  69. 69. Objects.useful? # => true Fun and expressive.
  70. 70. Thanks!@zombidev
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×