I have two classes, Repository and Search. Repository encapsulates the logic for storing and managing the content, and Search encapsulates the logic for searching through the content and also requires access to the Repository object, in order to read the content.
Generally, I want to divide the functionality of Repository class into public, that every user of the class will have access to, and private, which should only be available internally. The problem I have is that Search class should have access to the private functionality of Repository but because the Repository object will passed to Search object from the outside, it will only expose public functionality.
The way I am doing it right now, is that Repository uses pimpl idiom, to hide implementation details, and I declare Search as friend, so it can use the private functionality in implementation code.
Repository.h:
// Forward declaration of implementation class
class RepositoryImpl;
// Forward declaration of friend class
class Search;
class Repository
{
public:
// public interface
private:
RepositoryImpl* pImpl;
};
Search.h
class Search
{
public:
void searchRepository(Repository* repo);
};
Search.cpp
void Search::searchRepository(Repository* repo)
{
repo->pImpl->someHiddenMethod();
}
This obviously is just to give and idea so it does not make any sense in terms of actual functionality but I hope it explains well what I am struggling with. Some might say that using friend here is perfectly fine, but to me it feels a bit "hacky" so if there is a better way (some design pattern) to go about it, I would like to use.
I also don't want to merge those classes into one (even though it might seem like a good idea) because it introduces certain limitations that are not acceptable. Basically I want to be able to have one search engine, being able to work with multiple repositories. I am open though to complete redesign of the classes if it will allow to achieve the goal in clean manner.
Thank you very much in advance.