Objective-C-Template方法模式?[英] Objective-C - Template methods pattern?

本文是小编为大家收集整理的关于Objective-C-Template方法模式?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

因此,我一直在阅读有关Objective-C上的模板方法的阅读,我正在尝试了解它们有什么特别之处.从我的理解来看,基类中的任何方法都可以过度缠身,可以称呼超级?因此,模板方法除了覆盖基类中的方法外,还有什么其他的吗?

如果我错了,您能说明什么是模板 - 模式模式,您可以提供一个例子吗?

推荐答案

是的,模板模式不仅仅是基类中的方法.

算法的轮廓具体定义时,可以使用

模板图案,但是算法的步骤抽象为抽象.这意味着可以以不同的方式实施这些步骤.但是,算法的一般轮廓不会改变.

我刚刚创建的一个例子:

class Life {

   public method goThroughTheDay(){
     goToWork();
     eatLunch();
     comeBackHome();
     programABitMore();
   }
   abstract method goToWork();
   abstract method eatLunch();
   abstract method comeBackHome();
   abstract method programABitMore();
}

class GoodLife extends Life {
   //override all the abstract methods here
}

//The client application
Life life = new GoodLife();
life.goThroughTheDay();

基本上,预计每天倒塌的方式是在生活阶层中具体定义的.但是,该过程的细节是由子类(即GoodLife)照顾的. GoodLife课程将与可能的艰难阶级班的实施步骤截然不同.

这种模式有一些变化;例如,某些步骤也可以具体定义.在示例中,可以在生命阶级中定义eatlunch().这意味着不能期望子类改变这种行为.

如果您具有可以以不同方式实现的相对复杂的算法,则模式很有意义.

================================== p>

我在答案中以Objective-c错过了这一部分.这是Objective-C中的外观:

@interface Life : NSObject

- (void) goThroughTheDay;

- (void) goToWork; // Abstract
- (void) eatLunch; // Abstract
- (void) comeBackHome; // Abstract
- (void) programABitMore; // Abstract

@end

@implementation Life

- (void) goThroughTheDay {

    [self goToWork];
    [self eatLunch];
    [self comeBackHome];
    [self programABitMore];
}

- (void) goToWork { [self doesNotRecognizeSelector:_cmd]; }
- (void) eatLunch { [self doesNotRecognizeSelector:_cmd]; }
- (void) comeBackHome { [self doesNotRecognizeSelector:_cmd]; }
- (void) programABitMore { [self doesNotRecognizeSelector:_cmd]; }

@end

@interface GoodLife : Life

@end

@implementation GoodLife

- (void) goToWork { NSLog(@"Good Work"); }
- (void) eatLunch { NSLog(@"Good Lunch"); }
- (void) comeBackHome { NSLog(@"Good Comeback"); }
- (void) programABitMore { NSLog(@"Good Programming"); }

@end

Objective-C没有对抽象类的内置支持,因此我使用doesNotRecognizeSelector:方法围绕它进行了工作.可以找到有关抽象类和Objective-C的更多详细信息, a>.

其他推荐答案

我认为我应该给出一个更具客观的特定答案.您可以阅读有关Cocoa在苹果上使用的模板方法可可设计模式页面.一个例子是drawRect:模板方法.像其他模板方法一样,您永远不会直接调用模板方法.它由setNeedsDisplay调用.这一点是允许框架优化图形.如果您直接打电话给drawRect:自己可能会多次进行重新绘制.

实际上,您可能应该尝试使您想要在子类中覆盖的每种方法一个模板方法.这减少了知道您是否应该在替补时应调用基类实施的问题并使调试更容易.您可以在基类中放置一个断点,而不是子类中的所有覆盖方法.

其他推荐答案

从我的理解来看,基类中的任何方法都可以过度缠绕,可以称呼超级?因此,模板方法除了覆盖基类中的方法外,还有什么其他的吗?

是的,这只是覆盖基类方法的更多内容.模板方法是实现算法的一种方法,其中一些步骤依赖于子类.例如,考虑一个基础中具有三个主要步骤的方法,即F1,F2和F3.每个步骤都包含许多语句.在此处,F1和F3在所有子类中都是相同的,但是F2是子类依赖性的.那在这里做什么呢?您可以在子类复制语句中的整个方法中覆盖F1和F3的语句,但这是浪费.因此,您仅在子类中提供F2.在这种情况下,您可以定义算法(执行F1,然后执行F2,然后执行F3),但在基类中,但为子类提供了压倒性的钩子(F2).请注意,基类中的模板方法是最终的,因此子类无法更改算法,例如.它不能更改F1,F2和F3的顺序,两个子类都无法省略步骤F1,F3.

