Factory Methods
See original GitHub issueI was thinking it might be quite nice to have an opt-in feature that makes you rely on factory methods to create the different types within a union. I was thinking this could be another attribute maybe [UnionWithFactory]
or something like [Union(factory: true)]
.
I took the code example from the samples and modified it to show how the factory methods could be used:
using Dunet.Choices;
using Dunet.Shapes;
var rectangle = Shape.Rectangle(10, 10);
var triangle = Shape.Triangle(10, 10);
var circle = Shape.Circle(10);
var getArea = (IShape shape) =>
shape switch
{
Rectangle rect => rect.Length * rect.Width,
Circle circle => 2.0 * Math.PI * circle.Radius,
Triangle triangle => 1.0 / 2.0 * triangle.Base * triangle.Height,
_ => 0d,
};
var rectangleArea = getArea(rectangle);
var triangleArea = getArea(triangle);
var circleArea = getArea(circle);
Console.WriteLine($"Rectangle area: {rectangleArea}");
Console.WriteLine($"Triangle area: {triangleArea}");
Console.WriteLine($"Circle area: {circleArea}");
var choice = GetChoice();
if (choice is Yes)
{
Console.WriteLine("YES!!!");
}
if (choice is No)
{
Console.WriteLine("NO!!!");
}
static IChoice GetChoice() =>
Console.ReadLine() switch
{
"yes" => Option.Yes(),
_ => Option.No()
};
What do you think?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Factory Method
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the...
Read more >Factory method pattern
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects ......
Read more >Factory method for designing pattern
The factory method is a creational design pattern, i.e., related to object creation. The Factory Method pattern is used to create objects without...
Read more >Factory Method Design Pattern
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the...
Read more >Factory Method Design Pattern
Factory Method is to creating objects as Template Method is to implementing an algorithm. A superclass specifies all standard and generic behavior (using ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Preliminary PR for this here: https://github.com/domn1995/dunet/pull/33
After some more analysis, I’m going to defer this feature for the following reasons:
Foo.NewThing()
rather thannew Foo.Thing()
is not worth the increase in complexity and potential maintenance burden to this library.If the community remains interested in this feature, I would whole-heartedly support a separate package containing this functionality. I’d also be glad to link to it from Dunet’s readme if that becomes the case.