Single Responsibility Principle in Clean Architecture, Aggregating UseCases in one UseCaseManager which can provide UseCase based on In & Out Object.

╄→尐↘猪︶ㄣ 提交于 2019-11-28 08:49:22

问题


I want to implement Single Responsibility principle in my projects Domain layer (Clean MVVM).

I've approx. 200 different use-cases which are being very hectic to manage. Now I'm thinking to create one UseCaseManager which can provide me required UseCase based on Input & Output Object.

I've tried an approach but that's not looking very good.I'm mentioning some sample code, Please help me how can I aggregate all the UseCases to one UseCaseManager.

UseCase1:

public class ActualUseCase1 extends AsyncUseCase<Object3,Object4> {

    public ActualUseCase1(SchedulerProvider schedulerProvider) {
        super(schedulerProvider);
    }

    @Override
    public Flowable<Object4> buildUseCaseFlowable(Object3 input) {
        return Flowable.just(new Object4());
    }
}

UseCase2:

public class ActualUseCase2 extends AsyncUseCase<Object1, Object2> {

    public ActualUseCase2(SchedulerProvider schedulerProvider) {
        super(schedulerProvider);
    }

    @Override
    public Flowable<Object2> buildUseCaseFlowable(Object1 input) {
        return Flowable.just(new Object2());
    }
}

UseCaseManager:

public interface UseCaseManager<In, Out> {
    <T> T getUseCase(In input, Out output);
}

T can be different UseCase with different In & Out Object.

UseCaseManagerImpl:

public class UseCaseManagerImpl  implements UseCaseManager {

    @Override
    public Object getUseCase(Object object1, Object object2) {
        return null;
    }
}

Now this is the main problem, I'm not able to understand. How can i implement getUseCase method.


回答1:


I think you're re-inventing the abstract factory pattern. Google will provide you with lots of content on that subject...

The tricky bit is how you decide which subtype to instantiate and return; that can be as simple as a switch statement, or involve lookup tables, etc. The key point is that you isolate that logic into a single place, where you can unit test it.

A bigger question is - how do you end up with 200 subclasses?




回答2:


Okay I am getting an idea here that you want sort of a system wherein for a given input you get some output. And you can have 200 such inputs for which 200 corresponding outputs are possible. And you want to make all that manageable.

I will try to explain the solution I have in mind. I am a beginner in Java and hence won’t be able to produce much code.

You can implement this using the Chain of Responsibility pattern. In this design pattern, you have a job runner (UseCaseManagaer in your case) and several jobs (UseCases) to run, which will be run in sequence until one of them returns an output.

You can create a RequestPipeline class which will be a job runner. In the UseCaseManager, you instantiate the pipeline once and all the use cases you want to add using a Builder Pattern like so:

RequestPipeline.add(new UseCase1())
RequestPipeline.add(new UseCase2())...

When an input comes in, you trigger the RequestPipeline which will run all the jobs added to it, one after the other in sequence. If a UseCase returns null, the job runner will call the next UseCase in line until it finds a UseCase which can manage the input and produce an answer.

The advantages of this design pattern are:

  1. Abstraction: The RequestPipeline is responsible for running the jobs in line but does not know anything about the jobs it is running. The UseCase on the other hand only knows about processing it’s own use-case. It’s a unit in itself. Hence Single Responsibility Principle is satisfied as both are independent of each other and are re-usable whenever we have a similar design requirement later.
  2. Easily extensible: If you have to add 10 other use-cases, you can easily add them to the RequestPipeline and you are done.
  3. No switch case and if-else. This in itself is a big achievement. I love Chain of Responsibility for this very reason.
  4. Declarative Programming: We simple declare what we need to do and leave the details how to do it to the separate units. The design of the code is easily understandable by a new developer.
  5. More control: RequestPipeline has the ability to dynamically decide the job to run at run-time.

Reference: https://www.google.co.in/amp/s/www.geeksforgeeks.org/chain-responsibility-design-pattern/amp/

Some Java Code is provided here for you to check, if it satisfies your use-case.

Hope this helps. Please let me know if you have any doubts in the comment section.



来源:https://stackoverflow.com/questions/50998636/single-responsibility-principle-in-clean-architecture-aggregating-usecases-in-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!