An example of the application of the builder model in the development of IOS app design model _ios

Source: Internet
Author: User
Tags abstract

defines                                                                                                              

The

Separates the build of a complex object from its performance so that the same build process can create different manifestations. The
look at this concept, may feel very abstract, can understand but do not know what use. Let's make an analogy to understand the definition above. Before we make a metaphor, let's talk about what this design pattern is for. Why do we use this model? The builder model is responsible for decoupling the process of building complex objects from its parts, that is, process and component decoupling. For example, a car is a very complex object, it has a lot of parts, wheels, engines, seats, doors, tanks and so on, its assembly process is also very complex (requires a professional to assemble the steps), the builder model is to separate parts and assembly process. Similarly, we use the same computer, there are many parts, the assembly process is also very complex (of course, for professionals like us may not feel complicated). The greatest benefit of the builder model is that it separates the construction process from the performance, so if you need to change the performance of a product, you just need to redefine a specific builder (which is a bit difficult to understand, or a car to use as an analogy, we will be able to assemble the car independently, using this assembly process, We could assemble a BMW, or we could assemble a Mercedes or other models, and we just need to redefine a specific builder (a class for product performance).

motives

In software systems, there are times when the creation of a complex object (such as the car in the example above) is encountered, it usually consists of a number of parts of the child objects using a certain algorithm (process) composition; Due to the change of requirements, the various parts of this complex object often face drastic changes (for example, the car doors of various models, The steering wheel, engine, etc., are not the same), but the algorithm (process) that combines the parts together is relatively stable.

The builder model is born under such requirements, encapsulating the change point (component) so that the same build process can create different performance.

The builder pattern is the pattern that is applied when the algorithm for creating complex objects should be independent of the components of the object and the way they are assembled.

The builder model includes product class (products), abstract Builder Class (Builder), Concrete builder class (ConcreteBuilder1, ConcreteBuilder2 ...). ) and conductor class (Director)

Use the various classes from the following code:

Copy Code code as follows:

Personbuilder *builder = [[Personthinbuilder alloc]init];
Personview *personview = [Persondirector Creatperson:builder];

Among them Personbuilder is the abstract builder class, Personthinbuilder is the concrete builder class, Personview is the product class, the Persondirector is the conductor class.

The code understands how to use it:

1, the creation of concrete builders.
2. The conductor returns the product through the concrete builder.

The Sensory builder model is very similar to the factory method pattern, but it is joined by the command class.

Structure diagram

As you can see from the structure diagram, the generator pattern has two important roles: Director (mentor) and builder (builder). Director knows what builder should be built (the process of construction), and builder knows how to build it (performance). A construct method is defined in the Director class to instruct specific builders ConcreteBuilder objects to Buildpart. Builder is an abstract interface (protocol) that contains methods for building the various parts (Buildpart) for building actual product products, as well as a GetResult method for returning build finished product to the client.

Do not know that people do not feel very abstract? Let's use an example of life to speak in layman's parlance. For example, now I have to build a house in my hometown, first I do not know how to repair the house (walls, lack of builders), and then I do not know how to design (repair a few rooms, the layout of the room, the room window how to design and so on, lack of instructors), so I find a bunch of migrant workers (builders), they will be walls, and also I have to find a designer ( Mentor), he knows how to design, and finally, I also want to ensure that the migrant workers (Builders) listen to the designer (mentor) guidance, where needs to build a wall, where to install Windows and so on, so that migrant workers (builders) began building the house, in the construction process, the designer (mentor) is responsible for the design and release of orders. After the house was built, the migrant workers (Builders) handed in my room. To put it bluntly, the Director (mentor) is responsible for controlling the macroscopic aspect (process), Builder (builder) to control the microscopic aspect (performance).

Let's go through the code to illustrate the structure diagram below.

Instance

Builder-Mode code

PRODUCT.M (partial code):

Copy Code code as follows:

-(ID) init

{

Self = [Superinit];

if (self)

{

Arrparts = [Nsmutablearrayarray];

}

returnself;

}

-(void) Addpart: (NSString *) part

{

[Arrpartsaddobject:part];

}

-(void) show

{

For (NSString *strpart inarrparts)

{

NSLog (@ "%@", Strpart);

}

}


Builder.h (partial code):
Copy Code code as follows:

@classProduct;

@protocol Builder <NSObject>

-(void) addpartone;

-(void) addparttwo;

-(Product *) GetResult;

@end


CONCRETEBUILDER.M (partial code):
Copy Code code as follows:

-(ID) init

{

    self = [superinit];

   

    if (self)

    {

        product = [[Productalloc] init];

   }

   

    returnself;

}

 

-(void) Addpartone

{

    [productaddpart:@ "Part One"];

}

 

-(void) Addparttwo

{

    [productaddpart:@ "Part Two"];

}

 

-(Product *) GetResult

{

    returnproduct;

}

Director.m (partial code):

-(void) construct: (id<builder>) Builder

{

  &nbs P [Builder Addpartone];

    [builder Addparttwo];

}


Client Invoke Code:
Copy Code code as follows:

Director *director = [[Directoralloc] init];

id<builder> Builder = [[Concretebuilderalloc] init];

[director Construct:builder];

Product *product = [Builder GetResult];

[Product show];

[Builder release];

[Director release];

when to use builder mode

The builder pattern is often used in the following situations:

You need to create complex objects that involve a variety of parts. The algorithm for creating an object should be independent of the Assembly method of the part.
The build process needs to build objects in different ways.

PS: In Facebook's open source animation framework Pop, there are similar applications for builder pattern:

Copy Code code as follows:

Popanimatableproperty *animatableproperty = [Popanimatableproperty propertywithname:@ "Property" initializer:^ ( Popmutableanimatableproperty *prop) {
Prop.writeblock = ^ (id obj, const cgfloat values[]) {
};
Prop.readblock = ^ (id obj, cgfloat values[]) {
};
}];


Here the initializer is essentially builder, just a different name.

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.