简而言之,模板方法模式不仅是覆盖的,而且是使用继承来处理某些特定情况.这不是特定于OBJ-C,我不能为您提供任何特定于OBJ-C的东西.为了了解这种模式,我建议以下内容:

  1. gof li>
  2. 头部首次设计模式
  3. wikipedia template方法上的文章

,网络上还有大量的教程/文章.基本思想不是obj-c依赖.

本文地址:https://www.itbaoku.cn/post/627789.html

问题描述

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 be over-ridden and super can be called? So is a template method anything more than overriding a method in the base class?

If I'm wrong, can you please explain what a template-method-pattern is, and can you provide an example?

推荐答案

Yes, the template pattern is a bit more than just overriding a method in the base class.

Template pattern can be used when an outline of an algorithm is concretely defined, however the steps of the algorithm are left abstract. That means that the steps can be implemented in different ways. But, the general outline of the algorithm is not expected to change.

An example that I have just created on the fly:

class Life {

   public method goThroughTheDay(){
     goToWork();
     eatLunch();
     comeBackHome();
     programABitMore();
   }
   abstract method goToWork();
   abstract method eatLunch();
   abstract method comeBackHome();
   abstract method programABitMore();
}

class GoodLife extends Life {
   //override all the abstract methods here
}

//The client application
Life life = new GoodLife();
life.goThroughTheDay();

Basically, the way a day is expected to run down is concretely defined in the Life class. However, the details of the process are taken care by the subclass (ie. GoodLife). GoodLife class will implement steps very differently than a possible ToughLife class.

There are some variations to this pattern; for example some of the steps can also be concretely defined. In the example, the eatLunch() can be concretely defined in the Life class; meaning that the subclasses are not expected to change this behaviour.

The pattern makes a lot of sense if you have a relatively complex algorithm that could be implemented in different ways.

======================================

I somehow missed the part with Objective-C in my answer. Here is how it would look in Objective-C:

@interface Life : NSObject

- (void) goThroughTheDay;

- (void) goToWork; // Abstract
- (void) eatLunch; // Abstract
- (void) comeBackHome; // Abstract
- (void) programABitMore; // Abstract

@end

@implementation Life

- (void) goThroughTheDay {

    [self goToWork];
    [self eatLunch];
    [self comeBackHome];
    [self programABitMore];
}

- (void) goToWork { [self doesNotRecognizeSelector:_cmd]; }
- (void) eatLunch { [self doesNotRecognizeSelector:_cmd]; }
- (void) comeBackHome { [self doesNotRecognizeSelector:_cmd]; }
- (void) programABitMore { [self doesNotRecognizeSelector:_cmd]; }

@end

@interface GoodLife : Life

@end

@implementation GoodLife

- (void) goToWork { NSLog(@"Good Work"); }
- (void) eatLunch { NSLog(@"Good Lunch"); }
- (void) comeBackHome { NSLog(@"Good Comeback"); }
- (void) programABitMore { NSLog(@"Good Programming"); }

@end

Objective-C doesn't have built-in support for abstract classes, so I worked around it using the doesNotRecognizeSelector: method. A lot more details about the abstract classes & Objective-C can be found here.

其他推荐答案

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.

其他推荐答案

From my understanding any method in a Base class can be over-ridden and super can be called? So is a template method anything more than overriding a method in the base class?

Yes, this is much more that just overriding a base class method. Template methods is a way to implement an algorithm where some steps are subclass dependent. For example consider a method in Base which has three major steps, f1, f2 and f3. Each steps contains a number of statements. Here f1 and f3 are same in all subclass, but f2 is subclass dependent. So what to do here? You can override the whole method in subclass copying statements of f1 and f3, but that is a waste. So you provide only f2 in subclass. And in this is way you define the algorithm (execute f1, then execute f2, then execute f3) in base class but provide overriding hooks for subclasses (f2). Note that, template method in base class is final, so subclass can't change the algorithm, e.g. it can't change the order of f1, f2 and f3, neither subclass can omit the steps f1, f3.

In short, Template Methods pattern is not just overriding, it is the use of inheritance to handle some specific situations. This is not Obj-C specific, and I can't provide you anything specific to Obj-C. To get an overall idea about this pattern, I recommend the followings:

  1. Design Patterns book by GoF
  2. Head First Design Patterns
  3. Wikipedia article on Template Method

And there are also tons of tutorials/articles on web. The basic idea is not Obj-C dependent.