Click here to Skip to main content
11,496,146 members (769 online)
Click here to Skip to main content

DesignPattern

, 7 Jun 2008 CPOL 11.1K 68 21
Singleton design pattern helps create just object from a class and direct that object only.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

Introduction

The idea of design patterns is not new. Ever since there were engineers, design patterns have existed. The architects of the great pyramids must have used design patterns to envision and eventually build one of the most durable testimonies to quality engineering.
So what are design patterns? Simply put, design patterns name and describe effective solutions for common design problems. The name of a pattern gives us a common term that may be used when discussing design solutions. The description provides a guideline or template that can be applied to a frequently occurring design problem.

Background

Actually you dont need any backgroud in this kind of stuff because the Design Pattern is nothing just some experience of other guys!

Design patterns are recurring solutions to software design problems you find again and again in real-world application development. Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges. The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral.

strictly speaking,With Design pattern you will not wish to invent the wheel again,although you would use them for creating most efective apllications.In this Article I will explain what Singleton design pattern is and how to implement that.now That's upon you to choose where to use That!

Come on Let whine to up!

Using the code

Ok What is Singleton pattern.As its name imply it has to do with somthing "single"!.This design pattern is categoried in "Creation Patteren" and it's intention is to ensure that we can create just one object from our desired class.So due to this all our needs would wrap to this object.

Actually this object does the whole work as a common and One object.So now lets look at some codes:

           

public class SingletonPattern

{

//We Intentionally use Private constructor because we want not to be able to create objects

SingletonPattern() { }

//Here is One Nested class for lazy instantiation

//Lazy instantiation means untill there is no invokation there would be

//no object initalization.

class SingletonCreator

{

//This class is not able to be reached by the caller,so we use static private for it.

static SingletonCreator() { }

// Private object instantiated with private constructor

internal static SingletonPattern OneInstance = new SingletonPattern();

}

// Public static property to get the object

public static SingletonPattern ONEInstance

{

get 

{

return SingletonCreator.OneInstance; 

}

}

public void Foo()

{

MessageBox.Show("I have created just Once!");

}

}
        

I have used Lazy instantiation.What is "Lazy instantiation" any way?

It means until the first Invokation, no object would be instantiate.You are also able to use "static" fields for Lazy instantiation because they also use the concept of Lazy ins...

So Guys I hope you get what Singleton pattern would be.Any feedback would be appreciated.Happy Coding!

Points of Interest

I am now workin and trying hard to increase my object oriented and design pattern knowledge.If anybody knows any helpfull areas in this kind of fields please share that.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Morteza Naeiamabadi
Software Developer (Senior)
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
independent IT Consultant,Currently engaged with Sharepoint developing and MVC Asp.net apps

Comments and Discussions

 
GeneralSingelton Holder Pin
FlorianPraxmair11-Jun-08 11:41
memberFlorianPraxmair11-Jun-08 11:41 
GeneralRead this: Pin
PIEBALDconsult7-Jun-08 6:26
memberPIEBALDconsult7-Jun-08 6:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 7 Jun 2008
Article Copyright 2008 by Morteza Naeiamabadi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid