iOS design mode-combination
Schematic diagram
Description
Combining objects into a tree structure to represent a "partial-whole" hierarchy, combining patterns makes the user consistent with the use of individual objects and composite objects. The key to mastering the combination pattern is to understand the meaning of "part/whole" as well as "single object" and "combined object".
Source
Https://github.com/YouXianMing/CompositePattern
////File.h//Compositepattern////Created by youxianming on 15/10/20.//copyright©2015 year Zipeiyi. All rights reserved.//#import<Foundation/Foundation.h>typedefenum: Nsuinteger {kfolder,//folderKfile,//file} FileType;@interfaceFile:nsobject#pragmaMark-Basic Properties/** * File type*/@property (nonatomic) FileType FileType;/** * The name of the folder or file*/@property (nonatomic, strong) NSString*name;/** * Add Files * * @param file files*/- (void) Add: (File *) file;/** * All Files * * @return Files folder*/-(Nsarray <file *> *) files;#pragmaMark-Convenience Builder/** * returns the created folder or file * * @param type file types * @param name file names * * @return created instance objects*/+ (Instancetype) Createwithfiletype: (FileType) type name: (NSString *) name;@end
////FILE.M//Compositepattern////Created by youxianming on 15/10/20.//copyright©2015 year Zipeiyi. All rights reserved.//#import "File.h"@interfaceFile () @property (nonatomic, strong) Nsmutablearray<file *> *Filesarray;@end@implementationFile-(instancetype) init { self=[Super Init]; if(self) {Self.filesarray=[Nsmutablearray array]; } returnSelf ;}- (void) Add: (File *) file {[Self.filesarray addobject:file];}-(Nsarray <file *> *) files {returnSelf.filesarray;}+ (Instancetype) Createwithfiletype: (FileType) type name: (NSString *) name {File*file = [[Selfclass] [alloc] init]; File.filetype=type; File.name=name; returnfile;}@end
Details
iOS design mode-combination