POODR Ch 3


There are some concepts and information checks I put together for POODR book club with CodeNewbies. Feel free to try out the concept checks.

1.) First concept from POODR Ch 3

Dependency Injection

class Customer
  attr_reader :money, :bread, :meat, :mayonnaise
  def initialize(money, bread, meat, mayonnaise)
    @money      = money
    @bread      = bread
    @meat       = meat
    @mayonnaise = mayonnaise
  end

  def lunch
    Chef.new(bread, meat, mayonnaise).prepare
  end
end

class Chef
  attr_reader :bread, :meat, :mayonnaise
  def initialize(bread, meat, mayonnaise)
    @bread      = bread
    @meat       = meat
    @mayonnaise = mayonnaise
  end

  def prepare
    bread + meat + mayonnaise
  end
end
in `Customer`, what are some of the things `lunch` method "knows" about `Chef`?

+ the name of the class `Chef`

+ the `prepare` method name

+ how many arguments `prepare` needs

+ the order of arguments into `prepare`

Try decoupling Customer and Chef above

How would decouple the Customer class from the Chef class?

My Answer

class Customer
  attr_reader :money, :lunch
  def initialize(money, lunch)
    @money = money
    @lunch = lunch
  end
end

    
class Chef
  attr_reader :bread, :meat, :mayonnaise
  def initialize(bread, meat, mayonnaise)
    @bread      = bread
    @meat       = meat
    @mayonnaise = mayonnaise
  end

    
  def prepare
    bread + meat + mayonnaise
  end
end

    
joe = Customer.new(5, Chef.new(bread, meat, mayonnaise).prepare)

Chef is untouched and lunch is *pulled out of* `Gear`

Note: this is my answer to the questions presented, if you disagree or have other considerations to bring to light, comment below.