模版方法模式定义: 核心思想是在抽象类的一个方法中定义某些“标准”算法。在这个方法中调用的基本操做应由子类重载来实现。这个方法能够被称为所谓的“模板”,它定义方法实现中缺乏了一些针对性的操做。算法
第一点:定义一个操做中的算法框架 第二点:将一些步骤延时到子类实现 第三点:子类能够不改变算法结构,能够从新定义算法的某些特定步骤swift
二、模版方法模式应用场景 场景一:多个子类有公有方法,且逻辑基本相同 场景二:重复、复杂的算法,将核心算法设计为模版方法,其周边细节能够由各个子类实现 场景三:代码重载框架
使用模板方法要思考如下几点:ide
1.在父类中一次性实现算法中的不可变部分,并将可变的行为留给子类来实现。idea
2.子类共用的行为应该被提出来放到公共类中, 以免代码重复。.net
3.要考虑一些特殊状况特殊处理。这里采用子类的扩展来实现。能够定义一些在特定点调用“钩子”操做方法。子类能够经过对钩子操做的实现从而在这些点上扩展功能。钩子操做默认状况是不对整个模板形成影响的。子类重载后,才为模板算法提供附加的操做。设计
模版方法模式->角色划分 两个角色 角色一:抽象类->做用(定义算法框架结构,将一些特定步骤延时到子类实现) 角色二:具体模版实现类,不改变算法结构,选择性去实现某些特定步骤。3d
模板类:code
[@interface](https://my.oschina.net/u/996807) AnySandwich : NSObject { } - (void) make; // Steps to make a sandwich - (void) prepareBread; - (void) putBreadOnPlate; - (void) addMeat; - (void) addCondiments; - (void) extraStep; - (void) serve; [@end](https://my.oschina.net/u/567204) @implementation AnySandwich - (void) make { [self prepareBread]; [self putBreadOnPlate]; [self addMeat]; [self addCondiments]; [self extraStep]; [self serve]; } - (void) putBreadOnPlate { // We need first to put bread on a plate for any sandwich. NSLog(@"放盘"); } - (void) serve { // Any sandwich will be served eventually. NSLog(@"服务"); } #pragma mark - #pragma Details will be handled by subclasses - (void) prepareBread { [NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]; } - (void) addMeat { [NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]; } - (void) addCondiments { [NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]; } - (void) extraStep{} @end
汉堡类:orm
#import "AnySandwich.h" @interface Hamburger : AnySandwich { } - (void) prepareBread; - (void) addMeat; - (void) addCondiments; //- (void) extraStep; // Hamburger specific methods - (void) getBurgerBun; - (void) addKetchup; - (void) addMustard; - (void) addBeefPatty; - (void) addCheese; - (void) addPickles; @end @implementation Hamburger - (void) prepareBread; { [self getBurgerBun]; } - (void) addMeat { [self addBeefPatty]; } - (void) addCondiments { [self addKetchup]; [self addMustard]; [self addCheese]; [self addPickles]; } #pragma mark - #pragma mark Hamburger Specific Methods - (void) getBurgerBun { // A hamburger needs a bun. NSLog(@"添加小圆面包"); } - (void) addKetchup { NSLog(@"添加番茄酱"); // Before adding anything to a bun, we need to put ketchup. } - (void) addMustard { NSLog(@"添加芥末"); // Then add some mustard. } - (void) addBeefPatty { NSLog(@"添加牛肉"); // A piece of beef patty is the main character in a burger. } - (void) addCheese { NSLog(@"添加起司"); // Let's just assume every burger has cheese. } - (void) addPickles { NSLog(@"添加咸菜"); // Then finally add some pickles to it. } @end
热狗类:
#import "AnySandwich.h" @interface Hotdog : AnySandwich { } - (void) prepareBread; - (void) addMeat; - (void) addCondiments; //- (void) extraStep; // Hotdog specific methods - (void) getHotdogBun; - (void) addWiener; - (void) addKetchup; - (void) addMustard; - (void) addOnion; @end @implementation Hotdog - (void) prepareBread { [self getHotdogBun]; } - (void) addMeat { [self addWiener]; } - (void) addCondiments { [self addKetchup]; [self addMustard]; [self addOnion]; } #pragma mark - #pragma mark Hotdog Specific Methods - (void) getHotdogBun { // First of all, we need a hotdog bun. } - (void) addWiener { // A nice piece of wiener is the main character here. } - (void) addKetchup { // Every hotdog needs ketchup. } - (void) addMustard { // I think mustard is also needed. } - (void) addOnion { // I think adding onion is a good idea. } @end
class ConmentComputerLine: NSObject { final func makeComputer(){ System() DisPlay() Memory() MainBoard() Screen() Power() Hook() } func System(){ print("系统") } func DisPlay(){ print("显卡") } func Memory(){ print("内存") } func MainBoard(){ print("主板") } func Power(){ print("电源") } func Screen(){ print("显示器") } //钩子 给子类添加额外操做 func Hook(){} }
MacComputer:
class MacComputer: ConmentComputerLine { override func System() { print("MAC OS") } override func Hook() { TouchBoard() } func TouchBoard(){ print("触摸板") } }
WindowsComputer:
class WindowsComputer: ConmentComputerLine { override func System() { print("Windows2000 OS") } override func Hook() { Mouse() keyBoard() } func mouse(){ print("鼠标") } func keyBoard(){ print("键盘") } }