Sunday, November 19, 2023

Angular 17 : New control flow syntax

Angular17- New control flow

Angular 17 recently came out with several exciting updates. One notable addition is the Control Flow feature. This new feature simplifies template writing by introducing a direct way to handle control flow within the template itself. Now, there's no need to rely on directives like *ngIf, *ngFor, and *ngSwitch for control flow as this new syntax streamlines the process.

This post will demonstrate a basic project using a new control flow method, moving away from the traditional directive-based approach. You can go through my other Angular post here.Let's begin right away!

Angular Project Setup

Before we start using the new feature, let's make sure you have an Angular project set up and ready to go. If you haven't done so yet, create a new Angular project using these commands with the Angular CLI:

npm install -g @angular/cli@latest
ng new ng17-control-flows

This command creates a fresh Angular project, including all the essential files and dependencies with the latest version.

Once it's set up, open the app.component.html file and remove the default Angular code. Let's add a basic HTML structure to it instead.

<h1>NG -17 :New Control Flows</h1>

Conditionally rendered control blocks: @if and @else

Let’s start with the replacement of *ngIf.

In the first example, I create a checkbox and bind it to the isChecked property.Starting with a default value of true, the checkbox appears checked, displaying the content within the @if block.The examples below are from the app.component.html template file:

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.

Sunday, October 1, 2023

Factory Design Pattern in C#

The Factory Design Pattern is one of the most frequently used design patterns in real-time applications. The Factory Design Pattern in C# falls under the Creational Design Patterns Category.

What is Factory Design Pattern in C#?

Let us first try to understand the definitions of the factory design pattern.

According to Gang of Four (GoF), the Factory Design Pattern states that A factory is an object used for creating other objects. In technical terms, we can say that a factory is a class with a method. That method will create and return different objects based on the received input parameter.

In simple words, when we have a main class(super class) and several different types of classes(subclasses) that are related to it, and we want to make an object from one of these related classes based on some information, we use something called the Factory Design Pattern in C#.

The Key Components:

  1. 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.
  2. Concrete Products:These are the real-deal classes that implement the product interface. Each concrete product embodies a specific type of object.
  3. Factory Class:This class contains a method for creating objects.Typically, this method is named something like 'createProduct()' or 'factoryMethod()'.

Sunday, September 10, 2023

Thread-Safe Singleton Design Pattern in C#

In this post, we are going to discuss How to Implement Thread-Safe Singleton Design Pattern in C# with Examples. Please read our previous article where we discussed Singleton Design Pattern. In that post, the way we have implemented the Singleton Design Pattern is not Thread Safe in a Multithread Environment.

Understand Thread-Safe in Singleton Design Pattern in C#.

Before deep dive in to Thread-Safe Singleton Pattern, let us first see the problem that we face in a multithread environment if the Singleton Class is not Thread-Safe. Below is the Singleton Class which we have created in our last post.

    /// <summary>
    /// Sealed class to ensure that it cannot be inherited
    /// </summary>
    public sealed class Singleton
    {
        /// <summary>
        /// To store the Singleton Instance
        /// </summary>
        private static Singleton instance = null;

        /// <summary>
        /// Counter value will be increment by 1 each time the object of the class is created
        /// </summary>
        private static int counter = 0;

        /// <summary>
        /// Private constructor to restrict the class to be instantiated from outside the class
        /// </summary>
        private Singleton()
        {
            counter++;
            Console.WriteLine("Counter Value " + counter.ToString());
        }

        /// <summary>
        /// Static Method to return the Singleton Instance
        /// </summary>
        public static Singleton Instance()
        {
            if (instance == null) { instance = new Singleton(); }
            return instance;
        }

        /// <summary>
        /// Method to accessed from outside of the class by using the Singleton Instance
        /// </summary>
        /// <param name="message"></param>
        public void PrintDetails(string message)
        {
            Console.WriteLine(message);
        }
    }

Let’s modify the Program.cs as follows to use multithread programming. As you can see in the below code, we are using Parallel.Invoke method and call PrintCoachDetails() and PrintPlayerDetails() methods parallelly.

Parallel.Invoke(() => PrintCoachDetails(),
                () => PrintPlayerDetails());

Console.ReadLine();

static void PrintCoachDetails()
{
    //Thread-1 Calling the GetInstance() Method of the Singleton class
    Singleton fromCoach = Singleton.Instance();
    fromCoach.PrintDetails("From Coach");
}
static void PrintPlayerDetails()
{
    //At the same time, Thread-2 also Calling the GetInstance() Method of the Singleton Class
    Singleton fromPlayer = Singleton.Instance();
    fromPlayer.PrintDetails("From Player");
}

So in above example, we are using the Parallel.Invoke method to access the Instance() Method parallelly. That means at the same time multiple threads are accessing the Instance() Method. The above code is not Thread-Safe because the way we have written the code here two different threads can evaluate the condition if (instance == null) at the same time and both threads found it to be true and they both will create the instances, which violates the singleton design pattern. So, now run the application and it will give you the following output.

singleton design pattern

The above output clearly shows that the counter value has incremented to 2, which proves that the constructor of the Singleton class is executed two times as a result two instances of the singleton class have been created. So, if the Singleton class is not Thread-Safe, then we may end up creating multiple instances of the Singleton class in a Multithread Environment.

^ Scroll to Top