1

我正在尝试遵循 SOLID 概念,同时保持干燥(不要重复自己)。

我有一个父类在它的子类之间共享一个方法,但是这个方法没有在父类本身中使用:

class ParentClass
  def locals
    { id: ..., result: ... }
  end
  ...
  private

  def method_used_in_descendants  # this method is not used in ParentClass
  end
end

class ChildClass < ParentClass
  def locals
    super.merge { whatever: method_used_in_descendants, one_result: ... }
  end
  ...
end

class AnotherChildClass < ParentClass
  def locals
    super.merge { whatever: method_used_in_descendants, another_result: ... }
  end
  ...
end

# And not all the descendants of ParentClass need method_used_in_descendants:
class AnotherChildClass < ParentClass
  def locals
    super.merge { whatever: my_private_method }
  end
  ...
end

这种方法method_used_in_descendants是在ParentClass不好的做法中创建的吗?

谢谢

4

0 回答 0