Tuesday, March 9, 2010

Singleton Pattern!

Singleton Pattern... A right choice to start our journey of learning design patterns with, because it's not that hard to understand and we should take start with easy things before moving forward to the more difficult and tricky ones.

Let's first examine what official definition says:

"Ensure a class has only one instance and provide a global point of access to it."

pretty straight forward? although many of you may find it hard to get a concept from a definition like myself, so we'll practically implement it.

But let's first elaborate the definition or should i say break it into parts to understand it more deeply, shall we? ok Great!

The definition can be broken into parts like:

  1. Ensure a class has only one instance
  2. Provide a global point of access to it

The very first point suggest us to design a class (I am supposing here you already know what a class is!) in such a way that it can be instantiated only once. You can think of the object instantiated as ONE OF ITS KIND, throughout your application not another instance of the same object can be created. Let's have a look at the example:

Class ScoreBoard

..........Private Shared _instance As ScoreBoard

..........Private Sub New()
....................'private constructor
..........End Sub

..........Public Shared Function GetInstance() As ScoreBoard

....................If _instance Is Nothing Then

.............................._instance = New ScoreBoard()

....................End If

....................Return _instance

..........End Function

End Class

Let's examine the class, the very first thing you might have noticed is it's constructor which is private which means you cannot directly instantiate the class, you have to call it's GetInstance() function to instantiate the object, which brings us on the Function itself where we've assured that if the instance is already there we don't need to create another remember the 1st point? Ensure a class has only one instance. Now let's use the class:

Sub Main()

..........Dim s1 = ScoreBoard.GetInstance()
..........Dim s2 = ScoreBoard.GetInstance()

..........If s1.Equals(s2) Then

....................Console.WriteLine("Objects are same...")

..........End If

..........'Just to hold the window until user presses a key
..........Console.ReadLine()

End Sub

Output:

Objects are same...

ohh... what about the second point? Provide a global point of access to it... Relax! it's there in the Main function ScoreBoard.GetInstance()!

Making it safe!

what if you are working in a multi-threaded application and threads are trying to access the GetInstance() function at the same time? You can use any locking mechanism provided by .NET e.g.

Class ScoreBoard

..........Private Shared _instance As ScoreBoard
..........Private Shared synchLock As New Object()

..........Private Sub New()
....................'private constructor
..........End Sub

..........Public Shared Function GetInstance() As ScoreBoard

....................SyncLock synchLock

..............................If _instance Is Nothing Then

........................................_instance = New ScoreBoard()

..............................End If

....................End SyncLock

....................Return _instance

..........End Function

End Class

Thanks for your patience and I hope you got the concept of this pattern. So, stay live for more!

Monday, March 8, 2010

Head first design pattern a must have!

Head first design pattern is a great book to start your learning with on Design patterns. Because the way they present a particular pattern is what makes it a unique book and a must have for every developer, although the code examples is written in Java but anyone working in any language what so ever can have benefit from it. I'll be posting my learning on each design pattern from the book but they are not in any case an alternate to the material presented in the book because It's much more elaborated in the book then the ones I'll be presenting here.

Friday, January 29, 2010

iPad another smart move by Apple but ...




iPad another smart move by Apple but how smart it is? can't be sure when we look at the price factor which have been set to a huge $500 if we compare it with Amazon kindle available at nearly half of the price Apple has to offer for iPad or Sony Digital Reader PRS-900 Touch which is available at nearly $100 less, and wait for the international deal if you are not from U.S. which might be more than $500 i guess. I don't think so that it'll be a threat to already at #1 best seller position on Amazon - Kindle, or for Sony digital reader either. If Apple wants to remain in the competition, it has to revise it's pricing strategy.

Sunday, August 2, 2009

Windows Live Writer !

A piece of software rather a blessing for bloggers where they can easily manage their blog posts and publish blogs after previewing them with spell check functionality :)

Wednesday, April 22, 2009

Software Design Charactristics

Now a days I am reading a fantastic book Code Complete 2 written by one of the finest and famous writer of Software Engineering books Steve McConnell. This blog will actually now served me as keeping notes and important points from this book so that I can use these points as reference and also to share it with all of you.

Although this books is mostly about software construction but he also described the characteristics of a well designed software. Bellow I am presenting these Characteristics with their key concepts.

Minimal Complexity:
  •  Keep your design simpe and easy to understand don't make clever designs because they are hard to understand.

Ease of Maintenance:
  • Design the system to be self-explanatory.

Minimal Connectedness:
  • Means designing so that you hold connections among defferent parts of a program to a minimum. Use the principles of strong cohesion, Loose coupling, and information hiding to design classes with as few interconnections as possible.
Exensibility:
  • Extensibility means that you can enhance a system without effecting other parts.
Reusability:

  • Design a system in such a way that you will be able to use the work you've already done in other system where there is a similar functionality needed.
High fan-in:
  • Referes to having a high number of classes that use a given class.
Low-to-Medium fan-out:
  • Means a given class use a low-to-medium number of other classes.
Probability:
  • Probability means designing system in such a way that it can easily be moved to another environment.
Leanness:
  • means designing the system so that it has no extra parts.

Stratification:
  • Stratified design means trying to keep the levels of decomposition stratified so that you can view the system at any single level and get a consistent view.
  • If yor're writing a modern system that has to use a lot of older, poorly designed code, write a layer of new system that's responsible for interfacing with the bad code. Design the layer so that it hides the poor quality of the old code, presenting a consistent set of services to the newer layers.

Standard techniques:
  • Try to use standardized and common approaches or It'll be very much difficult for someone to understand it first time.