An example analysis of applying the combinatorial pattern in the design pattern in IOS application development _ios

Source: Internet
Author: User
Tags abstract tree

What is a combination mode?
combined mode lets you 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 "part-whole" hierarchy? It is a hierarchical structure of a single object that contains both a combination of objects and a leaf node. Each group contains other nodes, which can be leaf nodes or other combinations. 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 type check on the client. The differences between the clients can be ignored when they operate on the combination and leaf nodes.

Group mode: The grouping of objects into a tree structure to represent the "part-whole" hierarchy. A combination makes the user consistent in the use of a single object and a grouped object.

When to use combination mode?

1. Want to get object abstract tree representation (part--overall hierarchy);

2. Want the client to uniformly process all objects in the composite structure.

Using combination mode in the cocoa touch frame

In the cocoa touch frame, the UIView is organized into a composite structure. Each UIView instance can contain other instances of UIView, forming a unified tree structure. Let the client treat the combination of a single UIView object and a UIView.

The UIView inside the window forms its own child view. Each of them can contain other views and become a hyper view of their own child views. The other UIView added in is its child view. Each of them can contain other views and become a hyper view of their own child views. A UIView object can have only one super view and 0 to multiple child views.

The view composition structure participates in drawing event handling. When the request Hyper view is rendered for display, the message is processed in the Hyper view and then passed to its child view. Messages are propagated to other child views throughout the tree. Because they are the same type--uiview, they can be treated uniformly.

Instance references for combined mode

The application of combinatorial mode is that when you find that a requirement is a structure that embodies part and whole levels, and you want users to be able to ignore the differences between the grouped objects and the individual objects, you should consider using the combination mode when using all the objects in the composite structure uniformly. A combination pattern defines the class hierarchy of basic objects and grouped objects. The base objects can be grouped into more complex combinations of objects, which can be grouped together so that they are recursively recursive, so that in the client code, any use of the base object can be used in a combination object. Having said so much, in fact, the combination mode is to allow customers to use the combination structure and individual objects consistently. Then, the following or give a class structure diagram, we must look at the understanding.

The relationship between classes in the above diagram can be analogous to that of a tree. There are roots (Component), Side points (composite), and leaf nodes (leaf). The logic is clear and the structure is simple. In fact, the equivalent leaf node (leaf) and side point (composite) are inherited from the root node (Component). OK, here's a simple code implementation.

Note: All of the code in this article is compiled in an arc environment.

Comcomponents class interface//corresponding Graph component

Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface comcomponents:nsobject{
NSString *name;
}
-(comcomponents*) Myinit: (nsstring*) myname;
-(void) Add: (comcomponents*) C;
-(void) Remove: (comcomponents*) C;
-(void) Display: (int) Depth;
@end


Comcomponents class implementation
Copy Code code as follows:

#import "ComComponents.h"

@implementation comcomponents

-(comcomponents*) Myinit: (NSString *) myname{
name = MyName;
return self;
}
-(void) ADD: (comcomponents *) c{
Return
}
-(void) Remove: (comcomponents *) c{
Return
}
-(void) Display: (int) depth{
Return
}
@end


Leaf Type interface
Copy Code code as follows:

#import "ComComponents.h"

@interface leaf:comcomponents
-(leaf*) Myinit: (nsstring*) myname;
@end


The Leaf class realizes
Copy Code code as follows:

#import "Leaf.h"

@implementation Leaf

-(leaf*) Myinit: (NSString *) myname{
name = MyName;
return self;
}
-(void) ADD: (comcomponents *) c{
NSLog (@ "Cannot add to a leaf");
}
-(void) Remove: (comcomponents *) c{
NSLog (@ "cannot remove from a leaf");
}
-(void) Display: (int) depth{
NSLog (@ "[%dlevel]%@", depth,name);
}
@end


Composite class Interface
Copy Code code as follows:

#import "ComComponents.h"

@interface composite:comcomponents{
Nsmutablearray *children;
}
-(composite*) Myinit: (nsstring*) myname;
@end


Composite class implementation
Copy Code code as follows:

#import "Composite.h"

@implementation Composite

-(composite*) Myinit: (NSString *) myname{
name = MyName;
children= [Nsmutablearray new];
return self;
}
-(void) ADD: (comcomponents *) c{
[Children ADDOBJECT:C];
}
-(void) Remove: (comcomponents *) c{
[Children ADDOBJECT:C];
}
-(void) Display: (int) depth{
NSLog (@ "[%dlevel]%@", depth,name);
For (comcomponents *component in children)
[Component Display:depth +1];
}
@end


Main method call
Copy Code code as follows:

#import <Foundation/Foundation.h>
#import "Leaf.h"
#import "Composite.h"

int main (int argc,const char *argv[])
{
@autoreleasepool {
Composite *root = [[Composite alloc]myinit:@ ' root '];
[Root add:[[leaf alloc]myinit:@ "Leaf A"]];
[Root add:[[leaf alloc]myinit:@ "Leaf B"]];
Composite *comp = [[Composite alloc]myinit:@ "composite X"];
[Comp add:[[leaf alloc]myinit:@ "Leaf XA"]];
[Comp add:[[leaf alloc]myinit:@ "Leaf XB"]];
[Root Add:comp];
Composite *COMP2 = [[Composite alloc]myinit:@ "composite XY"];
[Comp2 add:[[leaf alloc]myinit:@ "Leaf Xya"];
[Comp2 add:[[leaf alloc]myinit:@ "Leaf xyb"];
[Comp ADD:COMP2];
[Root add:[[leaf alloc]myinit:@ "Leaf C"]];
Leaf *leaf = [[Leaf alloc]myinit:@ ' leaf D '];
[Root add:leaf];
[Root remove:leaf];
[Root display:1];
}
return 0;
}


Well, the combination mode structure is also simple, the key or ingenious ah, this is really difficult ah!




Related Article

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.