Taswar Bhatti
Practical guides on AI agents, LLM security, C#/.NET and Cloud, from Taswar Bhatti
Tag: Design Pattern
interview

In interviews there is always the Singleton question, I personally refuse to answer it on interview. The reason is very simple I don’t code singletons anymore. I use my IoC Container to provide me with a singleton. Sorry if you dont know what an IoC (Inversion of Control Container) is, then you are still in the old ages of coding.

For once and for all here is the hand written code of a singleton in multi-thread environment.

… Read the rest “I refuse to answer your stupid singleton question in interview”
delegate

Here is a nice little code I did recently, where the Command Pattern is used with a notification observer like pattern.

First of the Command Pattern, a simple interface for task with an execute method

Then the notification interface, with 2 methods, one when it started and one when its complete and the event delegate

… Read the rest “Command Pattern with Notification Event Delegates”
design patterns

Null Pattern

Last time I blogged about “Learn the Null Pattern“, but I forgot to mention one thing about the Null Pattern. That is you have to be mindful of it.

For example

The code looks okay but what if the GetEmployee method actually returns a NullEmployee? Well that would still be okay, since when the Pay method is called it would just do nothing.

Now what if we did this though

… Read the rest “Be mindful of the Null Pattern”
design patterns

As promised here is the version of observer pattern with Event & Delegate. Personally I am not a big fan of this solution, although it does remove quite a bit of code and show how to use a push model with the observer pattern. Next time around I will show a more elegant solution by using Event Aggregator.

But for now lets see how I implemented Observer Pattern with Event/Delegate.

First I tackled the Subject of our class, the thing you will see different is there is an instance object called TextChanged and is an EventHandler, the second change is it Notify method. I also removed the ISubject interface just because there isn’t really a need for it in this example and to make things simple.

… Read the rest “Learn the Observer Pattern with Event & Delegate”
UA-4524639-2