What is a combined mode?
The combined pattern allows us to combine objects of the same base type into a tree structure, where the parent node contains the same type of child nodes. In other words, this tree structure forms a "partial-whole" hierarchy. What is the "partial-overall" hierarchy? It is a hierarchy of a single object that contains both a combination of objects and a leaf node. Each group contains additional nodes, which can be leaf nodes or other compositions. This relationship repeats recursively in this hierarchy. Because each combination or leaf node has the same base type, the same operation can be applied to each of them without having to do type checking on the client. The difference between a client and a leaf node can be ignored when they are working on the combination.
Combined mode: Combines objects into a tree structure to represent a "part-to-whole" hierarchy. The combination makes the user consistent with the use of a single object and a composite object.
When do I use the combo mode?
@: Want to get a tree representation of object Abstraction (part--the overall hierarchy);
@: You want the client to handle all objects in the composite structure uniformly.
Using the combo mode in the cocoa touch frame
In the cocoa Touch Framework, UIView is organized into a composite structure. Each instance of UIView can contain other instances of UIView, forming a unified tree structure. Let the client treat the combination of a single UIView object and the UIView in a uniform manner.
The UIView in the window forms its child view internally. Each of them can contain other views and become a super view of their own child views. Add the other uiview that come in as its child view. Each of them can contain other views and become a super view of their own child views. The UIView object can have only one super view, and there are 0 or more child views.
The view composition structure participates in drawing event processing. When a request for a super view is rendered for the display, the message is processed first in the Hyper view and then passed to its child view. Messages propagate to other sub-views throughout the tree. Because they are the same type--uiview, they can be processed uniformly.
Instance references for combined patterns
First look at the static structure of the combined mode:
The base interface is a companyprotocol that defines the common operation of the Companyleaf class and the Companycomponent class. Some operations detachment component meaningful, such as Addcompany, Removecompany. Why not put these methods in the Companycomponent class? Because we don't want the client to know at runtime what type of node they are dealing with, nor do they want to leak the internal details of the composite structure to the client. This is why, although the operation only makes sense for companycomponent, we declare them on the base interface so that the various nodes have the same interface, so that the client can handle them uniformly.
The code for co-operation Companyprotocol is as follows:
#import <Foundation/Foundation.h> @protocol companyprotocol <nsobject>-(void) Addcompany: (id< companyprotocol>) company;-(void) Removecompany: (id<companyprotocol>) company;-(void) display; Display Head Office and subsidiary @end
Common Operations Define the addition of the company, delete the company, show the company three methods, we continue to see how the combined Companycomponent class is implemented, the code is as follows:
#import <Foundation/Foundation.h> #import "CompanyProtocol.h" @interface Companycomponent:nsobject < Companyprotocol> @property (nonatomic, copy) NSString *companyname;-(instancetype) Initwithcompanyname: (NSString * ) CompanyName; @end
#import "CompanyComponent.h" @interface CompanyComponent () @property (nonatomic, Strong) NSMutableArray *childList; @end @implementation companycomponent- (instancetype) Initwithcompanyname: (nsstring *) companyname { self = [super init]; if (self) { _ companyname = companyname; _childList = [[NSMutableArray alloc] initWithCapacity:0]; } return self;} - (void) Addcompany: (id<companyprotocol>) company { [self.childlist Addobject:company];} - (void) Removecompany: (id<companyprotocol>) company { [self.childlist removeobject:company];} - (void) diSplay { nslog (@ "Company Name:%@", self.companyname); for (id<companyprotocol> company in self.childlist) { [company display]; }} @end
the code for Companyleaf is as follows:
#import <Foundation/Foundation.h> #import "CompanyProtocol.h" @interface Companyleaf:nsobject < Companyprotocol> @property (nonatomic, copy) NSString *companyname;-(instancetype) Initwithcompanyname: (NSString * ) CompanyName; @end
#import "CompanyLeaf.h" @implementation companyleaf- (Instancetype) Initwithcompanyname: (nsstring *) companyname { self = [super init]; if (self) { _companyName = companyName; } return self;} - (void) Addcompany: (id<companyprotocol>) company { // child nodes Although these methods are also implemented, But do not do any processing, so it is convenient for the client to make calls, eliminating the process of judging the type. // This method child node does not have}- (void) Removecompany: (id<companyprotocol>) company { // child nodes implement these methods, but do not do any processing, so that the convenience of the client to make calls, eliminating the process of judging the type. // This method child node does not have}- (void) Display { nslog (@ "Company Name:%@" , self.companyname);} @end
from the code we can see that although there is addcompany in the companyleaf, but did not do any processing, the Companyleaf class does not implement this method, so the advantage of writing is to maintain the consistency of the interface, In this way the client does not have to differentiate the combination type from the leaf type.
The client code is called as follows:
#import "ViewController.h" #import "CompanyProtocol.h" #import "CompanyComponent.h" #import " CompanyLeaf.h "@interface ViewController () @end @implementation viewcontroller- (void) Viewdidload { [super viewdidload]; companycomponent *root = [[companycomponent alloc] initwithcompanyname:@ "Toot NIU Technology Co., Ltd."]; // Add a leaf node [root addCompany:[[CompanyLeaf alloc] initwithcompanyname:@ "Toot NIU Human Resources department"]]; companycomponent * component = [[companycomponent alloc] initwithcompanyname:@ "Shenzhen Vision Co., Ltd. (Dudu cattle subsidiary)"]; [component addcompany:[[companyleaf alloc] initwithcompanyname:@ "Human Resources Division"]; // Add a composite node [root addCompany:component]; nsloG (@ "-----------------Chart----------------"); [root display]; }- (void) didreceivememorywarning { [super Didreceivememorywarning]; // dispose of any resources that can be recreated.} @end
The output is as follows:
2015-09-10 22:22:27.493 component[27847:655455]-----------------structure----------------2015-09-10 22:22:27.494 COMPONENT[27847:655455] Company name: Toot NIU Technology Co., Ltd. 2015-09-10 22:22:27.494 component[27847:655455] Company name: Toot NIU Human Resources department 2015-09-10 22:22:27.494 component[27847:655455] Company name: Shenzhen Visual Lattice Co., Ltd. (Toot NIU subsidiary) 2015-09-10 22:22:27.495 component[27847:655455] Company name: Human resources Department of Visual lattice
The primary intent of the composite pattern is to have each node in the tree structure have the same abstract interface. This allows the entire structure to be used as a unified abstract structure without leaking its internal representations. Any operation on each node (leaf node or composition) can be done through the same interface that the protocol or abstract base class can only blame.
Demo Link Address: https://github.com/guoshimeihua/Component.git
iOS design mode--combined mode