I am reading , Orielly Design patterns and there is a line that "We can change the Behavior of
any object at runtime
" & how getters and setters are used to changed the Behavior of object at runtime .
The behavior is how the object works. Run time is the application life. So that statement means that during the run of program we are able to manipulate what object can do.
To simulate that please see following example:
public class MyObjectTest {
public static final void main(String[] args) {
MyObject myObject = new MyObject();
String theMessage = "No message yet.";
theMessage = myObject.readSecretMessage();
myObject.setIsSafe(true);
theMessage = myObject.readSecretMessage();
}
}
public class MyObject {
private boolean isSafe = false;
public boolean isSafe() {
return this.isSafe;
}
public boolean setIsSafe(boolean isSafe) {
return this.isSafe = isSafe;
}
public String readSecretMessage() {
if(isSafe()) {
return "We are safe, so we can share the secret";
}
return "They is no secret message here.";
}
}
Analysis:
The program will return two different messages, the decision depend on the field isSafe
. That can be modified during object life (object life start with operator new
) in run time.
And that what it means, that we can change the behavior of object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With