Tag Archives: singleton

jQuery Communicator Singleton (Manage & Cache ajax requests)

Written by Garry Lachman (Admin). Filed under JavaScript, jQuery. Tagged , , , , , , , , , , , , , . .

Hey,
I wrote this class for cache ajax request if the same request sent twice.
The second reason why i use this class is a like there is only one exit point
from the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var Communicator =(function(){
    var instantiated;
    var retry = new Array();
    var cached_data = new Array();

    function init (){
        return {
            dispatch:function(url, callback){
                var self = this;
                if (Communicator.getInstance().cached_data[url])    {
                    callback(Communicator.getInstance().cached_data[url]);
                    return;
                }
                if (!retry[url])    {
                    retry[url] = 0;
                }
                var _rq = $.ajax({
                    url: url,
                    timeout: 30000,
                    success: function(_data){
                        Communicator.getInstance().cached_data[url] = _data;
                        callback(_data);
                    },
                    error: function(){
                        if (retry[url] < 3) {
                            retry[url]++;
                            dispatch(url, callback, retry[url]);
                        }   else    {
                            callback("");  
                        }
                    }
                });
            },
            cached_data: {}
        }
    }

    return {
        getInstance :function(){
            if (!instantiated){
                instantiated = init();
            }
            return instantiated;
        }
    }
})()

Using the class

1
2
3
4
5
6
7
8
9
10
11
12
Communicator.getInstance().dispatch(url,function(data){
    var error = false;
    try {
            data = eval("("+data+")");
    } catch(err)    {
        error = true;
    }

    if (!error)    {
        console.log(data);
    }                    
});

Have fun :)
Garry

Share

Doru Moisa’s Blog: Static call versus Singleton call in PHP

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , . .

New blog post by Doru Moisa about Static call vs. Singleton… very recommended!!!

“In the past several months I’ve been working with a rather large application built with symfony. I noticed that symfony makes heavy use of the Singleton pattern (other frameworks, like Zend do that too)…”

http://moisadoru.wordpress.com/2010/03/02/static-call-versus-singleton-call-in-php/

Enjoy ;)
Garry Lachman

Share

Singleton Pattern in PHP5

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , , , . .

Singleton is very basic design pattern.
If the instance is inited once than the class return the same instance.

Example of signleton in php5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?
Class SingletonExample {
  static private $instance;

  private function __construct() {
  }

  static function getInstance() {
    if(!Self::$instance) {
      Self::$instance = new SingletonExample();
    } else {
      return Self::$instance
    }
  }

  public function doHello() {
     return "Hello World";
   }
?>

Usage Example:

1
2
3
4
5
6
<?
include_once("singleton.class.php");

$single = SingletonExample::getInstance();
echo $single->doHello(); //return "Hello World"
?>
Share

Example of useing Custom Screen Manager in ActionScript 3

Written by Garry Lachman (Admin). Filed under ActionScript 3. Tagged , , , , , , , , , , . .

Screen Manager.
When i write window based systems i always use a Screen Manager.
when i can control and manage and display object on my screen.

Every display object need to extends ScreenContent class, then
he can attached to the screen manager.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package {
import flash.display.Sprite;
// screen content Abstract

public class ScreenContentAbstract extends Sprite {

private var _contentContainer:Sprite;
public function ScreenContentAbstract():void
{

}

public function hideContent():void  {
// hide content
}

public function showContent():void  {
// show content
}

public function set content(_s:Sprite):void {   _contentContainer = s;      }
public function get content():Sprite        {   return _contentContainer;   }

}
}

Screen Manager class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class ScreenManager extends Event
{
private var _contentContainer:Sprite;

public function ScreenManager(_f:Enforcer) {
_contentContainer = new Sprite();
}

// Add content to display
public function addContent(_content:ScreenContentAbstract):void {
_contentContainer.addChild(_content);
_content.addEventListener(MouseEvent.MOUSE_DOWN, contentMouseHandler);
}

// Remove content from display
public function removeContent(_content:ScreenContentAbstract):void  {
if (_contentContainer.contains(_content))
_contentContainer.removeChild(_content);
}

// Hide all content on display
public function hideAllContent():void   {
for (var i:int = 0; i &lt; _contentContainer.numChildren; i++) {
if (_contentContainer.getChildAt(i) is ScreenContentAbstract) {
(_contentContainer.getChildAt(i) as ScreenContentAbstract).hideContent();
}
}
}

// Show all content on display
public function hideAllContent():void   {
for (var i:int = 0; i &lt; _contentContainer.numChildren; i++) {
if (_contentContainer.getChildAt(i) is ScreenContentAbstract) {
(_contentContainer.getChildAt(i) as ScreenContentAbstract).showContent();
}
}
}

// Set focus on object on the display
public function setFocus(_content:ScreenContentAbstract):void   {
_contentContainer.setChildIndex(_content, _contentContainer.numChildren - 1);
}

// mouse handler for content
private function _contentMouseHandler(_e:MouseEvent):void   {
switch(_e.type) {
case MouseEvent.MOUSE_DOWN:
setFocus(_e.target as ScreenContentAbstract);
break;
}
}

// SINGLETON PATTERN
private static var instance:ScreenManager;
public static function getInstance():ScreenManager  {
if (instance == null)
instance = new ScreenManager(new Enforcer());
return instance;
}

}

}
class Enforcer{public function Enforcer()}

As you can see, you can add more functionality this is example only.

Thanks,
Garry Lachman

Share

Singleton pattern in ActionScript 3

Written by Garry Lachman (Admin). Filed under ActionScript 3. Tagged , , , , , , , , . .

Singleton pattern in ActionScript 3

the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the system.

Useing of singleton in AS3 code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package
{

public class as3Singleton
{

public function as3Singleton(_e:Enforcer)
{

}

/**
*   singleton static instance holder
*/

private static var instance:as3Singleton;

/**
*
* @return as3Singleton Instance (Signleton)
*
*/

public static function getInstance():as3Singleton
{
if (instance == null)
instance = new as3Singleton(new Enforcer(), _stage);

return instance;
}
}

}
class Enforcer{public function Enforcer(){}}
Share