I will give a couple of examples of the implementation of the repository with the cache:

Example 1:

// две обязанности, получить данные и кэшировать class Repository { private final Cache cache; private final Api api; ........ Data getData(int arg) { if (cache.containsData(arg)) return cache.getData(data); Data data = api.getData(); cache.putData(data, arg); return data; } } 

Example 2:

 // одна обязанность, получить данные class Repository { private final Api api; .............. Data getData(int arg) { return api.getData(); } } // одна обязанность кэшировать данные // или две? получить с другого репозитория и кэшировать class CachedRepository { private final Cache cache; private final Repository next; ........ Data getData(int arg) { if (cache.containsData(arg)) return cache.getData(data); Data data = next.getData(arg); cache.putData(data, arg); return data; } } 

Help deal with porridge in my head.

  • If you are given an exhaustive answer, mark it as correct (tick the selected answer). - andreycha

1 answer 1

Yes, repository and caching are two responsibilities. However, in your second example, the caching repository is a proxy to a simple repository (we should also combine them with a common interface), and here the SRP is not broken.

Under the responsibility of SRP, Martin meant the need for a change in the class — there must be one reason for the need. In the second example, the class will have just one reason for the change - a change in the caching algorithm / method. If the repository implementation changes (changing the database, nosql, fake repository in memory for unit tests, etc.), this will not affect the caching class.


The class that organizes the interaction of the two classes (ie, the facade ) does not violate the SRP . Responsibility in this case will be the logic of interaction between the classes.

  • In the first version is also one reason. Change of concept. Because some abstract class is responsible for receiving data, and caching too. The concept of a repository is the handling of these two classes. As long as I was not steamed all this nonsense, I wrote normal applications. ))) - Alexey Malchenko
  • @Alexey Malchenko understood, if Cache and Data are abstract, and somehow transferred by dependencies to a class, then, of course, there is no violation. For some reason I thought that right there in your class, the logic of working with a data source and caching is protected. - kmv
  • Some sources say that it is vseravno several responsibilities for the class, implicitly. Or while there is no specifics, is the responsibility to coordinate the work of these 2 classes? - Alexey Malchenko
  • @Alexey Malchenko after the question changes: the second example makes sense if there are several different APIs - third-party libraries that we cannot change, and all of them need to be used. Then we unify all API in the form of repositories and we use. If the API is already abstract and is not tied to a specific data source, then the first example does not differ from the second one. - kmv
  • one
    @ Alexey Malchenko about the coordination of the work of two classes is an interesting question. They write here that this is not what violates the SRP, since only coordination will be the responsibility. - kmv