Skip to main content

Posts

Showing posts with the label Design Pattern

Singleton Design Pattern and different ways to implement it in C#

The singleton pattern is one of the best-known patterns in software engineering. In a singleton pattern, we ensure that the class has only one instance and we provide a simple and global point of access to this object. This is useful when we require exactly one object of a class to perform our operations. Singleton classes don't allow any parameters to be specified when creating the instance because a second request for an instance but with a different parameter could be problematic. In this article, I'll provide you with a real-time example, a Comparison of Singleton with Static class, a UML class diagram of Singleton, and then the implementation of Singleton in various ways. Real-time example: The practical implementation of the Singleton design pattern can be seen in various applications used by us. Take the example of Microsoft word, there is only one instance of the word application active at a time even though the user opened multiple documents at a time. And all the req...

Dependency Injection pattern and Inversion of Control with C#

Understand the Object Dependency and Coupling: When an object needs another object for a required task, then the first object is dependent on the second. For example  three objects, namely, X, Y, and Z. If object X is coupled to object Y, and Y is in turn coupled to Z, then object X is effectively coupled to object Z, i.e. it is dependent on Z. This behavior is known as transitivity. There are two ways to couple the objects: Tight Coupling:  In this, the objects are not independently reusable and hence are difficult to use effectively in unit test scenarios. Loose Coupling:  In this scenario, an object is loosely coupled with another object, and you can change the coupling with ease and can be used easily in unit test scenarios. Dependency Injection (DI): The basic idea behind the Dependency Injection is to have a separate object and "Design must be loosely coupled". Dependency injection eliminates tight coupling between objects to make both the objects and applications ...