Application of Android Builder mode in development

Source: Internet
Author: User

Recently, when learning the picture loading frame glide, he was shocked by his streamlined wording. In a word, it can be done.

Glide.with (mcontext).     load (URL)     . Centercrop ()     . Placeholder (r.drawable.default_img)     . Crossfade ( )     . into (Mimageview);

 

At the same time at the beginning of the year, when learning Rxjava, with a chain-style programming is very elegant.

     Observable                . Just ("1", "2")                . Subscribeon (Schedulers.io ())                . Observeon (Androidschedulers.mainthread ())                . Subscribe (New action1<string> () {                    @Override public                    void Call (String s) {                    }                });

  

Learn today, builder mode is simple to use in Android.

the benefits of Builder
    • Code readability is good, only need to write a code, chain programming is elegant
    • Convenient. You can create different objects with different combinations
    • Decoupling. You do not need to know the details to separate the object itself from the object construction process.

How do I write Builder mode?

Package com.app.lib;/** * Created by ${zyj} on 2016/6/27. */public class Usermoule {    private String ID;    private String name;    Public Usermoule (string ID, string name) {        this.id = ID;        this.name = name;    }    Static class Builder {        private  String ID;        Private  String name;        Public Builder setId (String id) {            this.id = ID;            return this;        }        Public Builder setName (String name) {            this.name = name;            return this;        }        Public Usermoule Create () {            return new Usermoule (ID, name);}}}    

Call

      New Usermoule.builder ().                setId ("id").                setName ("JSON")                . Create ();

  

Why does Builder use Static modification?

In general, the intention of writing inner classes is to show that the class and the host class are closely related, while private, static, final these can be taken alone, the difference between static is larger, there is no static modification, indicating that this class must depend on the object of the host class, and there is static decoration, It does not depend on the existence of the object, the class name. You can add static to the whole similar method. The private thing depends entirely on whether you want to expose the class or not, and final is to indicate that the class is not inheritable.

And

Using static as you create a new Java file, the static inner class does not maintain a reference to the external class. If your builder needs references to external classes, it doesn't make sense to build, so you have to add static.

Summary
    • Use Builder mode to decouple and make it easier to construct different objects
    • Builder class to be decorated with static
    • The simplified builder pattern is used here, omitting the abstract builder, and also omitting the role of the mentor.

RELATED Links: Http://www.jianshu.com/p/5d9db54892c8

Application of Android Builder mode in development

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.