Android Self-learning process-builder () mode

Source: Internet
Author: User

the previous article, in the study when OKHttp met builderpattern, of course, I didn't know it was builder mode. , just think strange how to follow a lot of points, and then understand that this is the Android Builder mode . A little study of Android , must have used alertdialog.buider, say ashamed! There was no blogging at the time, and it was confined to the classes that would be used. Through this, I hope to know more.

Builder Design pattern in Java

  The builder design pattern is a creative type pattern in Java. For example, to create an object, like the Factory method design pattern, she is also a form of creative design. When we learn any kind of design pattern, I suggest finding the real problem that this design pattern solves. Its been well said necessity are mother on invention. (God's sentence, to the effect that it was created because of demand). Learning Design Patterns from real-world problems doesn't work, and conversely, if you're already having problems then you'll learn more about design patterns and learn how to solve problems. in this tutorial on Java design Patterns, we will first understand the actual problems that Builder design pattern solves, which will give us some hints on when we should use the builder model. We will then see examples to understand the pros and cons of the builder model.

What problem Builder pattern solves in Java

as I said before,Builder pattern is a creative pattern, which means that the problem he solves is related to the creation of objects. Constructors in Java are often used to create objects, and can carry the parameters needed to create an object. The problem is that when creating an object can have many parameters, some parameters may be mandatory and some may be optional. Let's imagine that there is a class that is used to create a cake, and now you need to remember some parameters such as the proportion of eggs, milk, and flour to create a cake. Some of them are mandatory, and some are optional, for example, Cherry,fruits. If we overload the constructors for every different object, there will be a lot of constructors, and the worst is that they will have a lot of parameters.

Problem:

1) Too many constructors are required

2) error prone due to too many fields. For example

Sugar and and butter is in cups so instead of 2 cup sugar if you pass 2 cup butter, your compiler won't complain but W Ill get a buttery cake with almost no sugar with high cost of wasting butter.

(ability to this, really do not understand)

Example of Builder Design pattern in Java

We will use the same example by using the builder design pattern,

Guidelines for Builder design pattern in Java

1. Create a static inner class called Builder (a static nested Class), which objects within this class will be created by builder. In this example, the object is cake.

2.

3.

4.

5.

Builderpatternexample.java

 Public classBuilderpatternexample { Public Static voidMain (String arg[]) {//Creating object using Builder pattern in JavaCake Whitecake =Newcake.builder (). Sugar (1). Button (0.5). Egg (2). Vanila (2). Flour (1.5). Bakingpowder (0.75). Milk (0.5). Builder (); //Cake is ready to eat:)System.out.printIn (Whitecake); }}

Cake.class

Class cake{Private Final DoubleSugar//Cup    Private Final DoubleButter//Cup    Private Final integgs; Private Final intVanila;//Spoon    Private Final Doubleflour;//Cup    Private Final DoubleBakingpowder//Spoon    Private Final DoubleMilk//Cup    Private Final intCherry;  Public Static classBuilder {Private DoubleSugar//Cup        Private DoubleButter//Cup        Private integgs; Private intVanila;//Spoon        Private DoubleBakingpoeder//Spoon        Private DoubleMilk//Spoon        Private intCherry; //builder methods for setting property         PublicBulider Sugar (DoubleCup) { This. sugar = Cup;return  This;}  PublicBuilder Butter (DoubleCup) { This. butter = Cup;return  This; }  PublicBuilder Eggs (intNumber) { This. Eggs = number;return  This; }  PublicBuilder Vanila (intSpoon) { This. Vanila = Spoon;return  This; }  PublicBuilder Flour (DoubleCup) { This. flour = Cup;return  This; }  PublicBuilder Bakingpowder (DoubleSpoon) { This. sugar = spoon;return  This; }  PublicBuilder Milk (DoubleCup) { This. milk = Cup;return  This; }  PublicBuilder Cherry (intNumber) { This. cherry = number;return  This; } //return fully Build object         PublicCake Build () {return NewCake ( This); }                //private constructor to enforce object creation through builder    PrivateCake (Builder builder) { This. Sugar =Builder.sugar;  This. Butter =Builder.butter;  This. Eggs =Builder.eggs;  This. Vanila =Builder.vanila;  This. Flour =Builder.flour;  This. Bakingpowder =Builder.bakingpowder;  This. Milk =Builder.milk;  This. Cherry =Builder.cherry; } @Override PublicString toString () {return"cake{" + "sugar=" + Sugar + ", butter=" + Butter + ", eggs=" + Eggs + ", vanila=" + Vanila + ", flour=" + Flour + ", Baki Ngpowder= "+ Bakingpowder +", milk= "+ Milk +", cherry= "+ Cherry + '} '; }

Output
cake{sugar=0.75, butter=0.5, eggs=2, vanila=2, flour=1.5, bakingpowder=0.0, milk=0.5, cherry=0}

Builder design pattern in Java–pros and Cons (Pros and cons)

There are drawbacks to the constructor pattern, but there are still a lot of advantages relative to the pros. There are some advantages and disadvantages about how the Builder design pattern is when creating objects.

Advantages:

1. More maintainable If you create an object that requires more than five or more fields (parameters)

2. Fewer errors

3. More robust, only a complete constructor can be obtained by the client

Disadvantages:

1. Lengthy code duplication, builders need to copy from the original copy of the domain or project class

When to use Builder Design pattern in Java

The builder pattern is a creative pattern and should be used when the number of parameters of a constructor is redundant and manageable. Do not confuse builder and factory patterns, there is a clear difference between them.

As Factory can used to create different implementation of same interface, Builder is tied up with its Container clas S and only returns object of Outer class.

More details:

Translation: http://www.javacodegeeks.com/2012/07/builder-design-pattern-in-java.html

Http://www.javacodegeeks.com/2012/07/builder-design-pattern-in-java.html

http://jlordiales.me/2012/12/13/the-builder-pattern-in-practice/

Http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern

Android Builder pattern

Android Self-learning process-builder () mode

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.