3

I have read the concepts and practiced some examples on OOAD and Design Patterns. But when I was asked to implement the singleton pattern in a little bit different manner, I was unable to do it and failed to reach my goal. I think I need some more in-depth understanding of the basics and some real experience with the concepts.

Please suggest me some good books and mini projects for me to get hands on experience on OOAD and Design Patterns in C#. I have almost 9 months of time frame to retake the test.

I have done this for singleton. It's valid code. But i was asked to implement in such a way that, i should be able to create instance of DBConnection class only once. use that instance to get the Connection object. This is where, i have failed to implement singleton :(

class DBConnection 
{
    private Connection _conn = null;
    static Connection GetConnection()
    {
         if(_conn ==null)
              _conn = new Connection(...);
         return _conn; 
    }
}
Dinesh
  • 131
  • 5
  • What was your goal, and how did you fail? – kevin cline Jan 24 '12 at 16:36
  • I want to be an expert in OOAD and Design Patterns. I wanted to learn in-depth. – Dinesh Jan 25 '12 at 02:52
  • I was not able to answer a simple questions like Definition of Polymorphism, Why we have to use class, etc... :( – Dinesh Jan 25 '12 at 02:53
  • I thought you were "unable to implement the singleton pattern in a different manner". What did you try? – kevin cline Jan 25 '12 at 04:18
  • @kevin, I have added my singleton code to the question. – Dinesh Jan 25 '12 at 07:18
  • You aren't having problems with patterns. You are having problems with C#, and should focus your studies there. Your code fails because you do not understand `static`. I would be surprised if this code compiles. Also, any use of `static` requires consideration of thread safety. – kevin cline Jan 25 '12 at 23:06

2 Answers2

1

Head First into Design Patterns is a great book for beginners that discusses the patterns with practical (but very ficticious) examples.

If you're more inclined into a reference book then the original GoF book might be right up your alley, but is rather lacking in examples other than for descriptive purposes.

There is a book, Agile Software Development, Principles, Patterns, and Practices by Uncle Bob, that is highly regarded and commonly used as college-level course material. It has some practical examples and also goes through the SOLID principles and have a chapter on test driven development (IIRC).

Spoike
  • 14,765
  • 4
  • 43
  • 58
  • +1 Much as I hated the dumbed down style of the Head First book, I have to begrudgingly admit that I did learn quite a lot from it. With "Uncle Bob" I'd say his principles are bang on, but his code is at best mediocre. –  Jan 24 '12 at 10:54
0

Check out the dofactory website. They list all of the GoF patterns with concrete examples in C#. They are trying to sell a product that implements the patterns for you but there is a lot of useful examples on their website.

Kevin Junghans
  • 216
  • 1
  • 3