Objective-C - Template methods pattern?

前端 未结 3 920
南旧
南旧 2020-12-16 04:11

So I\'ve been reading about template methods on Objective-C and I am trying to understand what\'s so special about them. From my understanding any method in a Base class can

3条回答
  •  死守一世寂寞
    2020-12-16 04:48

    I thought I should give a more Objective-C specific answer. You can read about the template method as used in Cocoa on apples Cocoa Design Patterns pages. An example of this is the drawRect: template method. Like other template methods, you never call a template methods directly yourself. It is called by setNeedsDisplay. The point of this is to allow the framework to optimize the drawing. If you called drawRect: directly yourself you might end up doing redraws unnecessary many times.

    In fact you should probably try to make every method you want to override in a subclass a template method. This reduce the problem of knowing whether you should call the base class implementation when you override and makes debugging easier. You can just put a breakpoint in the base class, instead of every overridden methods in the subclasses.

提交回复
热议问题