C
C#15mo ago
George

❔ 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;
}
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;
}
5 Replies
phaseshift
phaseshift15mo ago
I don't see factory anywhere.
// not sure how to do it because the type is return double and not void
I don't know what 'it' is - your code seems ok Div:
if (y == 0)
throw new DivideByZeroException("Deiasda");

return x / y;
if (y == 0)
throw new DivideByZeroException("Deiasda");

return x / y;
George
GeorgeOP15mo ago
yeah I don't know how to implement factory to my code
phaseshift
phaseshift15mo ago
The factory method would be,
IOperator MakeOperator(string name)
{
if (name == "sub") {
return new Sub();
else if ... {
}
}
IOperator MakeOperator(string name)
{
if (name == "sub") {
return new Sub();
else if ... {
}
}
switch block or switch expression would be 'cleaner'. Also not sure what you're using for the key - I just used string name as an example
George
GeorgeOP15mo ago
yes thanks for your help!!
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server