I'm getting ready to release a feature to production tomorrow & feel I have misdesigned said feature. I'll try to explain my scenario clearly. I'm looking for alternative design suggestions to this. My real concern is my title - the "final" class takes two parameters of the same interface type, but they MUST be in the correct order...
I'm creating a mapping from string types to an enum meaningful within our system. So given a certain string, we should return the associated enum. Now, when we perform this lookup, our system will provide TWO strings. One of the strings is more specific than the other. So we should first look for a mapping for string A (more specific). If none is found, try to find a mapping for string B. For reasons that are difficult to get into, we NEED to have two separate mapping objects as the string mapping is a bit complex. One of my goals was to drive home this point & separate the definitions of the more specific mappings & less specific mappings. Here is my ultimate implementation scenario (rough C# pseudo-code, hopefully makes sense to other language users):
Interface IMap
{
public enum GetAssociatedValue(string lookup);
}
Class MapBase : IMap
{
protected override Dictionary<string, enum> MapAssociator;
public enum GetAssociatedValue(string lookup)
{
// lookup logic
}
}
Class MoreSpecificMap : MapBase
{
// This class only exists to define the mapping property, no behavior
constructor()
{
// Here, we override the "MapAssociator" property from the base class & define all
// values necessary
}
}
Class LessSpecificMap : MapBase
{
// This class only exists to define the mapping property, no behavior
constructor()
{
// Here, we override the "MapAssociator" property from the base class & define all
// values necessary
}
}
Class LookupService
{
constructor(IMap moreSpecificMap, IMap lessSpecificMap)
{
// Look, we need both the moreSpecific & lessSpecific maps, both of type IMap. But a
// user could pass these in the incorrect order & generate incorrect results, seems
// dirty
}
// Here as well, the caller of this function could pass the params incorrectly
public enum GetMapValue(string moreSpecificString, string lessSpecificString)
{
var result = moreSpecificMap.GetAssociatedValue(moreSpecificString);
if (result == null)
{
result = lessSpecificMap.GetAssociatedValue(lessSpecificString);
}
return result;
}
}
Right now this "LookUpService" class is defined via DI where I manually new up instances of the MoreSpecificMap/LessSpecificMap classes so it is not being created all over the code, but it still seems like a maintenance problem & point of confusion for future maintainers. Any thoughts or feedback on this?
Also, ignore the interface & class names - they have more meaningful names within my systems domain.
createRange(from, to)
orMath.pow(base, eponent)
, which is absolutely clear to every reader.)