Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of singleton objects in Scala

I get the coding in that you basically provide an "object SomeClass" and a "class SomeClass" and the companion class is the class declaration and the object is a singleton. Of which you cannot create an instance. So... my question is mostly the purpose of a singleton object in this particular instance.

Is this basically just a way to provide class methods in Scala? Like + based methods in Objective-C?

I'm reading the Programming in Scala book and Chapter 4 just talked about singleton objects, but it doesn't get into a lot of detail on why this matters.

I realize I may be getting ahead of myself here and that it might be explained in greater detail later. If so, let me know. This book is reasonably good so far, but it has a lot of "in Java, you do this", but I have so little Java experience that I sort of miss a bit of the points I fear. I don't want this to be one of those situations.

I don't recall reading anywhere on the Programming in Scala website that Java was a prerequisite for reading this book...

like image 343
gks Avatar asked Jun 30 '11 21:06

gks


People also ask

What is the difference between singleton object and companion object?

A singleton object named the same as a class is called a companion object. Also a companion object must be defined inside the same source file as the class.

How can a singleton object be made executable in Scala?

Scala Singleton Object Singleton object is an object which is declared by using object keyword instead by class. No object is required to call methods declared inside singleton object. In scala, there is no static concept. So scala creates a singleton object to provide entry point for your program execution.

What is singleton class explain with example?

In Java, Singleton is a design pattern that ensures that a class can only have one object. To create a singleton class, a class must implement the following properties: Create a private constructor of the class to restrict object creation outside of the class.

How do you describe a singleton?

A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class to only one object.


2 Answers

Yes, companion singletons provide an equivalent to Java's (and C++'s, c#'s, etc.) static methods.

(indeed, companion object methods are exposed via "static forwarders" for the sake of Java interop)

However, singletons go a fair way beyond this.

  • A singleton can inherit methods from other classes/traits, which can't be done with statics.
  • A singleton can be passed as a parameter (perhaps via an inherited interface)
  • A singleton can exist within the scope of a surrounding class or method, just as Java can have inner classes
  • It's also worth noting that a singleton doesn't have to be a companion, it's perfectly valid to define a singleton without also defining a companion class.

Which helps make Scala a far more object-oriented language that Java (static methods don't belong to an object). Ironic, given that it's largely discussed in terms of its functional credentials.

like image 109
Kevin Wright Avatar answered Sep 28 '22 00:09

Kevin Wright


In many cases we need a singleton to stand for unique object in our software system. Think about the the solar system. We may have following classes

class Planet object Earth extends Planet object Sun extends Planet 

object is a simple way to create singleton, of course it is usually used to create class level method, as static method in java

like image 44
jilen Avatar answered Sep 28 '22 00:09

jilen