The code I've inherited has a design that I'm not familiar with (I'm still new to the world of programming).
It is a .net project and there are 3 classes in question here.
public Class1
{
public void DoSomething()
{
Class2 class2 = new Class2();
string myValue = class2.GetSomeValuePlease();
//why would I not do class2.MyClass3.GetActualValue();
}
}
public Class2
{
public Class3 MyClass3 = new Class3();
public string GetSomeValuePlease()
{
return this.MyClass3.TheActualValue();
}
}
public Class3
{
public string TheActualValue()
{
return "This is the value";
}
}
As you can see, all 3 classes are public. I don't understand why, using the example above, I would use Class2 at all? I could understand if within the GetSomeValuePlease()
method there was some logic which affected Class2, but there isn't.
Normally, I'd just go ahead and remove the Class2 method (GetSomeValuePlease) and call Class3 method (GetActualValue) direct from Class1, but, the developer I took over (who is not contactable) is wiser and more experienced than I. I have a feeling this is just over engineering and this is just extra code / extra maintenance.
Does any one have experience in designing this way that could explain the thought process or implications of having this "middle class" vs going direct?
Best Answer
Looks like the purpose of
Class2
is to abstract away the implementation ofClass3
.This allows
Class3
to be changed in the future. Maybe the value will be grabbed from a database, maybe it will be read from a local file, maybe it will be read from the web. Maybe it won't even be read fromClass3
at all, maybe it will be read fromClass4
.The abstraction makes it possible to change the implementation of getting a correct value without
Class1
having to change any code.This is a valid design pattern (see
Facade PatternBridge Pattern). To say whether or not it is necessary, you'd need to provide the actual classes.