Skip to content

Instantly share code, notes, and snippets.

@bernEsp
Created June 25, 2013 08:43
Show Gist options
  • Save bernEsp/5856956 to your computer and use it in GitHub Desktop.
Save bernEsp/5856956 to your computer and use it in GitHub Desktop.
Separate out things changed from those that stay same. Use template pattern. created and abstract class with hooks methods and with methods to be override *required methods. Delegate, delegate, delegate. Use strategy pattern. Delete functionality and refactor using the power of Ruby Proc to dinamically pass code to objects.
class Animal #abstract class template pattern base on separete out the things changed from those that stay same
def initialize(&kind)
@kind = kind
end
#template patter
def live
eat
make_poop
have_sex
end
def eat
raise "Called to eat"
end
def fly #hook
end
#strategy means pass context
def live
#this pass to @kind of class and call live method
@kind.live(self) #pass the object if you define bunch of variables at initializes or attributes you pass it
#proc
@kind.call(self)
end
end
#template pattern abstract class without procs
class Dog < Animal
def eat
puts "eating"
end
def make_poop
puts "poopin'"
end
def have_sex
puts "grooming"
end
end
class Duck < Animal
def eat
puts "fishing"
end
def make_poop
puts "swimming"
end
def have_sex
puts "cuak"
end
end
#strategy
class Kind
def live
raise "Called live method"
end
end
class Dog < kind
def live(context)
puts "#{context.eat}"
puts "#{context.make_poop}"
puts "#{context.have_sex}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment