iOS設計模式-工廠模式

工廠模式分為簡單工廠模式,工廠模式、抽象工廠模式三類。

簡單工廠模式

1. 定義

簡單工廠模式并不是常用的設計模式之一,它只算是工廠模式的特殊實現(xiàn)??梢愿鶕煌膮祦韺嵗煌膶ο螅@些對象通常有共同的父類或者接口。

2. 適用場景
  1. 需要創(chuàng)建的對象較少。
  2. 客戶端不關心對象創(chuàng)建的過程。
3. 簡單工廠示例

創(chuàng)建一個可以繪制不同形狀的繪圖工具,可以繪制圓形,正方形,三角形,每個圖形都會有一個draw方法用于繪圖.

  1. 創(chuàng)建圖像父類Shape
   @interface Shape : NSObject

   - (void)draw;

   @end
  1. 創(chuàng)建繼承此父類的具體圖形類,分別重寫父類中的draw方法,這里分別創(chuàng)建了圓形、長方形、正方形

    #import "Circle.h"
    // 圓形類
    @implementation Circle
    
    - (void)draw {
        NSLog(@"圓形");
    }
    
    @end
    
    #import "Rectangle.h"
    // 長方形類
    @implementation Rectangle
    
    - (void)draw {
        NSLog(@"長方形");
    }
    
    @end
    
    #import "Square.h"
    // 正方形類
    @implementation Square
    
    - (void)draw {
        NSLog(@"正方形");
    }
    
    @end
    
    
  2. 創(chuàng)建工廠類,用來獲取實例

    @implementation ShapeFactory
    
    + (Shape *)getShape:(ShapeFactoryType)type {
        if (type == ShapeFactoryTypeCircle) {
            return [[Circle alloc] init];
        } else if (type == ShapeFactoryTypeRectangle) {
            return [[Rectangle alloc] init];
        } else if (type == ShapeFactoryTypeSquare) {
            return [[Square alloc] init];
        }
        
        return [[Shape alloc] init];
    }
    
    @end
    
    
  3. 測試方法

        // 簡單工廠模式
        Shape *shape = [ShapeFactory getShape:ShapeFactoryTypeCircle];
    //    Shape *shape = [ShapeFactory getShape:ShapeFactoryTypeRectangle];
    //    Shape *shape = [ShapeFactory getShape:ShapeFactoryTypeSquare];
        [shape draw];
    
    

工廠模式

1. 定義

工廠模式是簡單工廠模式的進一步深化,在工廠模式中我們將不再提供一個統(tǒng)一的工廠類去創(chuàng)建對象,而是針對不同的對象提供不同的工廠。將創(chuàng)建具體產品類的工作延遲到子類去完成。

2. 適用場景
  1. 對象創(chuàng)建過程復雜或對象創(chuàng)建要滿足某些條件,使用者不容易掌握,并且也不需要知道具體細節(jié)。
  2. 對象的創(chuàng)建和使用要進行解耦。
3. 工廠模式示例

現(xiàn)在需要設計一個這樣的圖片加載類,它具有多個圖片加載器,用來加載jpg,png,gif格式的圖片,每個加載器都有一個load()方法,用于讀取圖片。

  1. 創(chuàng)建抽象產品了類,ImageLoader

    @interface ImageLoader : NSObject
     
    - (void)loadImage;
     
    @end
    
    
  2. 創(chuàng)建具體的產品類,PNG加載器、GIF加載器、JPG加載器,并復寫父類的loadImage方法。

    #import "PNGImageLoader.h"
    
    @implementation PNGImageLoader
    
    - (void)loadImage {
        NSLog(@"加載PNG格式圖片");
    }
    
    @end
    
    #import "GIFImageLoader.h"
    
    @implementation GIFImageLoader
    
    - (void)loadImage {
        NSLog(@"加載GIF格式圖片");
    }
    
    @end
    
    #import "JPGImageLoader.h"
    
    @implementation JPGImageLoader
    
    - (void)loadImage {
        NSLog(@"加載JPG格式圖片");
    }
    
    @end
    
    
  3. 創(chuàng)建工廠抽象類ImageFactory

    @interface ImageFactory : NSObject
    
    - (ImageLoader *)getImageLoader;
    
    @end
    
    
  4. 創(chuàng)建工廠具體類

    #import "PNGImageFactory.h"
    #import "PNGImageLoader.h"
    
    @implementation PNGImageFactory
    
    - (ImageLoader *)getImageLoader {
        return [PNGImageLoader new];
    }
    
    @end
    
    #import "GIFImageFactory.h"
    #import "GIFImageLoader.h"
    
    @implementation GIFImageFactory
    
    - (ImageLoader *)getImageLoader {
        return [GIFImageLoader new];
    }
    
    @end
    
    #import "JPGImageFactory.h"
    #import "JPGImageLoader.h"
    
    @implementation JPGImageFactory
    
    - (ImageLoader *)getImageLoader {
        return [JPGImageLoader new];
    }
    
    @end
    
    

    5.測試方法,只需要知道具體的工廠類即可使用

    // 工廠模式
    //    ImageFactory *fac = [[PNGImageFactory alloc] init];
    //    ImageFactory *fac = [[GIFImageFactory alloc] init];
    ImageFactory *fac = [[JPGImageFactory alloc] init];
    ImageLoader *imageoadler =[fac getImageLoader];
    [imageoadler loadImage];
    
    

