C#C
C#3y ago
Sir Saba

❔ Factory Design method pattern

Hello! Can some one help me with Factory Method, I am working on RPN Calculator project and need to use this design method but I don't understand how it works and how to implement it. ChatGPT gave me a solution but i want to learn how it works.
public interface IOperator
{
    public double Calc(double x, double y);
}



public class Add : IOperator
{ 
    public double Calc(double x, double y) => x+ y;
    
}


//not sure how to do it
public class Div : IOperator
{

    public double Calc(double x, double y)
    {
        try
        {
            return x / y;
        }
        catch (Exception ex)
        {
            // make it in the program.cs that if this function gives 0 => to give "DivideByZeroException" 
            //Or find another solution

            throw new DivideByZeroException("Deiasda");
            return 0;
        }
    }
}
// not sure how to do it because the type is return double and not void 
public class Mod : IOperator
{
    //need to get modified, because of if u get 4 0 % the program should 

    public double Calc (double x, double y)
    {
        return x % y;
    }
}

public class Multi : IOperator
{
    public double Calc (double x, double y)=> x * y;
}

public class Sub : IOperator
{
    public double Calc (double x, double y)=> x - y;
}
Was this page helpful?