-
-
Save naviat/5aa38daecd620d5735b7d3996b2357e6 to your computer and use it in GitHub Desktop.
Revisions
-
egonelbre revised this gist
Oct 25, 2011 . 1 changed file with 12 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ /////// // Warning, code untested /////// type Thinger interface { SomeMethod( arg bool ) bool @@ -21,6 +22,7 @@ func (r *UnrealThing) SomeMethod( arg bool ) bool { r.state = r.state == arg } /////// // Abstract Factory ( Builder can be built using similar ideas ) type ThingerFactory interface { @@ -35,6 +37,13 @@ func ( tf *RealThingFactory ) CreateThing() Thinger { return &RealThing{ tf.initial } } // this can possibly also be replaced by a function // type ThingerFactory func() *Thinger tf := func() *Thinger{ return &RealThing{ true } } thing := tf() /////// // Pool type ThingPool struct { @@ -62,7 +71,8 @@ func (tp *ThingPool) Put( t *Thinger) { tp.unused <- t } /////// // Lazy Initialization // Can be used for multiton by not providing access // to the actual struct being created -
egonelbre created this gist
Oct 25, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,95 @@ // Warning, code untested type Thinger interface { SomeMethod( arg bool ) bool } type RealThing struct { state bool } func (r *RealThing) SomeMethod( arg bool ) bool { r.state = r.state ^ arg } type UnrealThing struct { state bool } func (r *UnrealThing) SomeMethod( arg bool ) bool { r.state = r.state == arg } // Abstract Factory ( Builder can be built using similar ideas ) type ThingerFactory interface { CreateThing() Thinger } type RealThingFactory struct { initial bool } func ( tf *RealThingFactory ) CreateThing() Thinger { return &RealThing{ tf.initial } } // Pool type ThingPool struct { factory *ThingerFactory unused chan *Thinger } func NewThingPool() *ThingPool { tp := &ThingPool{} tp.factory = &RealThingFactory{false} tp.unused = make( chan Thinger ) return tp } func (tp *ThingPool) Get() *Thinger { select { case t := <- tp.thing: return t; default: return tp.CreateThing() } } func (tp *ThingPool) Put( t *Thinger) { tp.unused <- t } // Lazy Init // Can be used for multiton by not providing access // to the actual struct being created type Things struct { factory *ThingerFactory things map[string] *Thinger lock chan int // there is probably some better way of doing this } func NewThings() *Things { th := &Things{} th.lock = make( chan int, 1 ) th.factory = &RealThingFactory{false} th.things = make( map[string] *Thinger ) th.lock <- 1 return th } func ( th *Things ) Get ( name string ) *Thinger { <- th.lock defer func(){ th.lock <- 1 } if t, err := th.things[name]; err == nil { return t } th := t.factory.CreateThing() th.things[name] = th return th }