Posts

Showing posts with the label Oops

Case class-an Ultra POJO class in Scala

Image
POJO stands for Plain-Old Java Object, a class that will have set of fields and their getters and setters. Pojo class is primarily used as a data structure or data carrier across the layers in the J2EE applications. It is understood as more of a best practice than a language standard to define one using any special keyword like pojo.  Is there any equivalent of such class in Scala? yes there is. But not just for a data structure but with valiant features to cater in various usage.  Case comes with handy  Case class in Scala is used for the purpose of defining an object with immutable data. On the otherhand, when a class is defined as a Case class, a lot of boilerplate code is generated by the Scala compiler. The methods like apply, unapply, tupled, copy, set of accessors and mutators, and objects default methods like toString, hashCode, and equals are part of the generated boilerplate code. A question might pop-up in our mind, Isn’t a case class similar to j...

Singleton class made easy in Scala

Singleton Singleton is a notorious OOPs design pattern, which allows only one instance of the class to be created at any point of time. This is used primarily for heavy weight classes that are not supposed to be created for every instantiation request. Private constructor (Ingredient #1) To make the class singleton the key ingredient is Private Constructor in the class. Since private constructor is not accessible from outside of the class, we can restrict the creation of the objects.  Similar to Java, Scala allows us to make the primary constructor as private so that we can use method definition to perform controlled creation of objects.  Companion Object (Ingredient #2)  Companion object is analogous to utility class in Java. A utility class in Java will contain only static methods, thus makes easy to invoke methods without object. In Scala, a companion object will have the same name of the class in the same file and all the methods that are declared will be static....

Visbility of Class members in Scala

Image
Scala programming model controls the visibility of class constructor arguments. Based on the declaration of the fields as val, var or private, the compiler generates the accessors and mutators accordingly. The following matrix explains how the compiler generates the accessors and mutators of the class internally based on the declaration of the fields. Val Fields Assume if the class constructor is declared with Val fields, the value of the fields can be accessed but not changed. In general, the val declaration is meant for immutable fields. Thus Scala does not generate the mutator for the val fields.  class Person(val firstName: String, val lastName: String) { override def toString = s"$firstName $lastName" def printFullName{ println(this)} } object Test extends App{ var p = new Person("Murugan", "Shiva") p.printFullName p.firstName = "Ganesh" // Compiler error: reassignment to val p.printFullName } Var Fields The var fields...

Popular posts from this blog

Trait, an interface in Scala

Singleton class made easy in Scala

Quickstart with Angular 2/4