Friday, September 13, 2024

Memento Design Pattern in C#

The Memento Design Pattern is a Behavioral Design Pattern that can restore an object to its previous state. This pattern is useful for scenarios where you need to perform an undo or rollback operation in your application. The Memento pattern captures an object’s internal state so that the object can be restored to this state later. It is especially useful when implementing undo functionality in an application.

Component of Memento Design Pattern

  1. Originator: The object whose state you want to save or restore.
  2. Memento: Stores the internal state of the Originator. It has two interfaces:
    • Caretaker Interface: This interface provides no access to the internal state of the Memento. It is used by the Caretaker to manage the Memento without modifying it.
    • Originator Interface: This interface allows the Originator to access the Memento to restore its state.
  3. Caretaker: The object that requests the saving and restoring of the Originator’s state.

Tuesday, September 10, 2024

Iterator Design Pattern in C#

The Iterator Design Pattern is a behavioral design pattern that allows sequential access to the elements of an aggregate object (i.e., collection) without exposing its underlying representation. That means using the Iterator Design Pattern, we can access the elements of a collection sequentially without knowing its internal representations. This pattern provides a uniform interface for traversing different data structures.

The collections in C#, like List, ArrayList, Array, etc., are containers containing many objects. In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the elements of the container.

Components of Iterator Pattern

  • Iterator: The interface that defines the methods for traversing the collection.
  • Concrete Iterator: The class that implements the iterator interface and performs the actual traversal.

Monday, September 9, 2024

Interpreter Design Pattern in C#

The Interpreter Design Pattern is a behavioral design pattern that defines a grammatical representation for a language and provides an interpreter to deal with this grammar. This pattern is particularly useful for designing simple languages or interpreting expressions.

When to Use the Interpreter Pattern

  • When you have a simple language to interpret.
  • When you need to interpret expressions in a language.
  • When the grammar of the language is relatively simple and stable.

Monday, July 8, 2024

Chain of Responsibility Pattern in C#

The Chain of Responsibility pattern is a behavioral design pattern that allows a group of objects to handle a request sequentially, each potentially handling the request or passing it on to the next object in the chain until the request is handled or reaches the end of the chain.

In simple words, we can say that the chain of responsibility design pattern creates a chain of receiver objects for a given request. In this design pattern, normally, each receiver contains a reference to the next receiver. If one receiver cannot handle the request, it passes the same request to the next receiver, and so on. In this case, one receiver can handle the request in the chain, or one or more receivers can handle the request.

Components of Chain of Resposibility Pattern

  1. Handler Interface (or Abstract Class): This defines a common interface for all handlers. It typically includes a method for handling requests and a reference to the next handler in the chain.
  2. Concrete Handlers: These are the actual handlers in the chain. Each handler implements the Handler interface and contains logic to handle requests. If it can't handle a request, it passes it to the next handler in the chain.
  3. Client: This initiates the request and sends it to the first handler in the chain.

Monday, April 22, 2024

Mediator Design Pattern in C#

The Mediator Design Pattern is a behavioral design pattern that promotes loose coupling between objects by encapsulating how they interact. It centralizes complex communication logic between multiple objects into a mediator object, thus reducing direct dependencies between them. This promotes easier maintenance and scalability of the system.

The Mediator Design Pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object. This pattern is used to centralize complex communications and control between related objects in a system. The Mediator object acts as the communication center for all objects. That means when an object needs to communicate with another object, it does not call the other object directly. Instead, it calls the mediator object, and it is the responsibility of the mediator object to route the message to the destination object.

Components of Mediator Design Pattern

  1. Mediator: Defines an interface for communication between colleague objects.
  2. Colleague: It is an abstract class, and Concrete Colleague classes will implement this abstract class.
  3. ConcreteMediator: Implements the mediator interface, coordinating communication between colleague objects.
  4. ConcreteColleague: Implements the colleague interface and communicates with other colleagues through the mediator.

Sunday, April 14, 2024

Template Method Design Pattern in C#

The Template Method Design Pattern is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. It allows reusing common behavior across multiple classes while still allowing customization where necessary.

