The facade pattern has been interesting to me recently. It seems like a solution to a common Rails problem: the model junk drawer. Aka “fat models”.
The problem is that a programmer will make a method like user.subscribe which kinda sounds ‘natural’ to object-oriented, Rails ears. But thing is that the subscribe method will change some stuff in the database, maybe call mailchimp or stripe, and in systems I have worked in it will set off a marketing ETL process and some Kafka messages.
So maybe all that should be a service like SubscribeUser? That is a common pattern and is one I use a lot but still feels less natural than user.subscribe to me.
So the facade pattern to the rescue? The Wikipedia article is less than perfectly clear to me but seems to suggest that we simplify readability while hiding the larger system complexity.
So let’s keep the user.register but put all the real code in SubscribeUser.
class User def subscribe(*args, &block) SubscribeUser.call(*args, &block) end end
This way everything is where is feels natural but we can move this method out if the Rails model and into a service instead.