Using the singleton design pattern
Using the singleton design pattern
Free Online Articles Directory
Why Submit Articles?
Top Authors
Top Articles
FAQ
ABAnswers
0 && $.browser.msie ) {
var ie_version = parseInt($.browser.version);
if(ie_version Login
Register
Hello
My Home
Sign Out
Email
Password
Remember me?
Lost Password?
Home Page > Computers > Programming > Using the singleton design pattern
Using the singleton design pattern
Posted: May 22, 2009 |Comments: 0
| Views: 376 |
]]>
What is the Singleton Design Pattern?
A group of authors now affectionately known as the ‘Gang of Four’ (Erich Gamma, Richard Helm, Ralph Johnson, and John Vilssides) wrote a book called ‘Design Patterns: Elements of Reusable Object-Oriented Software’. In this book they defined three categories of design patterns which are:
Creational Patterns Structural Patterns Behavioural Patterns
The Singleton Design pattern falls into the category of Creational Design Patterns, the Singleton Design Pattern ensures your software has only one instance of the class and provides a global access to it. In other words the Singleton design pattern is all about making sure that you can instantiate only one object of a particular class. If you don’t use a pattern like this one, the new operator just keeps on creating more and more objects.
Could I not just use a Static Class
Technically you could just use a static class as then there would only be one shared instance of a class for. However, if you did that then you would miss out on some of the benefits of the singleton design pattern. These benefits that singletons have over static classes include:
Singletons can implement interfaces and inherit from other classes A singletons can be lazy loaded. Only when it is actually needed. That’s very handy if the initialisation includes expensive resource loading or database connections Singeltons offer an actual object Singletons can be extended into a software factory. The object management behind the scenes is abstract so it’s better maintainable and results in better code Static classes are instantiated at runtime. This could be time consuming. Singletons can be instantiated only when needed So how do you use the Singleton Design Pattern?
]]>
In object oriented programming and software development you generally create an instance of an object by using a new operator, for example:
C#: MyObject myNewInstance = new MyObject()
VB.NET: Dim myNewInstance As New MyObject()
Each time a new object the computer allocates a heap of memory, creating lots of these object will increase the amount of memory is used and potentially effect the performance of your software product.
So how do we make it so that we only ever have one instance of our class, well it is a very simple 2 stage process:
Create the objects contructor as PRIVATE , that way no code outside of the class can creat an instance of the object. Create a static/shared method within the class that will return a newly created instance of your class, or if an instance already exists then it will return the current instance. Singleton Design Pattern Example
C#:
public class MyObject
{
//create a private static variale to store our instance
private static MyObject singletonObject;
//create a private constructor
private MyObject()
{
//initial
}
//create a publi static method to return the instance
public static MyObject CreateInstance()
{
//check if we already have an instance, if so return it, else create a new
//instance
if (singletonObject == null)
singletonObject = new MyObject();
return singletonObject;
}
}
VB.NET :
Public Class MyObject
‘create a private static variale to store our instance
Private Shared singletonObject As MyObject
‘create a private constructor
Private Sub New()
‘initialise the object
End Sub
‘create a public static method to return the instance
Public Shared Function CreateInstance() As MyObject
‘check if we already have an instance, if so return it, else create a new
‘instance
If singletonObject Is Nothing Then
singletonObject = new MyObject()
End If
Return singletonObject
End Function
End Class
Notice how in the implementation of the singleton design pattern we declare a private static variable to store the instance of our class. When we call the function we have created to create our instance a check is performed to see if an instance exists in that variable, if not one is created and returned.
Watch your traffic increase just by submitting articles with us, click here to get started.
Liked this article? Click here to publish it on your website or blog, it’s free and easy!
Real IT Solutions -
About the Author:
About the Author: This article was provided by Real IT Solutions, a Derbyshire based Derby Web Design, Software Development and Internet Marketing company.
]]>
Questions and Answers
Ask our experts your Programming related questions here…
200 Characters left
What are the design patterns used in spring ?
What are the design patterns in .net ?
How many types of design patterns in java ?
Rate this Article
vote(s)
9 vote(s)
0) {
ch_selected = Math.floor(Math.random()*ch_queries.length);
if(ch_selected == ch_queries.length) ch_selected–;
ch_query = ch_queries[ch_selected];
}
}catch(e){
ch_query = document.title;
}
]]>
Source: http://www.articlesbase.com/programming-articles/using-the-singleton-design-pattern-931313.html
Article Tags:
singleton design pattern, singleton software development, software, design patterns, gang of four, creational design pattern, derby software development, derby web design, derby internet marketing
When it comes to giving gifts, not everything fits neatly into a box. (03:34)
When it comes to wrapping presents, it’s not just about picking the paper. (02:16)
Learn Taped Patterns and Designs with Paint
Learn how to paint vertical stripes on a wall. Vertical stripes are one of the most versatile wall finishes. In this video you will learn the steps needed to create vertical lines on your walls. Proper measuring, taping and paint techniques are covered. For more information, visit http://www.homedepot.com/knowhow (03:55)
How to Design Spa Baths – Classic Elegance
When you have a classic spa bath, your space will never go out of style. (02:14)
Learn About Victorian Interiors
Victorian interiors focus on the elaborate, which was more than apparent when Meghan Carter, host of Ask the Decorator, toured three Victorian homes in Wheeling, WV. The interiors of the Victorian homes featured intricate patterns, detailed fretwork and stunning stained and art glass. Discover the main characteristics of Victorian interiors as Meghan takes you to Wheeling, WV to see them first-hand. (04:07)
Build Great Application For iPhone Using COCOS2D
Outsource iPhone development teams a can now build great apps for all models of the iPhone. This software can now facilitate iPhone application development by simplifying the creation.
By:
dharmeshtalaviyal
Computers>
Programmingl
Oct 01, 2011
Important Branding Tips For Sharepoint 2010
The knowledge framework for sustaining the SharePoint 2010 implementations. It is therefore essential to know how SharePoint 2010 operates when creating various branding tips. Companies can consult about SharePoint 2010 implementation so as to satisfy the needs for maintaining proper documentation. It was only possible to develop a gallery layout with the folder list that is always visible on the left side. With the newly invented SharePoint development.
By:
dharmeshtalaviyal
Computers>
Programmingl
Sep 30, 2011
Is HTML5 Development proving to be a risk for Flash and Silverlight?
HTML5 is a groundbreaking upgrade to how the web was being presented and can serve to be an effective game-changer to the era of web application development. So where do RIA technologies like Adobe Flash, Microsoft Silverlight, etc. stand?
By:
Elan Technologiesl
Computers>
Programmingl
Sep 29, 2011
The Benefits of Ecommerce Design Kent
A good business website should be attractive and user friendly for the viewer/customer, but should also be easy for the business owner. Building a great website shouldn’t be left to chance.
By:
Grahaml
Computers>
Programmingl
Sep 28, 2011
Making Use of the Skills and Expertise of your Programmers
Of course, other programming language can be used but you should be able to know which one will work best for your web application development; this will not only save you time but also resource and effort.
By:
Jimmy Hiltonl
Computers>
Programmingl
Sep 28, 2011
Add new Comment
Your Name: *
Your Email:
Comment Body: *
Verification code:*
* Required fields
Submit
Your Articles Here
It’s Free and easy
Sign Up Today
Author Navigation
My Home
Publish Article
View/Edit Articles
View/Edit Q&A
Edit your Account
Manage Authors
Statistics Page
Personal RSS Builder
My Home
Edit your Account
Update Profile
View/Edit Q&A
Publish Article
Author Box
Real IT Solutions has 1 articles online
Articles Categories
All Categories
Advertising
Arts & Entertainment
Automotive
Beauty
Business
Careers
Computers
Education
Finance
Food and Beverage
Health
Hobbies
Home and Family
Home Improvement
Internet
Law
Marketing
News and Society
Relationships
Self Improvement
Shopping
Spirituality
Sports and Fitness
Technology
Travel
Writing
Computer Forensics
Computer Games
Data Recovery
Databases
E-Learning
File Types
Hardware
Information Technology
Intra-net
Laptops
Networks
Operating Systems
Programming
Security
Software
]]>
Need Help?
Contact Us
FAQ
Submit Articles
Editorial Guidelines
Blog
Site Links
Recent Articles
Top Authors
Top Articles
Find Articles
Site Map
Mobile Version
Webmasters
RSS Builder
RSS
Link to Us
Business Info
Advertising
Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2011 Free Articles by ArticlesBase.com, All rights reserved.
About the Author: This article was provided by Real IT Solutions, a Derbyshire based Derby Web Design, Software Development and Internet Marketing company.
