Android Builder mode easy to understand

Source: Internet
Author: User

1. The builder mode means to construct (assign) the parts of an object at 1.1 points, like building a house on a site, which part of the brick is to be set up in a single way, otherwise the default bricks are used.

2. Here is a car class, the car class contains several of its own corresponding properties, its object will be created with some default values. But we can call the Builder class object of the car class as needed to assign a value to the specific property of the car object.

This means that there will be a builder class in the car's interior, which uses the builder pattern to create the method, and his method will return the builder object, so that the assignment method in the Builder object can be constantly called to make a concrete build of the car object.

 Public classCar {PrivateString name; Private floatSpeed ; Private floatPrice ;  PublicCar () {//car sets parameters when initializing         This. Name = "Car";  This. Speed =102f;  This. Price =1000f; } @Override PublicString toString () {return"car{" + "name=" + name + "\" + ", speed=" + Speed + ", price=" + Price + ‘}‘; }     Public Static classBuilder {PrivateCar MCar;  PublicBuilder () {MCar=NewCar (); }         PublicBuilder setName (String name) {Mcar.name=name; return  This; }         PublicBuilder Setspeed (floatSpeed ) {Mcar.speed=Speed ; return  This; }         PublicBuilder Setprice (floatPrice ) {Mcar.price=Price ; return  This; }         PublicCar Build () {returnMCar; }    }}

Android Builder mode easy to understand

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.