Home > front end >  Java coding style to be more concise
Java coding style to be more concise

Time:04-04

How can I compact these code in Java without creating other objects?

myObjectNumberOne.setTitle("title");
myObjectNumberOne.setBody("body");
myObjectNumberOne.setAutostart(true);
myObjectNumberOne.mySubObject1.setProp1("prop1");
myObjectNumberOne.mySubObject1.setProp2("prop2");
myObjectNumberOne.setFooter("footer");

with something like that other languages do

with (myObjectNumberOne) {
  setTitle("title");
  setBody("body");
  setAutostart(true);
  with (mySubObject1) {
    setProp1("prop1");
    setProp2("prop2");
  }
  setFooter("footer");    
}

Without modifying any classes or using any pattern (Builder or constructors). I mean something in order to write less code

CodePudding user response:

Make all the setters return the object itself. For example:

MyObject setTitle(String s){
    this.title = s;
    return this;
}

Here I assume that your class is called MyObject. Once this is done for all the other methods as well, do this instead:

myObjectNumberOne.setTitle("title")
.setBody("body")
.setAutostart(true)
.mySubObject1.setProp1("prop1")
.mySubObject1.setProp2("prop2")
.setFooter("footer");

Edit: This is called method chaining.

CodePudding user response:

Create an inner class in your class and call it Builder Here's a simple example:

public class Car {

    private  String name;
    private int minSpeed;
    private int maxSpeed;
    private int mileAge;
    private String owner;


    public String getName(){
        return name;
    }
    public String getOwner(){
        return owner;
    }


    public int getMaxSpeed(){
        return maxSpeed;
    }
    public int getMinSpeed(){
        return minSpeed;
    }
    public int getMileAge(){
        return mileAge;
    }

    public static CarBuilder create(){
        return new Car().new CarBuilder();
    }

    public class CarBuilder {
        public CarBuilder setName(String name){
            Car.this.name = name;
            return this;
        }

        public CarBuilder setOwner(String owner){
            Car.this.owner = owner;
            return this;
        }

        public CarBuilder setMileAge(int mileAge){
            Car.this.mileAge = mileAge;
            return this;
        }

        public Car build(){
            return Car.this;
        }


    }


}

Then you can use it:

Car car = Car.create()
.setName("Audi")
.setMileAge(200)
.setOwner("John")
.build();

CodePudding user response:

Perhaps you could create multiple constructors for mySubObject

 public MySubObject(String prop1){
     this.prop1 = prop1;
 }

 public MySubObject(String prop1, String prop2){
     this.prop1 = prop1;
     this.prop2 = prop2;
 }
 .
 .
 .

You could then replace

  with (mySubObject1) {
    setProp1("prop1");
    setProp2("prop2");
  }

With something like:

with (myObjectNumberOne) {
  setTitle("title");
  setBody("body");
  setAutostart(true);
  setMySubObject(new MySubObject("prop1", "prop2"));
  setFooter("footer");    
}

You could also checkout varargs: https://www.baeldung.com/java-varargs

  • Related