C
C#9mo 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
phaseshift9mo 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
George9mo ago
yeah I don't know how to implement factory to my code
phaseshift
phaseshift9mo 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
George9mo ago
yes thanks for your help!!
Accord
Accord9mo 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
More Posts
❔ Is there a way to make this more cleaner?```html <div class="buttons"> <div class="dropdown"> <a button type="button" class="btn btn-Is there a way to determine if a Winforms .exe is run by double clicking on it on Windows explorer?What I wish to achieve is nothing too fancy. I just want to determine if my winforms exe is run from❔ Recommended way to follow developments in C# / .NET world?I'm looking for a way to follow weekly or monthly developments in the C# / .NET world, something com✅ dotnet add reference in vscodethere is gui way to add reference in vscode instead of run command or type , i mean like vs communit❔ win form app desinger app erorrSeverity Code Description Project File Line Suppression State Error CS1061 '✅ ASP.NET EFC MVC deletes authentication cookie after next request.I've been having this issue that my asp.net app is deleting the login cookie right next request afte❔ What does "Specify the UNC path" mean when deploying using ClickOnce?I am attempting to deploy my application using ClickOnce, as per the title. I have set my publish lo❔ Stack Overflow Exception due to excessive cancellation token related method invocations?I'm getting a stack overflow exception at unpredicable times and upon investigation in Rider's paral❔ Insert a document to a MongoDBHey there... I am trying to build a simple API connected to a MongoDB. I have customers and I want ❔ Can someone help explain this is a more basic way.I get what arrays, integers, parameter and such are but reading it this way doesn't make sense.