1

A class whose purpose is to provide data (let's say it provide Students) and client code would look like this:

students = StudentProvider.getAllStudents()

Its underlying implementation will encapsulate the retrieval of Students from a remote server with an instance of RemoteStudentRepository, and then save it locally using an instance of LocalStudentRepository:

// StudentProvider#getAllStudents()
students = remoteStudentRepo.GetAll((students) => {
   // Callback with a list of students as a parameter

   for student in students {
       localStudentRepo.updateOrInsert(student);
   }
});

return students;

I know Facade is structural and Mediator is behavioural, so it makes me think this is a Mediator example because it encapsulates the logic of how object interacts (remote fetch and local storage), but I'm not so sure if this "added functionality" is enough to call it a Mediator.

So what is it?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206

1 Answers1

1

I would call it neither a Facade nor a Mediator. It perhaps technically has a singular ingredient which you would find in the former one. However, do not be inductively tempted to call it a design pattern, as it does not solve you a related problem. Right now it is just a class coupled to another one.

Facade provides a unified interface to subsystem's interfaces. In your example "StudentProvider" is not necessarily intended to eliminate the use of "remoteStudentRepo". And also if it did, this is not a complicated subsystem, and there are not multiple interfaces to be unified.

Mediator keeps objects referring to each other explicitly. It helps to decouple components but still lets them work together. Once again, not the same thing in our case.

If it does not solve the same problems, it is not the same pattern.


By the way, @MikeSW is right. Your example violates the command query separation (CQS) principle. A method should be either a query or a command which mutates state. Queries are idempotent.

TechWisdom
  • 3,960
  • 4
  • 33
  • 40