组合

作者: XZhongWen | 来源:发表于2019-03-28 17:20 被阅读0次

    iOS设计模式 - 组合

    原理图

    Composite.png

    说明

    将对象组合成树形结构表示"部分-整体"的层次结构, 组合模式使得用户对单个对象和组合对象的使用具有一致性

    代码实现

    Client

    //
    //  ViewController.m
    //  Composite
    //
    //  Created by mye on 2019/3/28.
    //  Copyright © 2019 mye. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "File.h"
    #import "Directory.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // client
        Directory *root = [[Directory alloc] initWithName:@"root"];
        File *file1 = [[File alloc] initWithName:@"file1"];
        File *file2 = [[File alloc] initWithName:@"file2"];
        File *file3 = [[File alloc] initWithName:@"file3"];
        Directory *subDir1 = [[Directory alloc] initWithName:@"subDir1"];
        [root addFile:file1];
        [root addFile:file2];
        [root addFile:file3];
        [root addFile:subDir1];
        
        File *subFile1 = [[File alloc] initWithName:@"subFile1"];
        File *subFile2 = [[File alloc] initWithName:@"subFile2"];
        File *subFile3 = [[File alloc] initWithName:@"subFile3"];
        [subDir1 addFile:subFile1];
        [subDir1 addFile:subFile2];
        [subDir1 addFile:subFile3];
        
        [root displayAllFile];
    }
    
    
    @end
    
    

    AbstractFile

    //
    //  AbstractFile.h
    //  Composite
    //
    //  Created by mye on 2019/3/28.
    //  Copyright © 2019 mye. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface AbstractFile : NSObject
    
    @property (nonatomic, copy) NSString *fileName;
    
    - (instancetype)initWithName:(NSString *)fileName;
    - (NSString *)getInfo;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    //
    //  AbstractFile.m
    //  Composite
    //
    //  Created by mye on 2019/3/28.
    //  Copyright © 2019 mye. All rights reserved.
    //
    
    #import "AbstractFile.h"
    
    @implementation AbstractFile
    
    - (instancetype)initWithName:(NSString *)fileName {
        self = [super init];
        if (self) {
            self.fileName = fileName;
        }
        return self;
    }
    
    - (NSString *)getInfo {
        return self.fileName;
    }
    
    @end
    
    

    File

    //
    //  File.h
    //  Composite
    //
    //  Created by mye on 2019/3/28.
    //  Copyright © 2019 mye. All rights reserved.
    //
    
    #import "AbstractFile.h"
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface File : AbstractFile
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    //
    //  File.m
    //  Composite
    //
    //  Created by mye on 2019/3/28.
    //  Copyright © 2019 mye. All rights reserved.
    //
    
    #import "File.h"
    
    @implementation File
    
    @end
    
    

    Directory

    //
    //  Directory.h
    //  Composite
    //
    //  Created by mye on 2019/3/28.
    //  Copyright © 2019 mye. All rights reserved.
    //
    
    #import "AbstractFile.h"
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface Directory : AbstractFile
    
    @property (nonatomic, strong) NSMutableArray<AbstractFile *> *files;
    
    /**
     添加文件
    
     @param file 文件
     */
    - (void)addFile:(AbstractFile *)file;
    
    /**
     删除文件
    
     @param file 文件
     */
    - (void)removeFile:(AbstractFile *)file;
    
    /**
     显示所有文件
     */
    - (void)displayAllFile;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    //
    //  Directory.m
    //  Composite
    //
    //  Created by mye on 2019/3/28.
    //  Copyright © 2019 mye. All rights reserved.
    //
    
    #import "Directory.h"
    
    @implementation Directory
    
    /**
     添加文件
     
     @param file 文件
     */
    - (void)addFile:(AbstractFile *)file {
        [self.files addObject:file];
    }
    
    /**
     删除文件
     
     @param file 文件
     */
    - (void)removeFile:(AbstractFile *)file {
        [self.files removeObject:file];
    }
    
    /**
     显示所有文件
     */
    - (void)displayAllFile {
        for (AbstractFile *file in self.files) {
            NSLog(@"%@", [file getInfo]);
            if ([file isKindOfClass:[Directory class]]) {
                Directory *dir = (Directory *)file;
                [dir displayAllFile];
            }
        }
    }
    
    - (NSMutableArray<AbstractFile *> *)files {
        if (!_files) {
            _files = [NSMutableArray array];
        }
        return _files;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:组合

          本文链接:https://www.haomeiwen.com/subject/rmdcbqtx.html