iOS design mode-combination

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.