Skip to content

Instantly share code, notes, and snippets.

@danielpetisme
Created April 28, 2013 10:44
Show Gist options
  • Save danielpetisme/5476546 to your computer and use it in GitHub Desktop.
Save danielpetisme/5476546 to your computer and use it in GitHub Desktop.
Design patterns are commons concepts for every devlopper. So I'm trying to implement the basic ones in Golo. The very first one is the Singoloton but I'm facing some issues. 1 - Is it possible to implement it in pure Golo? 2- Without DynamicObjects ? This is my shot. Any thoughts are welcomed!
module g0l0.patterns.creationnal.Singleton
local function _instance = -> null
local function _init = -> DynamicObject():
testing(|this| -> println(this + "doing some stuff"))
function MyObject = {
if _instance() is null {
let instance = _init()
^_instance = -> instance
}
return _instance()
}
function main = |args| {
var instance1 = MyObject()
var instance2 = MyObject()
assert (instance1 == instance2)
}
@jponge
Copy link

jponge commented Apr 28, 2013

We can't have class / value object definition yet, which means that it is coming at some point. It's a young language after all 😉

It's not a procedure-oriented language, but a kind of functional one, but not really, but duck-typed, and a language always has multiple paradigms, so...

@jponge
Copy link

jponge commented Apr 28, 2013

The samples/workers.golo is a good example of scoped objects and dependency injection. It took me a while to realize that scopes and DI are just partial applications on functions...

@danielpetisme
Copy link
Author

What about this

module g0l0.patterns.creationnal.Singleton


local function _init = -> DynamicObject():
  testing(|this| -> println(this + " doing some stuff"))

local function _MyObject = |this| -> this

function MySingletonFactory = -> ^_MyObject: bindTo(_init())

function main = |args| {

  let getInstance =  MySingletonFactory()

  let instance1 = getInstance()
  let instance2 = getInstance()

  require(instance1 == instance2, "Must be the same object")

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment