Crisp Reading Notes on Latest Technology Trends and Basics

Archive for April, 2016

Scala says “Singletons Instead of Static”

Singletons

Introduction

Java uses the static methods and blocks as a primary primitive.
Singleton is built as a common design pattern on top of static methods.

Scala on the other hand builds the singleton as a standard pattern.
Static methods are implemented as methods within a singleton class

Objects versus Classes

Scala uses two keywords –  object and class to distinguish between singletons and non-singletons.

  • Defining an object defines a class and a singleton object with the same name
object Account {
  var count = 0

  def numAccounts(): Unit = {
    return count
  }
}
  • From the corresponding Account class, the count is added whenever an account is created
class Account(ssn:Int, balance:Float) {
  Account.count++;
  ...
}

Companion Classes

  • In the above example, the use of the same name for the object and the class is deliberate
  • These are called companion classes. They are present in the same file, and may access each others private methods

Singleton Objects in Other Domains

  • Singleton objects are used heavily used in CMIP, which is a OSI standard for sophisticated modeling
  • Singleton objects are also heavily used in Dependency-Injection Frameworks, such as Spring.
Advertisements

Scala says “Bye-Bye Java Beans”

Scala avoids JavaBeans code

One of the most powerful features of Scala is that it removes the highly repetitive nature of writing Java Beans (something that occupies major space in programs and books)

Constructors and Accessors – Motivating Example

The Scala way

To code a class BankAccount with

  • A immutable string – name
  • A mutable int – balance
class Account( val name: String, var balance: int )
acc = new Account(“scott”, 25)
println(acc.name)
println(acc.balance)

The Java way

The equivalent code in Java would have

  • A constructor
  • A getter only for the immutable entity
  • A getter and setter for the mutable entities
public class Account {
  private String name;
  private int balance;

  public Account(String name, int balance) {
    this.name = name;
    this.balance = balance;
  }

  public String getName() {
    return this.name;
  }

  public int getBalance() {
    return this.balance;
  }

  public void setBalance(int balance) {
    this.balance = balance;
  }
}

IDEs do this work – Why bother

IDEs still produce code that you need to maintain. It could be distracting.

Also, if there is a way to modify the generated code, there will be a temptation to modify it. After that, change management becomes hard.

The meaning of the words on the constructors

  • If a constructor argument is specified as val, then only a getter is generated for it
  • If a constructor argument is specified as var, then both a getter and setter are generated for it
  • If a constructor argument is specified as neither, then, the variable is not part of the class. The argument is used in the constructor and discarded.

Multiple and Custom Constructors

Like Java, Scala allows multiple and custom constructors. It also improves by having only one primary constructor and having secondary constructors go through the same

Multiple Constructors

Like Java, scala allows multiple constructors. It calls one constructor as primary, and the other constructors as secondary

If the bank accounts were opened with zero balance, the most common case, a new constructor could be used

class Account( val name: String, var balance: int ) {
  def this( val name: String ) = {
    this( name, 0 );
  }
}

Custom Code in Primary Constructors

Primary constructors also have custom code in them

Suppose our constructor took a SSN, and did a lookup for the name

class Account(val ssn: int, var balance int) {
  val name = ssn_lookup(ssn)

  def ssn_lookup(val ssn: int): String {
    // To be implemented
  }
}

Tag Cloud