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

查看:199
本文介绍了Objective-C - 模板方法模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在阅读关于Objective-C的模板方法,我正在试图了解它们有什么特别之处。从我的理解在Base类中的任何方法都可以被覆盖,超级可以被调用?那么一个模板方法不仅仅是覆盖基类中的一个方法?



如果我错了,你可以解释一个模板方法模式,你可以提供一个例子吗?

解决方案

是的,模板模式不仅仅是覆盖基类中的一个方法



当具体定义算法的轮廓时,可以使用模板模式,然而算法的步骤是抽象的。这意味着这些步骤可以以不同的方式实现。但是,算法的一般大纲预计不会改变。



我刚刚创建的一个例子:

  class Life {

public method goThroughTheDay(){
goToWork();
eatLunch();
comeBackHome();
programABitMore();
}
抽象方法goToWork();
抽象方法eatLunch();
抽象方法comeBackHome();
抽象方法programABitMore();
}

class GoodLife延长生命{
//覆盖所有抽象方法
}

//客户端应用程序
人生=新的GoodLife();
life.goThroughTheDay();

基本上,在Life类中具体定义了一天的预期运行方式。然而,该过程的细节由子类(即GoodLife)来处理。 GoodLife类将实现与可能的ToughLife类非常不同的步骤。



这种模式有一些变化;例如一些步骤也可以具体地定义。在这个例子中,eatLunch()可以在Life类中具体定义;这意味着子类不会改变这种行为。



如果你有一个相对复杂的算法可以以不同的方式实现,这种模式是非常有意义的。 / p>

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



我以某种方式错过了Objective-C在我答案中的部分。以下是Objective-C中的内容:

  @interface生活:NSObject 

- (无效)goThroughTheDay;

- (void)goToWork; //抽象
- (void)eatLunch; //抽象
- (void)comeBackHome; // abstract
- (void)programABitMore; //抽象

@end

@implementation生活

- (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:生活

@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可以在这里找到。


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.

这篇关于Objective-C - 模板方法模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