The Singleton Design Patterns

In the world of software engineering singleton pattern is a design pattern that restricts the instantiation of a class to one object. To make it simpler Singleton Pattern ensures that there can be once a single instance to a class. For many WordPress developers in the world these patterns are a complete no-no and they term it as “anti-pattern”. Some claim it’s an overused pattern however overuse doesn’t make it wrong. The only thing that the developers need to keep in mind is to use these patterns very judicious. For instance if you are building a helper class or library with your theme this isn’t a great concept; but in case of plugin development it can be very useful.

You Have Used It

No matter what the arguments against Singleton Patterns are most developers have made extensive use of it. Each time you’ve used the global keyword in WordPress for framework-wide constructs such as $wpdb and $wp_rewrite you have made use of Singleton Patters. You can argue that these objects don’t implement Singleton design pattern in the true sense but the counter argument is that the intent and purpose of your use meets the definition. The objects that we have mentioned above are instantiated only once during WordPress request-response cycle’s lifetime. Apart from this you would never call wpdb() in your themes and plugins; what you do instead is make a reference to the global $wpdb object that WordPress set up for you. What kind of design pattern would you rate that as if not Singleton Pattern?

Secondly you would never want more than one connection to your database at any given point of time as this is likely to cause memory bloat. Only one instance of database connection is active at any given point of time. This is cost effective as it will relieve you from instantiating multiple copies of objects. This is another instance where you have made use of Singleton Design Patterns.

How It Impacts WordPress Development?

As a WordPress developer you would know that there are only few ways to instantiate plugins. Let us take a look at these ways and means.

  • One can instantiate a plugin at the bottom of the plugin file, however this often leads to an orphaned object.
  • You can also stick the plugin in the PHP $GLOBALS collection. But this puts you in a risky zone as you could trash something that already exists or makes the collection needlessly larger.
  • If you instantiate with each page load then the data is not persisted unless you put it into a serial order and hammer the database every single time.

All of these are good strategies but not the best. It is wiser for a developer to look for a strategy that doesn’t merely work but adds to the robustness of the project. This is where Singleton Design Patterns come into play in WordPress development.

How To Implement It?

By now you have understood the basics of Singleton Design Patters in WordPress and might be wondering how to implement in your project. In our example below we have declared a static object variable and also declared private constructor to avoid new object creation. Apart from this we have also declare a public property that can be accessed outside of the class and instantiated an object and assigned it to the private variable in getter.

public class Singleton
{
// Private static object.
private static Singleton instance;
// Private constructor prevent object creation.
private Singleton()
{
}
// A public property to access outside of the class to create an object.
public static Singleton Instance
{
get
{
if
(instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}

Advantages of the Singleton Pattern

  • In WordPress development normal class object can be instantiated ‘n’ number of times in a program and has its memory allocated to the instance. To free up the memory normal classes can be destroyed. Whereas a static class provides a global point of access. Singleton design patters combines the best of both.
  • Now, Singleton takes advantage of both the benefits defined above. Use of Singleton Pattern would prevent other objects or clients from duplicating instances of the class. This ensures that only a single copy of the data maintained at any given time. All access to the object is done so by the single instance and this ensures faster and better experience to the users.

To sum up Singleton Pattern is by far the simplest design pattern there exits in WordPress development. It is extremely easy to implement, cost effective and offers you great functionality when implemented correctly. It is ideally suited for web applications where you can offer great experience to the users.

03 Mar 2014

How to Setup and Optimize Image-based WordPress Blog

WordPress is generally considered as a blogging platform. But besides blogging, you…

23 May 2013

Speaking Heads in Website Design

Have you ever seen anything more interesting than a human face? Loving…

Written by

Allison Reed

Allison is a professional SEO specialist and an inspired author. Marketing manager by day and a writer by night, she is creating many articles on business, marketing, design, and web development. Follow her on LinkedIn and Facebook.

Post Comment

Your email address will not be published. Required fields are marked *

Managed by Quantum VXenon