根据 The Pragmatic Programmer 一书“正交性与 DRY 原则密切相关”。我不确定我是否按照作者希望读者的方式理解它。所以我问上面的问题。
例如,您有 A 类和 B 类。这两个类都有相似的方法。使用DRY原理,我做了一个C类,然后把A类和B类的类似方法移到C类,把C类指定为A和B的父类,是不是就不是正交的了?
根据 The Pragmatic Programmer 一书“正交性与 DRY 原则密切相关”。我不确定我是否按照作者希望读者的方式理解它。所以我问上面的问题。
例如,您有 A 类和 B 类。这两个类都有相似的方法。使用DRY原理,我做了一个C类,然后把A类和B类的类似方法移到C类,把C类指定为A和B的父类,是不是就不是正交的了?
Firstly, subtyping is not primarily about code reuse (though that is a side-benefit). You don't have two classes descend from a third simply because they have some code in common; you do it when instances of the child classes should be able to be taken for instances of the parent class in all contexts (the subtype instances can be substituted for a supertype).
Secondly, Hunt & Thomas's use of the term "orthogonality" focuses on different modules, not design or implementation considerations within a module. More specifically, it has to do with the lack of interdependence between modules. Two modules are orthogonal if changes to one do not affect the other. More typically, "orthogonality" has a different meaning (language features can be combined arbitrarily, rather than disallowing certain features in certain contexts, or having different versions of the same operation for different types), and you'd speak of "coupling" (interdependence between modules, the inverse of H & T's "orthogonality") and "cohesion".