0

Client_1 uses --> IService_1 and thus Service1_impl

public interface IService_1
{
    void DoTask_1();
}

public class Service1_impl : IService_1
{
    private readonly IDepService_1_v1 _depService_4;
    private readonly IDepService_2_v1 _batchdepService_4;
    private readonly IDepService_3_v1 _depService_2;
    private readonly IDepService_4_v1 _depService_3;

    public Service1_impl(
              IDepService_1_v1 depService_4, 
              IDepService_2_v1 batchdepService_4, 
              IDepService_3_v1 depService_2, 
              IDepService_4_v1 depService_3)
    {
        _depService_1 = depService_1;
        _depService_2 = depService_2;
        _depService_3 = depService_3;
        _depService_4 = depService_4;
    }
    
    public void DoTask_1()
    {
      // perform some work with help of all depServices_with_v1.
    }
}

Some new functionality added for Client_2, so I developed like below.

**Client_2 uses --> IService_1_v2 and thus Service_1_v2_impl **

Service_1_v2_impl now depends on IDepService_1_v2, IDepService_2_v2, IDepService_4_v2 which follow same patter. So IDepService_1_v2 has base interface as IDepService_1_v1 and implementation of IDepService_1_v2 has a base class as implementation of IDepService_1_v1 and so on.

public interface IService_1_v2 : IService_1
{
    void DoTask_2();
}

public class Service_1_v2_impl : Service1_impl, IService_1_v2
{
    private readonly IDepService_1_v2 _depService_1;
    private readonly IDepService_2_v2 _batchdepService_2;
    private readonly IDepService_3_v1 _depService_3;
    private readonly IDepService_4_v2 _depService_4;

    public Service1_impl(
              IDepService_1_v2 depService_1, 
              IDepService_2_v2 batchdepService_2, 
              IDepService_3_v1 depService_3, 
              IDepService_4_v2 depService_4)
    {
        _depService_1 = depService_1;
        _depService_2 = depService_2;
        _depService_3 = depService_3;
        _depService_4 = depService_4;
    }
    
    public void DoTask_2()
    {
      // perform some work with help of all_depServices_with_v1.
      base.DoTask_1();
      
      // perform some work with help of all_depServices_with_v1_OR_v2.
    }
}

I got review comment - this may cause interface explosion. so whenever a new requirement/ client going to be added, new set of interfaces. better patter is builder or bridge pattern.

I read through bridge pattern and to me it's still going to have interfaces explosion.

I didn't go through any pattern while writing above code, but wondering if above code represent any pattern.

5
  • "perform some work with help of depServices_v1." Do you not use the other services or do you have other tasks for other services ? – JayZ Feb 23 at 13:09
  • @JayZ I need all Dependent services to perform task. updated comment for better clarity – PKV Feb 23 at 13:17
  • Single Responsibility Principle? – Robert Harvey Feb 23 at 13:37
  • You don't necessarily need all these interface hierarchies. You don't have to neccesarily think of these new services as new versions of old services. This seems like a good case where you should favor composition over inheritance. Instead of inheriting, consider injecting two or more services as dependencies of the client. Or introduce a small object in between that delegates to these services and controls how they cooperate. If this object also implements the top level IService interface, than that's the Composite pattern. Try to find a balance between composition & inheritance. – Filip Milovanović Feb 23 at 14:39
  • 1
    I tried to start an answer but you really haven't included the details that would be needed to do so sensibly. I think you are misusing interfaces/abstraction. Hint: if you only have one implementation of each interface, you are doing it wrong. What you need to explain here is the difference between these various types i.e. What makes IDepService_1* different from IDepService_2* and why you need different versions of these. – JimmyJames Feb 23 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.