Share This Article
The Mediator Pattern in Ruby allows communication between two or more classes by encapsulating how these classes interact.
This means that you should use a mediator class when you have a group of classes that need to interact, and you want to remove the dependency between those classes.
For example, if you have several classes that need to request a server and process the response, you don’t want those classes to be aware of each other or to have a dependency between them.
You can use a mediator class to coordinate the communication between those classes.
In this article, you will learn how to implement the mediator pattern in Ruby.
The Problem With Dependencies
Let’s say you have some classes that need to make a request to a server.
# The request class makes a request to a server
class Request
def process
# makes a request to a server
end
end
# The response class processes the response from the server
class Response
def process
# processes the response from the server
end
end
# The client class makes a request to the server
# and processes the response
class Client
def initialize
@request = Request.new
@response = Response.new
end
def make_request
@request.process
@response.process
end
end
# The server class receives a request from the client
# and responds to it
class Server
def initialize
@client = Client.new
end
def receive_request
@client.make_request
end
end
# The main program
server = Server.new
server.receive_request
The server and client classes are tightly coupled and can’t be reused without each other.
Implementing the Mediator Pattern
To solve this problem, you can use the mediator pattern.
In this example, you will use the mediator to decouple the server and client classes.
# The request class makes a request to a server
class Request
def process
# makes a request to a server
end
end
# The response class processes the response from the server
class Response
def process
# processes the response from the server
end
end
# The mediator class coordinates the communication between
# the server & client classes
class Mediator
def send_request
# sends a request to the server
end
def receive_response
# receives a response from the server
end
end
# The client class makes a request to the server
# and processes the response
class Client
def initialize
@mediator = Mediator.new
@request = Request.new
@response = Response.new
end
def make_request
@mediator.send_request
@request.process
@mediator.receive_response
@response.process
end
end
# The server class receives a request from the client
# and responds to it
class Server
def initialize
@mediator = Mediator.new
@request = Request.new
end
def receive_request
@mediator.send_request
@request.process
end
def send_response
@mediator.receive_response
end
end
# The main program
server = Server.new
server.receive_request
server.send_response
You’ve used the mediator to decouple the server and client classes in this example.
With this approach, you can reuse the server and client classes without each other.
You’ve also used the mediator to encapsulate how the server and client classes interact.
Implementing the Mediator Pattern
A mediator pattern is a useful tool for decoupling the components of your application.
It can also help you to write more readable and maintainable code.
You’ve learned how to implement the mediator pattern in Ruby.
Now you can use this pattern to decouple the components of your application and write more readable and maintainable code.