Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. My liberal-arts (think about programming lanugage like natural language) approach to software engineering and thinking about object orientation
  2.  
  3. - use clear language to reduce cognitive overhead
  4. - blow off details until you need them
  5. - call things what they are so that what they are is what you call them
  6. - Think of parts of programming languages as a spoken language (Nouns, verbs, etc...)
  7. - Solve the easiest problems first to build emotional wellbeing and momentum
  8.  
  9. - methods are verbs and verb phrases
  10. - classes/objects are our proper nouns like User, Transaction, Accounts, BlogPost, etc...
  11. - properties on our objects are lil' nouns or adjectives of our proper nouns
  12. - interfaces allow us adverbs/adjectives
  13.  
  14. ---
  15. Ok, that sounds nice, but show me the code!
  16.  
  17. If we have inheritance, then we do the following:
  18. - Specify the data type of a variable by the base/parent class
  19.  
  20. `Person a = new Person("John Wayne");`
  21.  
  22. `Person b = new User("Captain", "Janeway", true);`
  23.  
  24. - we can also use our interface name to specify a variable data-type b/c person class implements the methods from the interface
  25.  
  26. `Greeter c = new Person("Billy Bob");`
  27. `Greeter d = new User("Ada", "Lovelace", true);`
  28.  
  29. Polymorphism == many forms
  30.  
  31. - Suppose we have a class called Guest.
  32. - Guest does not inherit from person or user
  33. - Guest does, however, implement the Greeter interface
  34.  
  35. `Greeter e = new Guest(); // compiler is good w/ this if the Guest class implements a sayHello()`
  36.  
  37. `Person f = new Guest(); // will not pass the compiler b/c guest never inherited from person`
  38.  
  39. parent classes allow us to specify a datatype
  40. interface allow us to specify a datatype
  41.  
  42. - java is single inheritance which means only one parent class
  43. - but interfaces allow more flexibility
  44.  
  45. ----
  46.  
  47. Consider the following:
  48.  
  49. Make an interface called Feedable with a feed method on it
  50. Make another interface called Petable with a pet method on it
  51.  
  52. Make a parent class called Pet
  53. Make a Cat class that implments Feedable and Petable. Cat inherits from Pet.
  54. Make a PetRock class that implements Petable only. PetRock inherits from Pet.
  55. Make a Porcupine class implements Feedable. Porcupine inherits from Pet
  56.  
  57. ```java
  58. Pet cat = new Cat();
  59. Pet rock = new PetRock();
  60. Pet porcupine = new Porcupine();
  61. ```
  62.  
  63. ```
  64. // by ensuring that cat implements the petable interface, we're forcing whoever builds the Cat class to built out a pet method.
  65. cat.pet()
  66. cat.feed()
  67.  
  68. Petable cat = new Cat(); // passes the compiler
  69. Petable porcupine = new Porcupine() // does not compile
  70. Feedable porcupine2 = new Porcupine() // compiles a-ok
  71. Feedbable rock = new PetRock() // does not compile
  72. ```
  73.  
  74. all of this polymorism stuff is about balancing strictness of data types with flexibilty.
  75.  
  76. interfaces are an IOU to yourself/team to figure out how to do things later.
  77.  
  78. ```
  79. List a = new ArrayList();
  80. List b = new LinkList();
  81. ```
  82.  
  83. ----
  84. BIG PICTURE:
  85.  
  86. - variables abstract away values. (instead of hard coding everything, variables are placeholders)
  87. - conditionals abstract away decisions (so the code decides instead of a human)
  88. - loops abstract away repetion (so we tell the loop how many times to run rather than duplicating code)
  89. - functions abstract away process (so we don't duplicate processes)
  90. - classes abstract away particulars (because classes are about groups) just like CSS classes (one or multiple of things that have stuff in common) (stuff -> either behavior or properties)
  91. - objects allow us to create particular indiduals
  92. - interfaces abstract functionality even further (blow off the details (implementation) for later)
  93.  
  94.  
  95. all of this polymorism stuff is about balancing strictness of data types with flexibilty.
  96. all of this polymorism stuff is about balancing strictness of data types with flexibilty.
  97. all of this polymorism stuff is about balancing strictness of data types with flexibilty.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement