Posts

Showing posts from May, 2024

C# | Chain of Responsibility Design Pattern | Behavioral Design Pattern

Image
The chain of responsibility pattern creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a request based on type of request. This pattern comes under behavioral patterns. In this pattern, normally each receiver contains reference to another receiver. If one object cannot handle the request, then it passes the same to the next receiver and so on. UML Diagram - Who is what? The classes, interfaces and objects in the above class diagram can be identified as follows: 1.       Approver - Handler abstract class. 2.       Clerk, Assistant Manager & Manager - ConcreteHandler classes. 3.       Loan & LoanEventArgs - These classes are used for internal processing and holds request details.

C# | Proxy Design Pattern | Structural Design Pattern

C# | Flyweight Design Pattern | Structural Design Pattern

C# | Facade Design Pattern | Structural Design Pattern

C# | Decorator Design Pattern | Structural Design Pattern

C# | Composite Design Pattern | Structural Design Pattern

C# | Bridge Design Pattern | Structural Design Pattern

Image
Bridge pattern is used to separate an abstraction from its implementation so that both can be modified independently. This pattern involves an interface which acts as a bridge between the abstraction class and implementer classes and also makes the functionality of implementer class independent from the abstraction class. Both types of classes can be modified without affecting to each other. Bridge Pattern - UML Diagram & Implementation The UML class diagram for the implementation of the bridge design pattern is given below: The classes, interfaces and objects in the above UML class diagram are as follows: 1.       Abstraction This is an abstract class and containing members that define an abstract business object and its functionality. It contains a reference to an object of type Bridge. It can also acts as the base class for other abstractions. 2.       Redefined Abstraction This is a class which inherits from the Abstraction c...

C# | Adapter Design Pattern | Structural Design Pattern

Image
Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop. We are demonstrating use of Adapter pattern via following example in which an audio player device can play mp3 files only and wants to use an advanced audio player capable of playing vlc and mp4 files. Who is what? The classes, interfaces and objects in the above class diagram can be identified as follows: 1.       ITraget - Target interface 2.       Employee Adapter - Adapter Class 3. ...

C# | Prototype Design Pattern | Creational Design Pattern

Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls. Code Syntax- /// <summary> /// The 'Prototype' interface /// </summary> public interface IEmployee {     IEmployee Clone();     string GetDetails(); } /// <summary> /// A 'ConcretePrototype' class /// </summary> public class Developer : IEmployee {     public int WordsPerMinute { get; set; }     public string Name { get; set; }   ...

C# | Singleton Design Pattern | Creational Design Pattern

Singleton pattern is one of the simplest design patterns in C#. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class. - The Singleton pattern is a design pattern that restrict the instantiation he lifetime of the application, the instance remains same. - Throughout the lifetime of the application the instance will remain same. - Instance should be requested instead of created, if instance not available then create the instance, then provide otherwise provide available instance.  Why we need Singleton design pattern? - When there is single resource throughout the application. e.g- Database, Log File etc. - When there is a single resource and the...

C# | Factory Design Pattern | Creational Design Pattern

Image
Defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. This structural code demonstrates the Factory method offering great flexibility in creating different objects. The Abstract class may provide a default object, but each subclass can instantiate an extended version of the object. Code Syntax - public interface ICar {     void Start(); } public class SixSeater : ICar {     public void Start()     {         throw new NotImplementedException();     } } public class FourSeater : ICar {     public void Start()     {         throw new NotImplementedException();     } } public class CarFactory {     public ICar GetCar(string carType)     {          switch(carType)         {           ...

C# | Abstract Factory Pattern | Creational Design Pattern

Image
Abstract Factory patterns act as a super-factory which creates other factories. This pattern is also called as Factory of factories. In Abstract Factory pattern an interface is responsible for creating a set of related objects, or dependent objects without specifying their concrete classes. UML Diagram The classes, interfaces and objects in the above UML class diagram are as follows: 1.       AbstractFactory This is an interface which is used to create abstract product 2.       ConcreteFactory This is a class which implements the AbstractFactory interface to create concrete products. 3.       AbstractProduct This is an interface which declares a type of product. 4.       ConcreteProduct This is a class which implements the AbstractProduct interface to create product. 5.       Client This is a class which use AbstractFactory and AbstractProduct ...