Ruby and OOP (Tue Sep 12, lecture 6)

Homework due for today

Legend: : Participation | : Early | : PDF | : Portfolio | : Zipped

  1. RTHW: Complete Learn Ruby The Hard Way up and including Lesson 44. You are welcome to go further if you want and found it fun. Or come back to it in the future when time allows.
  2. Movies2: Continue working on (PA) Movies Part 2. It’s a continuation of movies_1. It will be due soon! As usual, you please do your coding in your Portfolio. Start a new top level directory for this called “movies-2”.

Discussion about homework

Intro to Object Oriented Design

What is software design?
  • In software, “arranging code in a particular way”
  • YAGNI principle vs. BUFD
  • Finding the balance: effort and care proportional to cost over time!
Patterns
Classes
  • Classes: How do you decide what a class is and what it is not?
    • What is the domain or the problem space or the problem domain?
    • Look for Nouns!
    • Remember SRP: Single Responsibility Principle (see Single Responsibility Principle)
    • Ask yourself: “What does this class represent/do?”
Objects
  • Classes and objects are different!
  • Instance variables
  • Constructor (no destructor in Ruby!)
  • Methods: private/public
Polymorphism
  • When the same interface can be used with objects of different classes
    • Example: In ruby, message #to_s can be sent to objects of different classes
    • Why is it useful?
  • Ways to achieve this:
    • Inheritance
    • “Duck” Typing
  • “Duck” typing.
    • Don’t rely on the class/type of a thing to know what it can do
    • Reverse it: based on what it can do, you can tell the type of a thing.
    • How is duck typing is an alternative/complement to inheritence?
    • And what about Java interfaces?
    • Consider a method like #talk in a gaming application
    • Here’s The classic Duck Typing Example
Inheritance
  • inheritence: rarely is this the best solution
  • composition and delegation: a better solution

Ruby Live Coding Demonstration and discussion

Next Class