Scala | Stateful Object
Stateful Objects are that objects which have changeable members or mutable members, that may vary what transactions or operations were previously performed on the object. For a same operation performed many times, the result outputted may be different from previous result’s. It is very common to compare stateful objects with real world objects, where the state of the objects changes over-time. Syntax:
class classname { // declaring some states that are mutable var state1 var state2 def changestate { // some operation to change the states of the object } }
When the members of an objects changes its value overtime for certain operation, which are performed on the object. Due to this the statefulness of the object changes that depends on the previously performed operations. Below are some examples to understand stateful object. Examples #1:
Scala
// A Scala program to illustrate // stateful objects // creating a class class waterbottle { // creating states var water : Int = 0 def drinkwater = { if (water > 0 ) { water = water- 1 println("water left = "+water) } else { println("waterbottle empty fill water") } } // Defining method def fillwater (c : Int) = { if (water + c > 5 ) { water = 5 } else { water = water + c } } override def toString = "water in bottle = " + water } // Creating object object GFG { // Main method def main(args : Array[String]) { // waterbottle object var w = new waterbottle // calling w.fillwater( 3 ) println(w) // Changing state w.drinkwater w.drinkwater w.drinkwater w.drinkwater } } |
Output :
water in bottle = 3 water left = 2 water left = 1 water left = 0 waterbottle empty fill water
As we can see in the above example state for w.drinkwater changes its result for the same operation to “waterbottle empty fill water” till we fill or update water. The mutable state depends on water which is a variable. Thus we can say that stateful objects are made from vars and not val but this is not true, a class can change its state without containing any vars. For an object that changes its state to another state, the set of operation or the path taken to reach that particular state can be different, but at the end what state is achieved should be same. This is also known as operational equivalence where x & y are different objects but with the same states at the end of different set of operations. Examples #2:
Scala
// A Scala program to illustrate // stateful objects // creating player class class player { // creating states var health : Int = 10 def punch(p : player) { if (p.health > 0 ) { p.health = p.health- 2 println(p + " health is " + p.health) if (p.health < 1 ) { // checking the state dead(p) } } else { dead(p) } } def kick(p : player) { if (p.health > 0 ) { p.health = p.health- 3 println(p + " health is " + p.health) if (p.health < 1 ) { // checking the state dead(p) } } else { dead(p) } } def superp(p : player) { if (p.health > 0 ) { p.health = p.health - 5 println(p + " health is " + p.health) if (p.health < 1 ) { // checking the state dead(p) } } else { dead(p) } } def dead(p : player) { println("Game Over") println(p +" is dead " + this + " is winner") } } // Creating object object GFG { // Main method def main(args : Array[String]) { // Creating objects for player var p 1 = new player var p 2 = new player p 1 .kick(p 2 ) p 1 .punch(p 2 ) p 1 .superp(p 2 ) p 1 .punch(p 2 ) } } |
Output :
player@506e1b77 health is 7 player@506e1b77 health is 5 player@506e1b77 health is 0 Game Over player@506e1b77 is dead player@4fca772d is winner Game Over player@506e1b77 is dead player@4fca772d is winner
Similarly in the above example we can tell that player is a stateful object without looking at the internal working of the class. Because the health cannot be reduced to negative so player has mutable states as the same operation returns different output at different or same inputs.
Please Login to comment...