The Singleton Design Pattern

The singleton pattern is the simplest of design patterns, so it seems a logical place to start,  it allows us to make sure that the instantiation of a class results in only one object.

This is useful when you only ever want one instance of an object, take for example a printer port (LPT not USB and yes I am that old), older computers came out with 1 LPT port on the motherboard.
So lets say you are writing a program that makes use of the port, and assuming that you can only ever access the port with one connection, you could use a singleton to ensure that the communication to the port is only ever handled through one object, regardless of how you instantiate it and what your names are.

Below is an example (in PHP 5) of a singleton pattern, this was taken from www.php.net.

PHP:
  1. class Example
  2. {
  3. // Hold an instance of the class
  4. private static $instance;
  5.  
  6. // A private constructor; prevents direct creation of object
  7. private function __construct()
  8. {
  9. echo 'I am constructed';
  10. }
  11.  
  12. // The singleton method
  13. public static function singleton()
  14. {
  15. if (!isset(self::$instance)) {
  16. $c = __CLASS__;
  17. self::$instance = new $c;
  18. }
  19.  
  20. return self::$instance;
  21. }
  22.  
  23. // Example method
  24. public function bark()
  25. {
  26. echo 'Woof!';
  27. }
  28.  
  29. // Prevent users to clone the instance
  30. public function __clone()
  31. {
  32. trigger_error('Clone is not allowed.', E_USER_ERROR);
  33. }
  34.  
  35. }

So as you can see from the 'singleton()' function, it first checks to see if an instance of itself exists, if there is no instance of itself, then it instantiates itself and returns the object. If it does find that an object already exists it merely returns that object.

Calling the singleton pattern from code would be done as follows, this is also from the php.net site;

PHP:
  1. // This would fail because the constructor is private
  2. $test = new Example;
  3.  
  4. // This will always retrieve a single instance of the class
  5. $test = Example::singleton();
  6. $test->bark();
  7.  
  8. // This will issue an E_USER_ERROR.
  9. $test_clone = clone $test;

So Example::singleton(); will always return a single instance of the class.

The most common use of singleton patterns seem to be for database classes, though it can be used whenever a resource (such as a database or port) may only be used once, or when many identical instances in different parts of the website represent the same thing (no matter where you reference a database object it still represents the database class).

Posted under Design Patterns

This post was written by Shaun on October 28, 2009

Tags: , ,

PHP Frameworks

mmmmm... So just written (and probably failed) an exam on systems analysis and development where brief mention was given to design patterns.

And a few months (almost a year) back I started working in a PHP framework that makes extensive use of design patterns, called Seagull. It opened my eyes to the possibilities of true Object Oriented Programming and helped me understand how design patterns can be effectively implemented.

One of my first attempts at a personal website using Seagull is a picture archive (www.picturearchive.co.za), here I used Seagull as a base to fast track a website where I could import images and display them in a chronological order, just don't concentrate on the looks, styling is NOT my strong point!!

Seeing how quick and easy that made things, I moved my 'events' website into Seagull as well Seagull has an events/calendar module that I merely 'tweaked'), all I really had to do was write the SQL scripts to pull the info in from the old calendar software I was running, easy peasy!

I have another exam coming up soon, but will hopefully try and dispense of some of the little pearls of wisdom that I have stumbled upon as I felt my way around a well written and stable framework.

I am also hoping to compare it with what I developed from scratch (A long time ago!) and show how the OOP way of doing things makes any module in Seagull much more extensible than my attempt at a framework.

Lastly I want to try and explain design patterns in as simple a manner as I can, hopefully by putting my thoughts out in writing I will gain a better understanding of how if all works, and can implement it more professionally, becoming a better developer in the process!!

Posted under PHP Web Development

SA Dealers Update

Apparently the Harley shop in Rivonia has gone under liquidation, I removed this but if you know for sure that the shop is still open let me know.

I was contacted a while back by someone, apparently good friends with the people from Bike Hospital, to add images etc. to their profile on the site. If this was you, I have tried to email you more than once, but have received no response.

So if you are a dealer, or are friends with a dealer and would like me to update their details please leave a comment via this blog, or, leave feedback on the site from their contact details.

If you want a dealer added you can comment on the site as well. If you can send through details and pics etc. I will add it all for you.

Posted under SA Dealers Updates

This post was written by Shaun on October 26, 2009

Tags: , ,