Check out the simplest Java Bean:
class Person { private String firstName; private String lastName; public String getFirstName() {return firstName;} public void setFirstName(String firstName) {this.firstName = firstName;} public String getLastName() {return lastName;} public void setLastName(String lastName) {this.lastName = lastName;} }
The following code creates an instance of Person an that initiates it:
Person president = new Person(); p.setFirstName("George"); p.setLastName("Bush");
As you can see:
- Initialization is really divided into 3 lines. This means that the object is in a constant state when all 3 lines are completed and in a consistent state before.
- The object is really changed: the values it calls can be changed by calling setter.
Why is that bad? Since our Person class is not thread safe, and therefore we cannot use it directly in a multi-threaded environment without thinking about synchronization.
Here is an example. A few years ago, Barack Obama became president of the United States. How can we express this in code?
p.setFirstName("Barak"); p.setLastName("Obama");
In muttti threaded envronent, the president object is in an improper state when setFristName() has already completed and setLastName() has not yet been called because the object contains "Barak Bush", which is obviously incorrect.
What's the solution? Let make `Person immutable:
class Person { private final String firstName; private final String lastName; Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() {return firstName;} public String getLastName() {return lastName;} }
As you can see, there is no way to change the first or last name stored in the object. Fields are final and have no setters. So our example looks like this:
The face of the president = the new face ("George", "Bush"); // elections ..... president = new person ("Barack", "Obama");
Since Person is immutable, we cannot reuse the old instance of Person and change its attribute. Instead, we must create a new instance. If president is volatitle , the link assignment is atomic, so the code is thread safe.
Update
The problem with designers is that they are not flexible. Our example has only 2 parameters. But think about the real world when the Person class has probably 20 or more fields. In this case, creating such an object is rather verbose.
In addition, some fields may be optional. In this case, you probably want to create several overloaded constructors with a different number of parameters. To avoid duplication of assignemnt code, the so-called telescopic constructors are usually used, i.e. A pattern when a constructor calls another constructor. This template is good, but in some too verbose and hard for modifications.
This means that there is no perfect solution. Each solution has its advantages and disadvantages.
By the way, a solution that combines the advantages of an immutable object and the flexibility of creating and initializing an object is a construction template.
I will not write all the examples that are necessary for a better understanding of these problems. But I hope my answer helped you a little. Now you have a starting point and you can find out this problem using other resources. Good luck.