A simple example of implementing chained operations (method chains) in JAVA

  • 2020-04-01 03:15:46
  • OfStack

When using jQuery, we often see or use method chains, such as:


$("#p1").css("color","red").slideUp(2000).slideDown(2000);

This code means that the selector selects the HTML tag with id p1, turns it red, and then slideup, slidedown.

Naturally, you can write these methods separately, but if you don't, not only will it be easier to read, but the amount of code will also be reduced. So why not?
This method of calling a function is called Chaining in jQuery, and the principle is simple: the method that can Chaining returns the object itself.

Here's a demonstration with Java code:

Don't use chaining:

Persion. Java:


public class Persion {
    private int id;
    private String name;
    private String phoneNumber;
    private String address;
    public  Persion() {
    }
    public void setId(int id) { 
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public void printId() {
        System.out.println(this.id);
    }
    public void printName() {
        System.out.println(this.name);
    }
    public void printPhoneNumber() {
        System.out.println(this.phoneNumber);
    }
    public void printAddress() {
        System.out.println(this.address);
    }
}

Test. Java:

public class Test {
    public static void main(String[] args) {
        Persion persion1 = new Persion();
        persion1.setId(3);
        persion1.setName("John");
        persion1.setPhoneNumber("1111111");
        persion1.setAddress("US");
        persion1.printId();
        persion1.printName(); 
        persion1.printPhoneNumber();
        persion1.printAddress();
    }
}

Using chaining:

Persion. Java:


public class Persion {
    private int id;
    private String name;
    private String phoneNumber;
    private String address;
    public  Persion() {
    }
    public Persion setId(int id) { 
        this.id = id;
        return this;
    }
    public Persion setName(String name) {
        this.name = name;
        return this;
    }
    public Persion setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
        return this;
    }
    public Persion setAddress(String address) {
        this.address = address;
        return this;
    }
    public Persion printId() {
        System.out.println(this.id);
        return this;
    }
    public Persion printName() {
        System.out.println(this.name);
        return this;
    }
    public Persion printPhoneNumber() {
        System.out.println(this.phoneNumber);
        return this;
    }
    public Persion printAddress() {
        System.out.println(this.address);
        return this;
    }
}

Test. Java:

public class Test {
    public static void main(String[] args) {
        Persion persion1 = new Persion();
        persion1.setId(3).setName("John")
                .setPhoneNumber("1111111").setAddress("US");
        persion1.printId()
                .printName()
                .printPhoneNumber()
                .printAddress();
    }
}


What a weird feeling ~ ha ha!


Related articles: