Skip to content

Instantly share code, notes, and snippets.

@johnc219
Created September 14, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnc219/73a262720952ee29cecd4368312a6e83 to your computer and use it in GitHub Desktop.
Save johnc219/73a262720952ee29cecd4368312a6e83 to your computer and use it in GitHub Desktop.
## -----------------------------------------------------------------------------
## Replace Type Code With Subclasses
## Replace Conditional with Polymorphism
## -----------------------------------------------------------------------------
# Start
class Animal
def initialize(type_code)
@type_code = type_code
end
def speak
case @type_code
when :human
puts 'hello'
when :dog
puts 'bark'
when :cat
puts 'meow'
end
end
def population
case @type_code
when :human
puts 7_500_000_000
when :dog
puts 525_000_000
when :cat
puts 600_000_000
end
end
end
# Refactor
class Animal
def self.create(type_code)
case type_code
when :human
new Human
when :dog
new Dog
when :cat
new Cat
else
raise 'not an animal'
end
end
def speak
# abstract
end
def population
# abstract
end
end
class Human < Animal
def speak
puts 'hello'
end
end
class Dog < Animal
def speak
puts 'bark'
end
end
class Cat < Animal
def speak
puts 'meow'
end
end
# Using refactored version
def speak(type)
Animal.create(type).speak
end
def population(type)
Animal.create(type).population
end
## -----------------------------------------------------------------------------
## Replace Type Code with State/Strategy
## Replace conditional with Polymorphism
## -----------------------------------------------------------------------------
# Start
class Animal
def initialize(type_code)
@type_code = type_code
end
def speak
case @type_code
when :human
puts 'hello'
when :dog
puts 'bark'
when :cat
puts 'cat'
end
end
def population
case @type_code
when :human
puts 7_500_000_000
when :dog
puts 525_000_000
when :cat
puts 600_000_000
end
end
end
# Refactor
class Animal
def self.create(type_code)
new Animal(type_code)
end
def initialize(type_code)
@type = AnimalType.new_type(type_code)
end
def mutate(type_code)
@type = AnimalType.new_type(type_code)
end
def type_code
@type.type_code
end
def speak
@type.peak
end
def population
@type.population
end
end
class AnimalType
HUMAN = :human
DOG = :dog
CAT = :cat
def self.new_type(type_code)
case type
when HUMAN
new Human
when DOG
new Dog
when CAT
new Cat
else
raise "not a valid type code"
end
end
def type_code
# abstract
end
def speak
# abstract
end
def population
# abstract
end
end
class Human < AnimalType
def type_code
AnimalType::HUMAN
end
def speak
puts 'hello'
end
def population
puts 7_500_000_000
end
end
class Dog < AnimalType
def type_code
AnimalType::DOG
end
def speak
puts 'bark'
end
def population
puts 525_000_000
end
end
class Cat < AnimalType
def type_code
AnimalType::CAT
end
def speak
puts 'meow'
end
def population
puts 600_000_000
end
end
# Using refactored version
def mutate_and_speak(original_type, mutation_type)
animal = Animal.create(original_type)
animal.speak
animal.mutate(mutation_type)
animal.speak
end
def mutate_and_population(original_type, mutation_type)
animal = Animal.create(original_type)
animal.population
animal.mutate(mutation_type)
animal.population
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment