让父类具有仅由其后代使用的私有方法是一种很好的做法吗?

问题描述

我正在尝试遵循 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 是不好的做法吗?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...