Saturday 4 February 2012

Scalable Black Jack Card Game using Best OOP Practices,Dependency Injection and Singleton patterns (C#)


Introduction:
In this post, I will present the working example of scalable and reusable simple Black Jack Card Game using Best OOP practices, Dependency Injection and Singleton Patterns.
The key feature of this solution is usability, which achieve through above-mentioned factors


Project Structure: 
1) CardGameFramework: it contains base classes, which can be inherited to implement different card games.
2) BlackJackGame: it consists of classes specifically related to Black Jack Card Game.
3) BlackJackConsoleClient: it is a console application through which users play the game.




Brief Description of Design and how to add Future Enhancements:
I have used base class wherever it adds value. For example, I have a base class named as Player in my implantation, and that base class is extended using BlackJackPlayer for more specialized implementation in case of BlackJack Card Game.
You can extend and use classes in CardGameFramework to implement different card games, to use these classes you just need to add dll of CardGameFramework into your project.

You can easily replace BlackJackConsoleClient GUI with web based or any other GUI client. Again, you only need to add reference of BlackJackGame project.

To implement more specialized Black Jack Card Game rules you can extend BlackJackGame class. By this way I have ensured OPEN/CLOSE PRINCIPLE, which states that classesshould be open for extension but modification, should not be allowed.

Design Pattern: 
I have used Singleton Pattern to make sure that only single instance of BlackJackGame class is created. Secondly, I have implemented Dependency Injection to remove tight coupling between classes in BlackJackGame class.

In the below section of code I have used these two design patterns.

private static BlackJackGame _blackJackGameInstance;
        //method that make sure that only single instance for blackjackgame is return
        public static BlackJackGame BlackJackGameInstance(BlackJackPlayer player, BlackJackPlayer dealer, Deck deck)
        {

            if (_blackJackGameInstance == null)
            {
                _blackJackGameInstance = new BlackJackGame(player, dealer, deck);
            }
            return _blackJackGameInstance;

        }
        //private constructor so that object can not be created apart from singleton method
        private BlackJackGame()
        {

        }
        //overload constructor used for Dependency Injection, is private to make sure that only instance is created through singleton method
        private BlackJackGame(BlackJackPlayer player, BlackJackPlayer dealer, Deck deck)
        {
            _dealer = dealer;
            _player = player;
            _deck = deck;
        }
 You can download complete demo code with link provided.
http://code.google.com/p/jqgridwithqtipintegration/downloads/list

1 comment:

  1. Also check out this Blackjack Sample Project (C#) on GitHub:
    https://github.com/koistya/Blackjack

    ReplyDelete