Assume we had a User
class which should hold references for all different kinds of request, let's say at the moment we only have 2 :RequestA
and RequestB
, both inherit from the base class Request
.
Now we are told to create a class representing the new kind of request in our business. We then create a RequestC
class that inherits from Request
as well (and this cycle can goes on and on).
How should we design the relationship between User
and the different kinds of Request
s that our system has?
If we directly couple them (please ignore the poor encapsulation):
public class User
{
public RequestA RequestA;
public RequestB RequestB;
public RequestC RequestC;
// Foo calls methods from RequestA and RequestB
public void Foo();
// Boo calls methods from RequestC
public void Boo();
}
Then, to my understanding, we are forever violating the open/close principle as we will be always modifying our User
class when new kinds of Request
are created.
Another solution I can think of is to create a collection of Request
to hold all the requests there, and make downcasts to RequestA
and RequestB
inside our Foo
method and to RequestC
inside Boo
and follow this in possible new methods and/or new request classes (assuming the downcast is valid, of course).
I have the feeling that the second approach is not correct either and that there could be a proper way to design such relationship.
That being said, how should we design this relationship (between users and the different types of requests) and keep following all the principles of clean code accepted in the community?