Components of Template Method Design Pattern

  • Abstract Class: This class defines the template method, which is the skeleton of the algorithm. It consists of several abstract methods that subclasses must implement.
  • Concrete Classes: These classes inherit from the abstract class and provide implementations for the abstract methods.

Monday, April 8, 2024

Command Design Pattern in C#

The Command Design Pattern is a behavioral design pattern that encapsulates a request as an object, thereby allowing parameterization of clients with queues, requests, and operations. This pattern decouples sender and receiver of a request based on a command, which helps in invoking the right method at the right time without knowing the actual implementation details.

Components of Command Design Pattern

  1. Command: Defines an interface for executing an operation.
  2. Concrete Command: Implements the Command interface and binds a receiver with an action. It defines a binding between the action and the receiver.
  3. Invoker: Requests the command to execute the operation.
  4. Receiver: Knows how to perform the operation.

Monday, April 1, 2024

Strategy Design Pattern in C#

The Strategy Design Pattern is a Behavioral Design Pattern that enables selecting an algorithm’s behavior at runtime. Instead of implementing a single algorithm directly, run-time instructions specify which of a family of algorithms to use.

This pattern is ideal when you need to switch between different algorithms or actions in an object dynamically. That means the Strategy Design Pattern is used when we have multiple algorithms (solutions) for a specific task, and the client decides which algorithm to use at runtime.

Components of Strategy Design Pattern

  1. Strategy Interface: This defines a set of methods that represent the algorithms. It acts as a contract for all concrete strategy classes.
  2. Concrete Strategies: These are the actual implementations of the algorithms defined in the strategy interface.
  3. Context: This is the class that uses the strategy. It contains a reference to the strategy interface and can switch between different strategies dynamically.

Sunday, March 24, 2024

Observer Design Pattern in C#

The Observer Design Pattern is a behavioral design pattern that defines a one-to-many dependency between objects. When one object (the subject) changes its state, all its dependents (observers) are notified and updated automatically. This pattern is widely used in software engineering to establish communication between objects in a loosely coupled manner.

This Design Pattern is widely used for implementing distributed event-handling systems where an object needs to notify other objects about its state changes without knowing who these objects are.

In the Observer Design Pattern, an object (called a Subject) maintains a list of its dependents (called Observers). It notifies them automatically whenever any state changes by calling one of their methods. The Other names of this pattern are Producer/Consumer and Publish/Subscribe.

Components of Bridge Design Pattern

  1. Subject: This is the object that is being observed. It maintains a list of observers and provides methods to attach, detach, and notify observers of state changes.
  2. Observer: This is the interface that defines the method(s) that the subject will use to notify observers of state changes.
  3. ConcreteSubject: This is the concrete implementation of the subject. It maintains the state of interest and notifies observers when changes occur.
  4. ConcreteObserver: This is the concrete implementation of the observer. It registers itself with the subject and implements the update method to react to changes in the subject's state.

Thursday, January 25, 2024

Bridge Design Pattern in C#

The Bridge Design Pattern is a structural pattern that separates the abstraction from its implementation so that the two can vary independently.This pattern involves an interface that acts as a bridge between the abstraction class and implementer classes. It is useful in scenarios where an abstraction can have several implementations, and you want to separate the implementation details from the abstraction.

Purpose of Bridge Pattern

  • Decouple an abstraction from its implementation so that the two can vary independently.
  • Promote code reusability by allowing the abstraction and implementation to evolve independently.

Sunday, January 21, 2024

Flyweight Design Pattern in C#

The Flyweight design pattern is a structural pattern that focuses on minimizing the memory footprint or computational expenses of an object. It achieves this by sharing as much as possible with related objects, rather than keeping all of the data in each object. This is particularly useful when dealing with a large number of similar objects, as it helps reduce the overall memory consumption and improves performance.

Purpose of Flyweight Pattern:

  • To reduce the number of objects and to conserve memory by sharing objects among multiple contexts.
  • To achieve performance improvement by minimizing the overhead of creating and managing large numbers of similar objects.

Saturday, January 13, 2024

Proxy Design Pattern in C#

The Proxy Design Pattern is a structural design pattern. that provides a surrogate or placeholder for another object to control access to it.

This pattern comes in handy when we want to add an extra layer of control over the access to an object, such as lazy loading, access control, or logging. In C#, the Proxy Design Pattern is commonly used to create a surrogate object that represents another object.

We can also say that the Proxy is the object the client calls to access the real object behind the scene. Proxy means in place of or on behalf of. That means, In the Proxy Design Pattern, a class represents the functionality of another class.

Component of Proxy Design pattern

  • Subject: This is an interface that defines the members that will be implemented by the RealSubject and Proxy class so that the Proxy can be used by the client instead of the RealSubject. In our example, it is the ISharedFolder interface.
  • RealSubject: This is a class that we want to use more efficiently by using the proxy class. This class should implement the Subject Interface. In our example, it is the SharedFolder class.

Friday, January 12, 2024

Composite Design Pattern in C#

The Composite Pattern is a structural design pattern that enables us to treat individual and group objects uniformly by creating a hierarchical (tree-like) structure of objects, where both the composite (groups) objects and leaf (individual) objects share a standard interface.

This pattern lets clients treat individual objects and compositions of objects uniformly. That means the client can access the individual objects or the composition of objects in a uniform manner. It’s useful for representing hierarchical structures such as file systems, UI components, or organizational structures.

What are Composite and Leaf classes?

  1. The Composite Class — represents a group of objects and can contain other objects (i.e. env_item_collection, env_item_collection_parent, env_root), and
  2. the Leaf Class — represents an individual object that cannot contain other objects (i.e. env_item).

Monday, January 8, 2024

Facade Design Pattern in C#

Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.It helps encapsulate the complexity of multiple subsystems into a single unified interface.

In software terms, Facade pattern hides the complexities of the systems and provides a simple interface to the clients.

This pattern involves one wrapper class which contains a set of methods available for the client. This pattern is particularly used when a system is very complex or difficult to understand and when the system has multiple subsystems.

Component of Facade Design Pattern

  • Complex System: A library of subsystems.
  • Subsystems: These are classes within a complex system and offer detailed operations.
  • Façade: This is a wrapper class which wrapper class which contains a set of members which are required by the client.
  • Client: This is a class which calls the high-level operations in the Façade.

Tuesday, January 2, 2024

Decorator Design Pattern in C#

The Decorator Design Pattern is a structural pattern in software development that allows behavior to be added to individual objects, either statically or dynamically, without affecting the behavior of other objects from the same class.

The idea of the Decorator Pattern is to wrap an existing class, add other functionality to it, then expose the same interface to the outside world. Because of this our decorator exactly looks like the original class to the people who are using it.

It is used to extend or alter the functionality at runtime. It does this by wrapping them in an object of the decorator class without modifying the original object. So it can be called a wrapper pattern.

Components of Decorator Design Pattern

  • Component: It defines the interface of the actual object that needs functionality to be added dynamically to the ConcreteComponents.
  • ConcreteComponent: The actual object in which the functionalities could be added dynamically.
  • Decorator: This defines the interface for all the dynamic functionalities that can be added to the ConcreteComponent.
  • ConcreteDecorator: All the functionalities that can be added to the ConcreteComponent. Each needed functionality will be one ConcreteDecorator class.

Friday, December 29, 2023

Adapter Design Pattern in C#

The Adapter Design Pattern is a structural pattern that allows incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces, enabling them to collaborate seamlessly without modifying their existing code.The adapter plays the role of converter or translator.

This pattern is particularly useful when integrating new components or systems that have different interfaces from the ones already in use.

To handle the incompatibility, we use different approaches, and based on that, we can classify the Adapter Pattern into 2 parts.

  • Object Adapter Pattern
  • Class Adapter Pattern

Monday, December 11, 2023

Prototype Design Pattern in C#

What is Prototype Design Pattern?

The prototype design pattern is a creational design pattern that allows creating new objects by cloning an existing object. This pattern is useful when the creation of an object is costly or complex, and we want to avoid repeating the same process for each new instance. By using the prototype pattern, we can create new objects by copying the properties and behaviors of an existing object, and then modifying them as needed.

