OPEN/CLOSED PRINCIPLE

类或者方法,对扩展是开放的,对修改是关闭的。

class UsageFileParser
  def initialize(client, usage_file)
    @client = client
    @usage_file = usage_file
  end

  def parse
    case @client.usage_file_format
    when :xml
      parse_xml
    when :csv
      parse_csv
    end
  end

  private

  def parse_xml
  end

  def parse_csv
  end
end

# refactor

class UsageFileParser
  def initialize(client, parser)
    @client = client
    @parser = parser
  end

  def parse(usage_file)
    @parser.parse(usage_file)
    @client.last_parse = Time.current
    @client.save
  end
end

class XmlParser
  def parse(usage_file)
  end
end

class CsvParsere
  def parse(usage_file)
  end
end
class Charger
  def charge
    case phone_type
    when iphone
      use_lightning
    when android_phone
      user_type_c
    end
  end

  private

  def use_lightning
  end

  def use_type_c
  end
end

# refactor

class Changer
  def initialize(phone)
    @phone = phone
  end

  def charge
    @phone.charge
  end
end

class IPhone
  def charge
    use_lightning
  end
end

class AndroidPhone
  def charge
    use_type_c
  end
end

Reference