Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Singleton Pattern (syntax)

Tags:

singleton

f#

I have a cache of data which is getting refreshed from an outside source, and I want to limit my access tot his cache (readonly) inside of my app. I don't want to have refresh the datasource everytime I need access to it (ie. on instantiation go and pull all the data I need, as there is quite a bit of data that is being kept up to date).

type MySingleton = 

        [<DefaultValue>]
        static val mutable private instance: MySingleton

        static member GetInstance() = 
            instance

I guess this is one of the gotchas about implementing a project and trying to learn the language at the same time. I know the logic needs to be

if instance is null
    synchronize
    if instance is null
        instance = new MySingleton()

but the lack of null is throwing me for a loop. I think I can use an option type etc but it is throwing me for a loop

type MySingleton = 

        [<DefaultValue>]
        static val mutable private instance: MySingleton option

        static member GetInstance() = 
            match instance with
                 | Some(i) -> i
                 | None -> 
                            *MySingleton.instance = new MySingleton()
                            MySingleton.instance*

that logic is wrong according to the compiler...

       if Helper.notExists MySingleton.instance then
            MySingleton.instance <- Some(new MySingleton())        
       MySingleton.instance 

should I be using IF statements instead? Is there a prefered pattern for this syntax in f#?

like image 346
akaphenom Avatar asked Apr 22 '10 14:04

akaphenom


People also ask

What is the best way to implement Singleton design pattern?

To implement a Singleton pattern, we have different approaches but all of them have the following common concepts. Private constructor to restrict instantiation of the class from other classes. Private static variable of the same class that is the only instance of the class.

What is singleton class in java syntax?

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 is singleton pattern implemented java?

The most popular approach is to implement a Singleton by creating a regular class and making sure it has: A private constructor. A static field containing its only instance. A static factory method for obtaining the instance.

Which is a key implementation point for a singleton pattern?

Implementations. Implementations of the singleton pattern must: Ensure that only one instance of the singleton class ever exists; and. Provide global access to that instance.


1 Answers

Both .NET 4.0 and F# have Lazy, so I think you want

module MySingleton =
    let private x = Lazy.Create(fun() -> 42)
    let GetInstance() = x.Value

(where 42 might be a new WhateverType() or whatever the expensive initialization is).

http://msdn.microsoft.com/en-us/library/dd997286.aspx

(Commentary: It's 2010, and getting rare to have to explicitly deal with synchronization primitives; languages and libraries are encapsulating all the common patterns.)

like image 52
Brian Avatar answered Oct 03 '22 19:10

Brian