Understanding The Pure And Impure Functions in C#

What is pure function in c#?

Pure functions

Pure function is that:

  1. Always returns the same output given the same inputs.
  2. Does not modify the state of object or any external state.
  3. Does not have any side effects such as I/O operations, exceptions or modifying external variables.
  • In other words, a pure function takes input, process it and returns the output without modifying anything else.
  • Pure functions are easy to test and debug.

Examples

  1. Mathematical calculations
public int Add(int a, int b)
{
 return a + b;
}
  1. String manipulation
public string Concatenate(string a, string b)
{
 return a + b;
}
  1. Array operations
public int[] SortArray(int[] array)
{
 return array.OrderBy(x => x).ToArray();
}
  1. Data transformations
public string ConvertToString(int value)
{
 return value.ToString();
}
  1. Validation
public bool IsValidEmail(string email)
{
 return email.Contains("@");
}

Impure Functions

Impure function is that

[Read More]

Command Pattern With C#

Command Pattern

The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object containing all the information about the request. This conversion allows you to parameterize methods with different requests, queue requests, and support undoable operations.

In simpler terms, the Command Pattern encapsulates a request as an object, thereby allowing users to decouple the sender (invoker) from the object that performs the action (receiver).

Key Concepts

  1. Command: An object that encapsulates a request or action.

    [Read More]

Adapter Pattern: Bridging the gap between incompatible interfaces

The Adapter pattern in c#

The Adapter pattern is a structural design pattern that allows two incompatible interfaces to work together by converting the interface of one class into an interface expected by the clients. In other words, it acts as a bridge between two incompatible interfaces, enabling them to communicate with each other.

Adapters in real world?

For a phone that does not support a 3.5 mm audio jack, you can use a USB-C to 3.5 mm audio adapter. The phone has only a USB-C port, so the adapter converts the USB-C connection into a 3.5 mm audio jack, allowing you to use traditional wired headphones with your phone. The adapter bridges the gap between the old headphone interface and the new phone design.

[Read More]

Facade Pattern With C#

Facade Design Pattern (with c# example)

The Facade pattern is a structural design pattern that provides a simplified interface to a complex system of classes or interfaces.

Let’s understand it with example. Imagine a home automation system.

home automation system

Let’s implement it programmatically.

public class FacadePatternTestDrive
{
    public static void Main(string[] args)
    {
        ILights lights = new Lights();
        IAirConditioner airConditioner = new AirConditioner();
        ISecuritySystem securitySystem = new SecuritySystem();

        // When leaving home
        lights.TurnOff();
        airConditioner.TurnOff();
        securitySystem.Arm();
        Console.WriteLine("You have left home.");

        // When entering home
        lights.TurnOn();
        airConditioner.TurnOn();
        securitySystem.Disarm();
        Console.WriteLine("Welcome home!");
    }

}

Note: AirConditioner, SecuritySystem and Lights class is not present in the above example for the sake of simplicity. We will write those classes when we implement the facade pattern.

[Read More]

Decorator Pattern With C#

decorator patter with c#

The Decorator pattern is a structural pattern that enables you to wrap an object with additional behaviors or responsibilities without changing its external interface. It’s like adding layers of decorations to a gift — you can add or remove decorations without altering the gift itself.

Example:

Let’s say, we have a book whose base price is 500 INR. Some buyers need a hard cover book which costs some extra money, some need a signed copy which also add some additional cost, some needs the both hard cover and a signed copy which adds more cost. May be in future we want to remove the option of hard cover book or may be we want to add some extra features to it. How do we make our system more flexible, that is the big question.

[Read More]

Observer Pattern

Observer pattern in c#

The Observer pattern is a behavioral design pattern that defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

In a simple words, it is like a publisher and subscriber pattern. When publisher publishes something, all it’s subscriber gets notified.

One Example could be, when we subscribe someone’s mailing list we get notified whenever the publisher publishes something.

[Read More]

Abstract Factory Pattern With C#

Abstract factory pattern (with c# example)

The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows a client to create objects from a variety of related classes without needing to know the specifics of their implementations.

GUI Factory Example

Let’s say, we need to create GUI components for different themes in our application. Each theme includes a consistent set of components such as buttons and checkboxes. We want to ensure that the creation of these components is consistent and interchangeable.

[Read More]

Factory Method Pattern with c#

factory method pattern in c#

The Factory Method Pattern is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

This pattern is useful when a class cannot anticipate the class of objects it needs to create or when subclasses want to specify the objects to be created.

A Notification System

Imagine we need to develop a notification system that can send different types of notifications such as Email, SMS, and Push Notifications. Each type of notification has a distinct implementation but shares a common interface. Instead of creating each notification type directly in our main logic, we can use the Factory Method Pattern to encapsulate the instantiation process, making our code more modular and extensible.

[Read More]

Understanding Aggregation: How It Differs from Composition

Aggregation in oops. Aggregation vs Composition.

Aggregation is when an object is made up of one or more objects, but those objects can live outside the main object. It defines the has-a relationship.

Note: In the aggregation, if main object is destroyed, its aggregated objects may still live.

Example #1: A Library is an aggregation of Books. Books can live without the library.

Example #2: A Department is an aggregation of Employees. If department closes, the Employees can exist on their own.

[Read More]

Composition: What, Why And When?

what is composition?

Composition is when your class is composed of one or more objects from other classes. A simple example is a Car composed of Engine and Wheel objects. Inheritance defines an “is-a” relationship, while composition defines a “has-a” relationship.

For example, Dog is-a Animal, and Circle is-a Shape. It defines inheritance.

This makes sense because you want to reuse all the features of Animal (eat, sleep, walk, makeNoise) in the Dog class. Cow is-a Animal too, so Cow can also inherit the Animal class. Since cows and dogs make different noises and eat different things, we have to redefine or override the makeNoise() and eat() methods in the Dog and Cow classes. The point is when we want to reuse the functionality of a class and also want subtype polymorphism (Dog and Cow are subtypes of Animal), then we must use inheritance.

[Read More]