java to create a girlfriend class (object what new is) = = builder mode one click rewrite

  • 2021-07-22 09:44:28
  • OfStack

Create a girlfriend, She has a lot of attributes, For example, gender, Age, height, Weight, type and so on, Although every girlfriend has these attributes, But each person looks for the girlfriend request is not 1 kind, some people like the male, some people like the female, some likes the fat, the different person may according to own preferences to build the different girlfriend, we do not need to care about her is how to build, we only need to specify her attribute on the line

Compared with text explanation, I am more accustomed to code explanation. Let's realize how to use java to create a girlfriend for you step by step

First define a girlfriend class:


package nuoyanli;
 
/**
 * Created by ${nuoyanli} on 2019/4/7.
 */
 
public class GirlFriend {
  private String sex;// Gender 
  private int age;// Age 
  private int stature;// Height 
  private int weight;// Weight 
  private String type;// Type 

According to our previous understanding, if you want to create a girlfriend, you should directly come out of new. We can pass the attribute through the constructor

For example, I only have one requirement for my girlfriend, just be a woman, and define one construction method:


public GirlFriend(String sex) {
    this.sex = sex;
  }

And then create her when you need it:


 GirlFriend girlFriend = new GirlFriend(" Female ");

If we ask for gender and height, we have to define:


 public GirlFriend(String sex, int stature) {
    this.sex = sex;
    this.stature = stature;
  }

If you think about everyone's requirements, how many constructors you have to create, and there are many parameters, the readability is very poor, such as:


GirlFriend girlFriend = new GirlFriend(" Female ",19,170,90," Seiyuu ");

java has one builder mode:

Build an GirlFriendBuilder class:


package nuoyanli;
 
/**
 * Created by ${nuoyanli} on 2019/4/7.
 */
 
public class GirlFriendBuilder {
   String sex;// Gender 
   int age;// Age 
   int stature;// Height 
   int weight;// Weight 
   String type;// Type 
 
  public GirlFriendBuilder setSex(String sex) {
    this.sex = sex;
    return this;
  }
 
  public GirlFriendBuilder setAge(int age) {
    this.age = age;
    return this;
  }
 
  public GirlFriendBuilder setStature(int stature) {
    this.stature = stature;
    return this;
  }
 
  public GirlFriendBuilder setWeight(int weight) {
    this.weight = weight;
    return this;
  }
 
  public GirlFriendBuilder setType(String type) {
    this.type = type;
    return this;
  }
 
  /**
   * Return 1 A GirlFriend Object 
   */
  public GirlFriend build() {
    return new GirlFriend(this);
  }
}

Then the constructor in the GirlFriend class passes in the GirlFriendBuilder object:


public GirlFriend(GirlFriendBuilder builder) {
    this.sex = builder.sex;
    this.age = builder.age;
    this.stature = builder.stature;
    this.weight = builder.weight;
    this.type = builder.type;
  }

Then when you create it:


GirlFriend girlFrie1nd = new GirlFriendBuilder()
        .setAge(19)
        .setSex(" Female ")
        .setType(" Seiyuu ")
        .setStature(175)
        .build();

In this way, a girlfriend was successfully created, and the readability of the code was quite high

If you are not satisfied with this girlfriend, you can customize the attributes. Because the author has limited level and can't find a girlfriend, you can only have new1 girFriend objects first


Related articles: