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.

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
^ Scroll to Top