Brendon Cheung
Brendon Cheung

Reputation: 1035

Is "one method per class" overdoing the Single Responsibility Principle?

I'm building a simple todo list application on android because I want to get myself familiar with the clean architecture. I layered the application with domain, data and presentation layer, and here is the example i'm following: https://github.com/android10/Android-CleanArchitecture

When I tried to figure out what is the domain for this application, I asked myself "what is this application about?". To which I reply, "It is about letting user create a group and create tasks within that group", very simple,

So I created the following:

Adding groups to Room database

public class AddGroupItemUseCase extends AbstractUseCaseCompletable<AddGroupItemUseCase.Params> {

    private final GroupItemRepository mGroupItemRepository;

    public AddGroupItemUseCase(GroupItemRepository repository,
                               PostExecutionThread postExecutionThread,
                               ThreadExecution threadExecution) {
        super(threadExecution, postExecutionThread);
        mGroupItemRepository = repository;
}

@Override
public Completable buildUseCaseCompletable(Params params) {
    return mGroupItemRepository.addItemToGroup(params.mItem);
}


public static final class Params {
    private final Item mItem;
    private Params(Item item) {
        mItem = item;
    }
    public static Params itemToBeAdded(Item item) {
        return new Params(item);
    }
}

}

Adding tasks to a group in Room database:

public class AddGroupUseCase extends AbstractUseCaseCompletable<AddGroupUseCase.Params> {

    private final GroupRepository mGroupRepository;

    public AddGroupUseCase(ThreadExecution threadExecution,
                           PostExecutionThread postExecutionThread,
                           GroupRepository repository) {
        super(threadExecution, postExecutionThread);
        mGroupRepository = repository;

    }

    @Override
    public Completable buildUseCaseCompletable(Params params) {
        return mGroupRepository.addGroup(params.mGroup);
    }

    public static final class Params {

        private final Group mGroup;

        private Params(Group group) {
            mGroup = group;
        }

        public static AddGroupUseCase.Params groupToAdd(Group group) {
            return new AddGroupUseCase.Params(group);
        }
    }
}

So, an obvious question arises, do I have to create these one class one method classes for every crud operation? For example, what if I want to get know how many tasks are in a group? do I have to create a class with that method in order to comply with the clean architecture? feels like a lot of classes need to be created, but I guess it make sense because of SRP but then you would have a lot of "functional classes" you need to keep up with,

any thoughts? thank you!

Upvotes: 1

Views: 329

Answers (1)

Duy Anh Pham
Duy Anh Pham

Reputation: 76

Yes! You should not have "one class one method".

Responsibility in SRP doesn't mean doing just one single task, it means holding all responsibility in single domain. So doing everything within a single concern, which is not overlapping with another class. You can have one class to do everything with "groups", and one class to do everything with "tasks". This is how things are normally organized.

From Wikipedia:

The single-responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. ... The reason it is important to keep a class focused on a single concern is that it makes the class more robust.

Upvotes: 1

Related Questions