Design Pattern: Singleton and YOU

Design Pattern: Singleton and YOU

Design patterns in life and Ruby — gain an intuitive understanding of OO design patterns by linking them with real-life examples.

 

Related post: Singleton Pattern Deep Dive: Why is it bad?

 

This might be the most important post in my Design Pattern series because this one is about YOU.

Before anything, you need to listen to this song:

No, I’m serious.

The song is less than 2 minutes.

Listened to the song before you read any further.

Let’s take a look at the lyrics of the song:

Stand tall.

 

You’re in a class by yourself.

 

Be proud.

 

You’re not like anyone else.

 

No doubt about it. You’re second to none.

 

’Cause you are the one and only one.

 

Chin up.

 

’Cause you are one of a kind.

 

Chest out.

 

We know that we’ll never find anyone like you under the sun.

 

’Cause you are the one and only one.

 

If everybody were like everybody else, how boring it would be.

 

The things that make me different are the things that make me me.

 

You’re in a class by yourself, you’re one of a kind, and you’re the one and only one!

This uniqueness is exactly what the Singleton pattern is about!

Definition of the Singleton pattern:

The Singleton pattern:

 

- ensures a class has only one instance,

 

- and provides a global point of access to it.

 

The second part of the criteria is easy to fulfill — basically any class can provide a global point of access.

class You
end

But a simple class like this does not ensure that the class only has one instance:

We can easily create two different instances of you.

It’s because the new method, the method for creating instances for the class, is public.

 

To prevent having multiple instances of the class, we can try to mark the new method private, so no one outside of the class has access to the method 1.

class You
  private_class_method :new
end

But then we can't create an instance of the class at all!

Why don’t we create an instance from inside of the class and open a point of access to the outside world?

class You
  @@instance = You.new
  
  def self.instance
    return @@instance
  end
  
  private_class_method :new
end

(In Ruby, @@ indicates the variable is a class variable.)

Now there is a way to access the instance created within the class:

Both criteria of the Singleton pattern’s definition have been met:

- ensures a class has only one instance,

 

- and provides a global point of access to it.

We just wrote a simple example of the Singleton pattern!

 

There are many advantages of using the Singleton pattern. One of them is lazy initialization:

the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.

class LazyYou
  def self.instance
    @instance ||= new
  end
  
  private_class_method :new
end

In the above code, we don’t initialize the instance until the first time LazyYou.instance is called which is the first time the instance is needed.

 

Now you have a basic understanding of the Singleton pattern.

Next time when you try to ensure a class only has a single instance, you can consider this pattern.

And when you are down, remind yourself that you are the one and only one and the things that make you different are the things that make you you : )

 

Takeaways

  1. The Singleton pattern ensures a class has only one instance and provides a global point of access to it.
  2. Be proud. You’re not like anyone else.

 

Thanks for reading! I hope you enjoy the article.

Don’t forget to subscribe! Next time we will talk about …

Enjoyed the article?

My best content on Software Design, Rails, and Career in Dev. Delivered weekly.

Unsubscribe at anytime. I'll never spam you. Powered by ConvertKit

 


[1] Unless you really go out of your way and use You.send('new'). You can always call .send(method_name) to invoke a private method in Ruby, if you really want to go against the will of the author of the code.

2 Comments Design Pattern: Singleton and YOU

  1. Ashi

    Hi Sihui Huang
    I saw your singleton and factory method videos. You content, approch and pressentation is simply amazing.
    Keep writing and making videos.
    all the best….

Leave A Comment

Your email address will not be published. Required fields are marked *