Make laddu

 To introduce the **Abstract Factory** design pattern, we need to encapsulate the creation of different types of laddus into separate classes. The `LadduFactory` will be responsible for deciding which laddu to create based on user input, adhering to the **Abstract Factory** pattern.


Here’s how you can modify your existing code with the **Abstract Factory** design pattern:


### Code Implementation:


```csharp

using System;


class Program

{

    static void Main(string[] args)

    {

        // Ask the user if they want Tirupati Laddu

        Console.WriteLine("Do you want Tirupati Laddu? (y/n)");


        // Read user input

        string ans = Console.ReadLine();


        // Create a factory based on the user's input

        ILadduFactory ladduFactory = LadduFactoryProducer.GetFactory(ans);


        // Check if a valid factory is returned

        if (ladduFactory != null)

        {

            // Create the laddu from the selected factory

            ILaddu laddu = ladduFactory.CreateLaddu();

            laddu.Make();

        }

        else

        {

            Console.WriteLine("Invalid choice. Please enter 'y' or 'n'.");

        }

    }

}


// Abstract Laddu interface

interface ILaddu

{

    void Make();

}


// Concrete Laddu classes

class NandnaLaddu : ILaddu

{

    public void Make()

    {

        Console.WriteLine("Make this laddu with Nandna ghee.");

    }

}


class ARDairyLaddu : ILaddu

{

    public void Make()

    {

        Console.WriteLine("Make this laddu with AR Dairy ghee.");

    }

}


// Abstract Factory Interface

interface ILadduFactory

{

    ILaddu CreateLaddu();

}


// Concrete Factories

class NandnaLadduFactory : ILadduFactory

{

    public ILaddu CreateLaddu()

    {

        return new NandnaLaddu();

    }

}


class ARDairyLadduFactory : ILadduFactory

{

    public ILaddu CreateLaddu()

    {

        return new ARDairyLaddu();

    }

}


// Factory Producer to select the appropriate factory based on user input

class LadduFactoryProducer

{

    public static ILadduFactory GetFactory(string answer)

    {

        // Convert input to lowercase to make the comparison case-insensitive

        if (!string.IsNullOrEmpty(answer) && answer.ToLower() == "y")

        {

            return new NandnaLadduFactory();

        }

        else if (!string.IsNullOrEmpty(answer) && answer.ToLower() == "n")

        {

            return new ARDairyLadduFactory();

        }


        // Return null if the input is invalid

        return null;

    }

}

```


### Breakdown of the Code:


1. **`ILaddu` Interface**: Defines the method `Make()` for all types of laddus.

   

2. **Concrete Laddu Classes**:

    - `NandnaLaddu`: Implements `ILaddu` and defines making the laddu with "Nandna ghee".

    - `ARDairyLaddu`: Implements `ILaddu` and defines making the laddu with "AR Dairy ghee".

    

3. **`ILadduFactory` Interface**: Declares a method `CreateLaddu()` for creating different types of laddus.

    

4. **Concrete Factory Classes**:

    - `NandnaLadduFactory`: Creates `NandnaLaddu`.

    - `ARDairyLadduFactory`: Creates `ARDairyLaddu`.

    

5. **`LadduFactoryProducer` Class**: Contains the logic to select the appropriate factory based on user input. If the user enters `'y'`, it returns a `NandnaLadduFactory`, otherwise, it returns an `ARDairyLadduFactory`.


6. **Main Method**: In the `Main` method, we get the appropriate factory using the `LadduFactoryProducer`, create the laddu, and call the `Make()` method to display which ghee to use.


### Benefits:

- **Separation of Concerns**: Laddu creation logic is separated into factories.

- **Extendibility**: You can easily add new types of laddus without changing existing code by adding new factories and laddu types.

- **Abstract Factory**: The pattern allows for creating families of related or dependent objects without specifying their concrete classes.


This structure enhances maintainability and scalability in a larger system where you might have multiple types of laddus and corresponding creation logic.

Comments

Popular posts from this blog

आईटी पेशेवर अब बेंगलुरु छोड़ने और मुंबई के पास नैना सिटी में बसने पर विचार कर रहे हैं।

Common HR Requirements Mistakes

BMS ABDUL PRINCIPLES