0

I am currently developing an API that uses abstract classes with default implementations. While I have done this in the past I have not really thought of the philosophical implications of calls back to base.SomeMethod() in the derived class since the APIs were not intended for public consumption. Since this API is intended to be publicly visible, I am wondering how I should handle calls back into the base class. Is it OK for me to assume that the base call will always happen first or last in the derived implementation and I simply document the assumption for the implementer? Or should the base class calls have no side effects, so it wouldn't matter when the implementer calls the base class? Is there a rule of thumb for this?

Thanks.

4

1 回答 1

0

恕我直言,这在很大程度上取决于您特定班级的需求。如果您想确保调用基本行为(甚至可能以特殊顺序),您可以使用模板方法。我发现这种模式是一种以受控方式提供子类扩展点的非常优雅的方式,我认为这比给开发人员留下评论要好。

明显的缺点是,如果您选择错误的钩子,您可能会过多地限制其他开发人员。我记得有一两次不得不重写模板方法,因为钩子不允许我执行我需要的行为。

高温高压

于 2012-12-26T14:00:57.930 回答