Tuesday, November 16, 2010

The Singleton pattern in C#

Singleton Pattern:Example One
Thread safe example
It is created at the beginning of the application and this makes it thread safe.
The static part of the instance is private and the public Singleton can be gotten using GetInstance() that returns an instance of this class hence making sure that only one instance of the object is created in the application current state for example a shopping cart on a webpage.
using System;
 /// 
/// Thread-safe singleton  created at first call
/// 
namespace Singleton
{  

    
    class Singleton
    {
         private static Singleton instance = new Singleton();

         private Singleton(){}

          public static Singleton GetInstance()
          {
            return instance;
          }

          public double Total { get; set; }
          public string UserName { get; set; }
     }
 }


Here is how you make a call on the Singleton class :
Singleton single = Singleton.getInstance();
single.UserName = "Ngala" ;
single.Total = 345.00 ;
//....

Singleton pattern: Example Two
Lazy Evaluation example
With Lazy evaluation it means it is created in the memory when it is needed. When the class is created, it first make sure that it is not in the memory before creating a one. When it is created it is locked on a single thread.

using System;
 /// 
///  Lazy Evaluation example
/// 
namespace Singleton
{  

    class SingletonLazyEval
    { 
         private static SingletonLazyEval instance;

          private SingletonLazyEval() { }

          public static SingletonLazyEval GetInstance()
          {
              lock (typeof(SingletonLazyEval))
            {
              if (instance == null)
              {
                  instance = new SingletonLazyEval();
              }
              return instance;
            }
          }
        
    }
}

Thursday, November 11, 2010

TekPub - Concepts

TekPub - Concepts
I love watching videos from Microsoft Mix . These videos are very enriching and one gets to know about current and future products of Microsoft .You get to see all these great videos(some of them) from the guys themselves that develop these products. I happen to have fallen on Rob Conery presentation on the MVC Store Front. Its just amazing and I really feel in love with the scripting aspect of his application. So I though to myself to see if this guy offers more. Luckily he had a website called Tekpub that publishes videos tutorials. The quality is really good. For now I am watching all his videos on key concepts in software. He demystifies them and I feel like I can start using all these knowledge the next time I want to build an application. The truth is interfaces are powerful in design of software and knowing how to use them makes you build really groundbreaking and flexible applications. Here are the key concepts he talks about and show us life code examples in his free videos .
1 - Dependency Injection and Inversion of Control
2 - Lambdas
3 - Unit Testing
4 - Loose Design with Interfaces
5 - Behavior-driven Design with Specflow
The truth is I have never really been interested in many of these concepts but when the old and experience guys in the industry tell you that it's important then it is .Since I want to become a big guy in the industry someday I will learn from the real big and experienced guys.
I really started blogging after following his video series from a Coder to a Developer .These videos are really enriching .I will encourage everyone to watch it .
The next thing for me to do is to write about the singleton pattern.Why?Its just a way for me to understand it better.

Sunday, November 7, 2010

A paddle and ball game in Javascript

 As a web developer you just have to know and write some javascript to add some extra feeling and functionality on your web applications .I am not a declared web developer but since i love the web it will be nice to be able understand its own key programming language . Here is where I start .I am building a small paddle and ball game .I am not starting from the basics because I already understand the basics in programming. What I wish to accomplish is to be able to link css to javascript ,move objects on the screen and let it work on all browsers. I wanted to do it with jquery but I feel like I am cheating on myself on my goal to learn the main scripting language for the web. Just some raw Javascript will make me happy.I know i will learn events in javascript,functions definitions,timing,key game concepts and others as I develop the game.
To start the development of this game .They are certain important objects that have to be defined.Anyway lets imagine how the player will play the game.

-load up the game on the browser
-move the paddle from left to right with the mouse
-every time the ball hits the paddle the game points increase.

From the above we see that they are certain key objects that we must have a reference to them and know their states.

1)The playing area.(where the game will be played)
2)The paddle
3)The ball
4)The score
In javascript you can represent your objects like css defined fields.This make life easy for everybody.Every object in the game has its own css properties.Lets not forget.This is a web based game.Hence I wrote these styles to fit the above objects .With this each style can me manipulated using Javascript.
As a web developer you must have some css feeling even if you are not a designer.Anyway you can put this in-line or in a separate file. I choose to put it in-line since its a small program

//Comment
//the ball and paddle object will be represented by images
//Still they will be in a div class representing them.



Coming up next will be the script to handle this objects on the screen.
Let start scripting ....

Since its inline Javascipt,this script will be at the head section of the html page.
1)In every game you must have references your objects or what ever components you will need to know their states in the game.
2)Name the components and initiate them to various positions


The first function is the initiate function
function init(){
      //get the ball,paddles and score references on the document by getting their Ids.           
            ball = document.getElementById('ball');
            paddle = document.getElementById('paddle');
            score = document.getElementById('score');
//since we shall use the keyboard to move the paddle we have to register input from the keyboard to the current document that is out game
            //register key listener with document object
            document.onkeydown = keyListener;
            //start the game loop
            start();
        }
We can find that there is a Keylistner that returns an event on a key press that will be linked to the paddle .Note that this can be different on various browsers.
function keyListener(e){
            if(!e){
                  //if its IE
                 e = window.event;
            }
            if(e.keyCode==37 && paddleLeft > 0){
                //keyCode 37 is left arrow
                paddleLeft -= 5;
                paddle.style.left = paddleLeft + 'px';
            }
            if(e.keyCode==39 && paddleLeft < 436){
                //keyCode 39 is right arrow
                paddleLeft += 5;
                paddle.style.left = paddleLeft + 'px';
            }
            //FYI - keyCode 38 is up arrow, keyCode 40 is down arrow
        }
 

The next function to handle will be the start function.Remember that is the last line in the initialisation function.This is where the magic begins.
That will be coming up next...........

Machine learning is the future

I am very enthusiastic about machine learning and the potential it has solve tough problems. Considering the fact that the amount of data we...