Wednesday, 5 February 2020

Stateless, Stateful Java Objects


These days people are talking more stateless and stateful objects.

State of the Object:
            Always represents it's instance variables.

Class Calculator {

      public int add(int a, int b) {
      }
}

Class Person {
        private String name;
       
         public String getName() {
              return this.name;
         }
        
          public void setName(String name) {
              this.name = name;
          }
}

Calculator calc = new Calculator();
    calc object having any instance variables , answer will be no. The object doesn't have any instance varialbe  we can call that object as stateless object.

Person ram = new Person();
         ram object having instance varialbe. But I am not update the instance varialbe, means not initializing the state. 
ram is statlesss object, 
ram.setName("Ramesh");
Now ram object initiliazing the instance variable with "Ramesh", then ram object having state.
now ram object became stateful object.

In design pattern state means object internal instance variable.



No comments:

Post a Comment