抽象工廠模式

1. 定義

抽象工廠模式是工廠模式的進一步延伸,提供了創(chuàng)建了一系列相關或相互依賴對象的接口。在抽象工廠模式中,每個具體的工廠都提供了多個工廠方法(>=2)用來創(chuàng)建不同類型的具體對象。

2. 適用場景

  1. 和工廠模式一樣客戶端無需知道創(chuàng)建對象的類,只需要知道具體工廠類。
  2. 需要一組對象共同完成某種功能時,并且可能存在多組對象完成不同功能的情況。
  3. 系統(tǒng)結構穩(wěn)定,不會頻繁的增加對象。(增加新的產品族符合開閉原則,但增加新的產品等級結構,需要修改所有的工廠角色,包括抽象工廠類,違背了開閉原則

3.抽象工廠示例

某軟件公司欲推出一款新的手機游戲軟件,該軟件能夠支持iOS、Android和Windows Mobile等多個智能手機操作系統(tǒng)平臺,針對不同的手機操作系統(tǒng),該游戲軟件提供了不同的游戲操作控制(OperationController)類和游戲界面控制(InterfaceController)類,并提供相應的工廠類來封裝這些類的初始化過程。軟件要求具有較好的擴展性以支持新的操作系統(tǒng)平臺,為了滿足上述需求,試采用抽象工廠模式對其進行設計。

  1. 創(chuàng)建抽象產品類OperationController和InterfaceController

    @interface OperationController : NSObject
    
    - (void)control;
    
    @end
    
    @interface InterfaceController : NSObject
    
    - (void)display;
    
    @end
    
    
  2. 創(chuàng)建具體產品類,每個系統(tǒng)的操作控制和界面控制,并重寫父類方法

    // 操作控制器
    #import "WindowsOperationController.h"
    
    @implementation WindowsOperationController
    
    - (void)control {
        NSLog(@"windows 控制器");
    }
    
    @end
    
    #import "iOSOperationController.h"
    
    @implementation iOSOperationController
    
    - (void)control {
        NSLog(@"iOS控制器");
    }
    
    @end
    
    #import "AndroidOperationController.h"
    
    @implementation AndroidOperationController
    
    - (void)control {
        NSLog(@"Android控制器");
    }
    
    @end
    
    // 界面控制器
    #import "WindowsInterfaceController.h"
    
    @implementation WindowsInterfaceController
    
    - (void)display {
        NSLog(@"渲染Windows界面");
    }
    
    @end
    
    #import "iOSInterfaceController.h"
    
    @implementation iOSInterfaceController
    
    - (void)display {
        NSLog(@"渲染iOS界面");
    }
    
    @end
    
    #import "AndroidInterfaceController.h"
    
    @implementation AndroidInterfaceController
    
    - (void)display {
        NSLog(@"渲染Android界面");
    }
    
    @end
    
    
  3. 創(chuàng)建抽象工廠類

    @interface SystemFactory : NSObject
    
    - (OperationController *)getOperation;
    
    - (InterfaceController *)getUI;
    
    @end
    
    
  4. 創(chuàng)建具體工廠類

    #import "WindowsFactory.h"
    #import "WindowsInterfaceController.h"
    #import "WindowsOperationController.h"
    
    @implementation WindowsFactory
    
    - (OperationController *)getOperation {
        return [WindowsOperationController new];
    }
    
    - (InterfaceController *)getUI {
        return [WindowsInterfaceController new];
    }
    
    @end
    
    #import "iOSFactory.h"
    #import "iOSInterfaceController.h"
    #import "iOSOperationController.h"
    
    @implementation iOSFactory
    
    - (OperationController *)getOperation {
        return [iOSOperationController new];
    }
    
    - (InterfaceController *)getUI {
        return [iOSInterfaceController new];
    }
    
    @end
    
    #import "AndroidFactory.h"
    #import "AndroidInterfaceController.h"
    #import "AndroidOperationController.h"
    
    @implementation AndroidFactory
    
    - (OperationController *)getOperation {
        return [AndroidOperationController new];
    }
    
    - (InterfaceController *)getUI {
        return [AndroidInterfaceController new];
    }
    
    @end
    
  5. 測試方法

    //    SystemFactory *system = [[iOSFactory alloc] init];
    //    SystemFactory *system = [[AndroidFactory alloc] init];
       SystemFactory *system = [[WindowsFactory alloc] init];
       InterfaceController *ui = [system getUI];
       OperationController *opera = [system getOperation];
       [opera control];
       [ui display];
    

示例代碼

Demo地址

參考

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容