Is there a design pattern or anti-pattern for an approach when you keep a union of all needed properties in your base class without creation of dedicated derived subclasses. Different cases are handled by different execution paths, which is achieved by sets of conditions on this properties. Let me illustrate it with an example. Suppose you have public class Figure
:
public class Figure
{
public double? Radius { get; set; }
public double? A { get; set; }
public double? B { get; set; }
public double GetArea()
{
if (Radius != null)
return Math.PI * Radius.Value * Radius.Value;
if (A != null && B != null)
return A.Value * B.Value;
else if (A != null)
return A.Value * A.Value;
else if (B != null)
return B.Value * B.Value;
else
return double.NaN;
}
}
Here in this class instead of a class hierarchy you have a union of properties and a GetArea
method that checks them. Depending on the execution path a Figure
instance can be a square, rectangle or circle.
This example is taken to the extreme. In a more complex context of our business application proponents of this approach argue that it allows to flexibly declare different sub-kinds of class (for example, an order or tax) without the addition of complex type hierarchy and with no need to keep all of them explicitly. So' I'm curious if this is a known design pattern.
Aucun commentaire:
Enregistrer un commentaire