Friday, December 28, 2012

Without a fear of being public !

I am a software developer, who picked up java nearly 3 years back as my first language. During my hey days i was little uncomfortable with it. I struggled a lot in writing small code snippets.As first initial days passed , I started becoming more comfortable with it.My love and Respect for the language started growing.

Few months back, A friend of mine told me about a language that was taking all developers by storm - Scala. So when i picked Scala to learn in my leisure hours,I started noticing the faults in java as a language. I was very impressed with the way those things were handled in Scala. It has everything you would ask for as a language with the fact code in Scala compiles to byte code(which means it is capable of running on all those old java capable servers).

With this post I am going to take a core concept of java - encapsulation and will show you how it is handled in Scala.

lets understand this with a simple example. Say i have class with name Laptop. one simple representation of it in Scala can be like this :

class Laptop{
var color : Color = null
}

now when i need to create an object of it i can simply do this : 

var myLaptop = new Laptop

and when i need to set the color to Black i will do :

laptop.color = Color.BLACK

But wait a minute ! Am i not breaking the basic rule of encapsulation here ! because now i have realized that i can never have a laptop with color Red (No offence.. Its not my color) .But From the code it still looks like that i can have a red colored laptop since the color is a public variable by default in Scala ! damn !

But Scala has already taken care of it.I can still achieve that without changing the singnature of my public class.
Scala supports encapsulation, but its syntax is considerably less verbose than Java's.We can imagine it as if there were mutator and accessor methods automatically generated for the variable.
To achieve same thing in java ,I will have to change the signature of my public class to make the variable private and expose the setter and getter instead and there i can have my condition that laptop can not be red.

public class laptop {
    private Color color;
    public Color getColor() {
        return color;
    }
    public Color setColor(Color color) {
        if(color.equals(Color.RED)) { throw new IllegalAgrunmentException(" Not my color ! ");}
        this.color = color;
    }
}

But in Scala we can take advantage of the fact that variables are actually functions.Hence i can easily rewrite the code to this check without changing the signature on my class.

class laptop{
   private var myColor : Color = null
        
      def color = myColor
      def color_=(color.Color)={
         if(!color.equals(Color.RED)) {
             myColor=color
         } else {
             throw new IllegalArgumentException("Not my color !")
         }
      }
} 


"_=" is the key here which does the trick here.It lets you redefine the assignment operator for the color.

Try to imagine the big picture of it. In Scala we can keep our variables public without any fear.also we don't need those boilerplate getter and setters to take care of encapsulation. 

Happy Scala !













2 comments:

Unknown said...

The motivation for auto generating setters/getters has been picked up from Ruby. And as Scala has already admitted its strong influence from Ruby..

P.S - good post :), while i am concentrating on sharing resources you are posting nice explanations to them :D

Kulbhushan Singhal said...

Nice one Jet...