[Design mode] javascript builder Mode

Source: Internet
Author: User

[Design mode] javascript builder Mode
Builder mode description 1. separates the construction of a complex object from its representation so that the same creation process can have different representations. This is called the builder mode. 2. description in object-oriented language. Main roles: 1>. the Builder interface class defines the Builder's [Worker] and unified operational behavior. It represents a complex structure object; 2>. concreteBuilder is used to create instance objects in various forms of [Implementation] Builder to represent different representations of Builder. 3>. ctor this conductor is used to guide the execution process and form of the Builder instance. It is used to separate the Builder instance performance and to guide the Builder instance to create and generate product results in a certain sequence of rules. 4>. all the results created by ResultObject generate a result object. This is the result created by the specific creator Based on Director. 3. the builder mode is actually a conductor, a builder, and a customer who uses the conductor to call the work of a specific builder and has to get results from the specific builder; 4. builder mode, simulated scenario: [see an example that describes the builder mode very well] said a family wants to build a house, but the house owner or other people in the House do not know how to build the house, so he has to ask several workers and the house building team has to have a foreman, to build a house according to the idea of the homeowner, And the foreman designs how to do the house according to the requirements of the homeowner. The foreman said that the first step is to build the overall skeleton of the house, the second step is the construction of the bedroom, the third step is the decoration of the kitchen, the fourth step is the completion of the construction of the living room, the fifth step... the foreman does not do things, but the specific builder must follow the foreman's requirements. The first step is the second step until the whole house is finished; the creator must have all the skills to create the house, that is, to create a skeleton, to decorate the bedroom ..., that is, what the builder does, or has the ability to do, must be greater than or equal to what the conductor requires to do, or has the ability; that is, the conductor is an organizer, and the Builder provides skills; 5. in a weak language like JavaScript, if there is no interface, ignore the interface definition layer, directly create a specific builder, and then create a guide class to call the builder back and forth; instance Source Code 1. worker builder X: function workerBuilder () {this. workOne = function () {// house skeleton} this. workTwo = function () {// create a bedroom} this. workThree = function () {// create a kitchen} this. workFour = function () {// create a living room }//.... this. getResult = function () {// build a house var House = new house (); // house. houseFrame... return house ;}} workBuilder is a specific builder class. workOne and Two are the tasks to be done, and the skeleton can be built. Of course, workBuilder can be created several more times to indicate that the worker's methods for each job are different; but the work content is the same; 2. baton class function ctor () {this. construct = function (builder) {builder. workOne (); builder. workTwo (); builder. workThree (); builder. workFour ();//... // The above content can be set in sequence, and the work item can also be set} the guiding method under the conductor class, there is a callback reference to the builder, the content includes several or all items of the builder's work; the conductor organizes and arranges the tasks to be done by the builder's worker; 3. product House function House () {this. houseFrame = ''; this. room = ''; this. kitchen = ''; this. livingRoom = '';//...} 4. usage var builder = new workBuilder (); var director = new Director (); director. construct (builder); var house = builder. getResult (); Step 4: The entire use is equivalent to the customer: the homeowner. The homeowner asks Director to build the house, but the foreman does not do anything, so he directs the builder to build the house, finally, the homeowner obtained the house from the workers;

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.