The builder mode of the big talk Design Model

Source: Internet
Author: User

Pangu opened up the world, with the body to create the sun, moon, stars, mountains and plants. The turbidity that remains in the sky and the earth slowly turns into insects, birds, and animals, adding anger to the quiet world. At this time, there was a goddess nvwa, walking in this reckless wilderness. She looked around, mountain ups and downs, rivers and streams, dense jungle, grass and wood striving for glory, hundreds of birds singing, on the ground group of animals, fish playing in the water, insects jumping in the grass, the world is also beautifully decorated. However, she always felt that there was a kind of unspeakable loneliness. The more she looked at it, the more she felt tired and the more she felt. She couldn't even figure out why. With mountains and rivers and vegetation tells the heart of the irritability, mountains and trees do not understand her words at all; to insects, birds, and animals pour out their thoughts, insects, birds, and animals can understand her distress.

She sat down next to a pond and looked at her own shadows. Suddenly a leaf falls into the pool, and a small ripple of static pool water makes her shadow shake slightly. She suddenly felt the knot had been solved, right! Why does she have that unspeakable sense of loneliness? It turns out that the world lacks a creature like her. Thinking of this, she immediately dug some dirt in the pool by her hand, and moved it to the water, and pinched it according to her own shadow. With a pinch, it becomes a small thing. It looks similar to nvwa, and it also has facial features and two hands. After being pinched, I put it on the ground and got alive. As soon as the nvwa saw it, she was filled with joy and then squeezed a lot. She called these little things "man ". Today, our big talk design begins with the story of a woman, who wants to build a person. The first thing we need to do is to create a head, body, arm, and leg, this is about a manufacturing process. I don't know if the reader feels like this. The Builder mode is very similar to the engineering mode, but the builder mode provides a more fine-grained object construction process, let's take a look at the structure diagram of the builder mode:

Take the above-mentioned nvwa as an example to look at our code. First, nvwa needs to create an object to be constructed first, as shown below:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> // object to be constructed public class Robust {String head; String body; String arm; String leg; public String getHead () {return head;} public void setHead (String head) {this. head = head;} public String getBody () {return body;} public void setBody (String body) {this. body = body;} public String getArm () {return arm;} public void setarm (String arm) {this. arm = arm;} public String getLeg () {return leg;} public void setLeg (String leg) {this. leg = leg ;}}Then there is an interface of the constructor, including the construction process that the builder will use.

public interface Builder {public void buidBody(String body);public void buidHead(String head);        public void buidArm(String arm);        public void buidLeg(String leg);public Robust getRobust();}public class RobustBuilder implements Builder {Robust robust = new Robust();        public void buidBody(String body)         {   robust.setBody(body);}public void buidHead(String head)        {           robust.setHead(head);}        public void buidArm(String arm)        {           robust.setArm(arm);}        public void buidLeg(String leg)        {           robust.setLeg(leg);}        public Robust getRobust()         {           return robust;}}
With the builder, a conductor is required to instruct the construction process:

// Command the public class Director {public Robust Construct (Builder builder) {builder. buidBody (null); builder. buidHead (null); builder. buidArm (null); builder. buidLeg (null); return builder. getRobust ();}}
In this way, the process of building people by the Queen is complete. From the code point of view, if we want to separate the complex types of building rules and the internal composition of the types, you can also consider using the builder mode when you want to use the same build process to build different types. From the application perspective, if we want to decouple the product creation process and specific accessories, or we can consider using the builder mode when we want to create and reuse a stable and complex logic for all products. Design tour, To be continued ......

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.