One of the benefits of the prototype pattern is that it reduces the dependency on subclasses and factory methods. Instead of creating objects using specific constructors or factory methods, we can use a generic prototype object that can be cloned and customized. This makes the code more flexible and extensible, as we can add new types of objects without changing the existing code.

Components of Prototype Pattern

  • Prototype: This will be an interface or abstract class used for the types of objects that can be cloned. In our example, it is going to be the Employee Abstract Class.
  • ConcretePrototype: This class will implement the Prototype abstract class or interface for cloning. In our example, it will be the PermanetEmployee and TemporaryEmployee Classes.
  • Client: The client is the class that creates a new object by asking a prototype to clone itself.

Monday, December 4, 2023

Builder Design Pattern in C#

What is the Builder Design Pattern?

The Builder Design Pattern is a creational design pattern that helps in constructing complex objects step by step. It separates the construction of an object from its representation, allowing the same construction process to create different representations. In C#, the Builder pattern is widely used to create objects with varying configurations while keeping the construction process unchanged.

The key idea is to separate the construction of a complex object from its representation, allowing the same construction process to create different representations.

So, the Builder Design Pattern is all about separating the construction process from its representation. When the construction process of your object is very complex, only you need to use the Builder Design Pattern.

COmponents of Builder Design Pattern

  • Abstract Builder: The Builder is an interface defining all the steps to make the concrete product.
  • Concrete Builder: The Concrete Builder Classes implements the Abstract Builder interface and provides implementation to all the abstract methods. The Concrete Builder is responsible for constructing and assembling the individual parts of the product by implementing the Builder interface. It also defines and tracks the representation it creates.
  • Director: The Director takes those individual processes from the Builder and defines the sequence to build the product.
  • Product: The Product is a class, and we want to create this product object using the builder design pattern. This class defines different parts that will make the product.

Sunday, November 5, 2023

Abstract Factory Design Pattern in C#

In this article, I will explain the Abstract Factory Design Pattern in C# with practical examples. I encourage you to check out our previous article, which covers the Factory Design Pattern in C# along with an example.The Abstract Factory Design Pattern falls under the category of creational design patterns and is widely applied in real-world software development. In this article, we'll explore the following topics

What is the Abstract Factory Design Pattern?

The Abstract Factory Design Pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It is a higher-level pattern than the Factory Method pattern, which deals with creating individual objects, while the Abstract Factory creates families of objects.

"Abstract" means hiding details, "Factory" refers to the entity that creates things, and "Pattern" indicates a design approach. Therefore, the Abstract Factory Pattern is a method in software design that allows you to wrap a set of factories with a shared theme.

Put simply, the Abstract Factory serves as a high-level factory that generates other factories. It's often referred to as the "Factory of Factories." This design pattern, the Abstract Factory, offers a way to create groups of related products without specifying the actual objects to be created.

Sunday, October 22, 2023

Factory Method Design Pattern in C#

The Factory Method Design Pattern belongs to the Creational Design Pattern Category.As part of this article, we will discuss this design pattern in detail with example

What is Factory Method Design Pattern

As per Gang of Four, the Factory Method Design Pattern states that Defines an interface for creating an object but lets the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.

In simple words, The Factory Method Design Pattern is used when we create the object without exposing the object creation logic to the client. In the factory method design pattern, we will create an abstract class as the Factory class, which will create and return the product instance, but it will let the subclasses decide which class to instantiate.

The Key Components:

  1. Factory Interface/Abstract Class:This serves as a blueprint for an interface or an abstract class that contains a method for creating objects. Typically, this method is named something like 'createProduct()' or 'factoryMethod()'.
  2. Concrete Factories:These are tangible classes that implement the factory interface. They provide specific implementations of the 'createProduct()' method. Each concrete factory is responsible for producing a particular type of product.
  3. Product Interface/Abstract Class:This defines an interface or an abstract class for the products generated by the factories. Product classes usually share common attributes or methods.
  4. Concrete Products:These are the real-deal classes that implement the product interface. Each concrete product embodies a specific type of object.
^ Scroll to Top