模版方法模式定義: 核心思想是在抽象類的一個方法中定義某些「標準」算法。在這個方法中調用的基本操做應由子類重載來實現。這個方法能夠被稱爲所謂的「模板」,它定義方法實現中缺乏了一些針對性的操做。算法
第一點:定義一個操做中的算法框架 第二點:將一些步驟延時到子類實現 第三點:子類能夠不改變算法結構,能夠從新定義算法的某些特定步驟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("鍵盤") } }