Posts

Showing posts from 2017

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...

Trait, an interface in Scala

Trait is the new Interface Scala comes with a new keyword for interface called Trait . An Interface in Java helps us to declare methods and subsequently get implemented in classes. Similarly, Scala provides the same feature through Traits. The syntax for declaring the traits goes with a keyword Trait before the name of the interface. The following snippet shows the example of Trait BaseDoor and the implementation of the same in a class. Here, implementation is done with extends keyword trait BaseDoor{ def open def close def lock def unlock } class FrontDoor extends BaseDoor Strength of Traits over Interface a) Method implementation in Interface Traits comes with some more interesting features over Java interfaces. It’s not only allowing to declare methods but just like abstract class it also to allows implementation of methods. In Java the interfaces do not have method implementation. Only abstract class is allowed to have methods implemented, but Scala allows Trait...

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...

Quickstart with Angular 2/4

Node platform Developing web application using Angular 2/4 goes easy with Node.js platform. It does not mean that Angular based web apps can be build using Node.js, we can build with any other program management tools like Maven or Gradle. However, Node.js provides easy way of building Angular 2 application.  First this we need to do is to make sure Node is installed in our system. The following urls will take you to the download page of Node.js for appropriate OS. Windows 64 :  https://nodejs.org/dist/v6.11.0/node-v6.11.0-x64.msi OS X :  https://nodejs.org/dist/v6.11.0/node-v6.11.0-darwin-x64.tar.gz Installing Angular CLI package Angular CLI is a command line interface for developing Angular 2 projects. The command line tool will help to create project and its components from few commands. Let us see how to install the Angular CLI into Windows system.   $ node –-version $ v6.9.2 $ npm install –g @angular/cli@latest ...

Connecting Virtual Box Guest via SSH client

Image
Overview Configuring Network settings for Virtual Box guest instance is one of the primary steps on setting up the VM instance. This allows users to connect to guest OS from SSH clients and ease to perform the desired operations on the OS.  Steps Open Oracle VM VirtualBox manager window Goto  File -> Preferences  for changing the configuration settings Choose  Network  option from the left menu panel     Select DHCP server tab for configuring server IPs   Click OK. Now on the VM instance Right-Click->Settings to modify the Network settings for Guest instance.   Choose Network option   There are Adapters options to configure different types of network adapters.   On Adapter1, select Host-only Adapter option for Attached to field. The name should be VirtualBox Host-Only Ethernet Adapter   Make sure the Enable Network Adapter is checked. Connecting guest from SSH client N...

Popular posts from this blog

Trait, an interface in Scala

Singleton class made easy in Scala

Quickstart with Angular 2/4