In many cases we need to define a class and there are a lot of member variables in it.
Class Person {PrivateString name;Private intAgePrivate intSexPrivate intHighPrivate intFacePrivate intWeightPrivate intFoot Public Person() {...} Public Person(String name) {...} ... Public Person(String name,intAgeintSexintHighintFaceintWeightintfoot) {}}
We provide a number of constructors so that we do not allow a constructor to be too long, so we do not seem to be very intuitive when we create a new object.
Person p = new Person();p.setName();p.setAge();...p.setWeight();
Of course, we still have this way, javabean this way can sometimes feel that the code is too many lines.
So there's the builder pattern for new use for classes that have too many member variables, presumably defined as follows:
Class Person {PrivateString name;Private intAgePrivate intSexPrivate intHighPrivate intFacePrivate intWeightPrivate intFoot Public Person(Builder builder) {name = Builder.name; age = Builder.age; sex = Builder.sex; High = Builder.high; face = Builder.face; Weight = builder.weight; foot = Builder.foot; } Public Static classBuilder {PrivateString name;Private intAgePrivate intSexPrivate intHighPrivate intFacePrivate intWeightPrivate intFoot PublicPersonBuild() {return NewPerson ( This); } PublicBuilderSetName(String name) { This. name = name;return This; } PublicBuilderSetage(intAge) { This. Age = Age;return This; } PublicBuilderSetsex(intSex) { This. sex = sex;return This; } PublicBuilderSethigh(intHigh) { This. High = high;return This; } PublicBuilderSetface(intFace) { This. face = Face;return This; } PublicBuilderSetweight(intWeight) { This. Weight = weight;return This; } PublicBuilderSetfoot(intfoot) { This. foot = foot;return This; } }}
And then we just write it in the demo test.
Person p = new Person.Builder().setAge(12).setFace(12).setFoot(12).setHigh(180).setName("jayu").setSex(1).setWeight(89).build();
is not looking more refreshing, after the use of many member variables in this way to write more force lattice, is not it.
Use builder mode to beautify your code