Objective-c design Pattern-builder Builder (object creation)

Source: Internet
Author: User

Generator

Generator, also a builder pattern, is also the design pattern when creating objects. There is a director (the conductor) in this mode, and the client knows that the class reference is used to create the product. There is also a builder (builders) who knows the details of the specific object being created.

The conductor only plays a coordinating role, and it has a builder reference to command how the builder creates the object, built within the builder to build the algorithm for the object.

The generator pattern, in fact, is the pattern that is created to customize some particular object. The length of the object is often very complex, and if the object is not created with this pattern, the object creation class will be very redundant and difficult to reuse. Using generators to separate the presentation layer from the creation layer is much easier to extend and decouple.

Usage Scenarios

1. You need to create complex objects that involve a variety of parts. The algorithm that creates the object should be independent of how the assembly is assembled. A common example is building composite objects.

2. The build process requires building objects in different ways (for example, different combinations of parts or performance).

Generator vs. Abstract Factory

Generator

Building Complex objects

Building objects in multiple steps

Return the product in the final step of the build object process

Focus on a specific product

Abstract Factory

Building Simple or complex objects

Building objects in a single step

Building objects in a single way

Return to Product now

Emphasize a set of products

Demo

For example, a car factory needs to make two different kinds of cars:

Prime Minister create car class with name and Speed two properties

#import <Foundation/Foundation.h>@interface*name; @property (Nonatomic, Assign)int speed ; @end #import " Car.h " @implementation Car @end

Create Builder,builder encapsulates the specifics of creating a class

#import<Foundation/Foundation.h>#import "Car.h"@interfaceCarbuilder:nsobject-(Carbuilder *) Buildcar;-(Carbuilder *) Buildcarname;-(Carbuilder *) Buildcarspeed;-(Car *) getproduct;@end#import "CarBuilder.h"@implementationcarbuilder{Car*_product;}-(Carbuilder *) buildcar{_product=Nil; _product= [CarNew]; returnSelf ;}-(Carbuilder *) buildcarname{_product.name=@"Benz"; returnSelf ;}-(Carbuilder *) buildcarspeed{_product.speed= -; returnSelf ;}@end

Specific builder classes:

#import<Foundation/Foundation.h>#import "CarBuilder.h"@interfaceCarbuilderbenz:carbuilder@end#import "CarBuilderBenz.h"@implementationcarbuilderbenz{Car*_product;}-(Carbuilder *) buildcar{_product=Nil; _product= [CarNew]; returnSelf ;}-(Carbuilder *) buildcarname{_product.name=@"Benz"; returnSelf ;}-(Carbuilder *) buildcarspeed{_product.speed= -; returnSelf ;}-(Car *) getproduct{return_product;}@end#import<Foundation/Foundation.h>#import "CarBuilder.h"@interfaceCarbuilderbmw:carbuilder@end#import "CarBuilderBMW.h"@implementationcarbuilderbmw{Car*_product;}-(Carbuilder *) buildcar{_product=Nil; _product= [CarNew]; returnSelf ;}-(Carbuilder *) buildcarname{_product.name=@"BMW"; returnSelf ;}-(Carbuilder *) buildcarspeed{_product.speed= -; returnSelf ;}-(Car *) getproduct{return_product;}@end

Director class, director know the production of products, but do not know the specific production details.

#import<Foundation/Foundation.h>#import "CarBuilder.h"#import "Car.h"@interfaceCardirector:nsobject-(Car *) Createcarbenz: (Carbuilder *) builder;-(Car *) CREATECARBMW: (Carbuilder *) builder;@end#import "CarDirector.h"@implementationCardirector-(Car *) Createcarbenz: (Carbuilder *) builder{[builder Buildcar];    [Builder Buildcarname];    [Builder Buildcarspeed]; return[builder getproduct];}-(Car *) CREATECARBMW: (Carbuilder *) builder{[builder Buildcar];    [Builder Buildcarname];    [Builder Buildcarspeed]; return[builder getproduct];}@end

Client code:

        New ];         New ];         New ];         *benz = [director Createcarbenz:benzbuilder];         *BMW = [director Createcarbmw:bmwbuilder];        NSLog (@ "benz:name =%@, speed =%d", Benz.name, benz.speed);        NSLog (@ "bmw:name =%@, speed =%d", Bmw.name, bmw.speed);

Results:

--£ º:04.298 builder[41745:  10008190--:04.298 builder[41745:10008190

As you can see, different builder packages have different creation object logic, resulting in different objects, while separating the creation layer and presentation layer.

Objective-c design Pattern-builder Builder (object creation)

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.