First of all, I don’t speak english very well. It’s the longer message I wrote in english. Please, If you see mistakes, please correct me and send me a private message.
I watched all Ruby Weekly made by @benorenstein and @jferris and I read a lot of books and articles about design pattern and applications design. Obviously, I use all those concepts when I work but I think it’s always hard to know what I should create and where to put all classes, behaviour, methods, parameters, etc…
When we learn the progamation, we learn simple example with cats, dogs and animals or with a bank account with transactions. Those examples are obvious and it’s easy to know where to put things. It’s harder to know where to place the behaviour of a complex form which use multiple services and models.
Yesterday, I thought about why is it difficult. I think it’s difficult because it’s abstract. There is no physical objects or persons. So, what is the solution? I think it’s possible to humanize an object and try to create a story with it. My idea is to tell a story with an object that will go in a well structured company. I will give you an exemple with a user registration, a POST method sent to create a new user.
Once upon a time, a person named HTTP request (A person who has a message and wants an answer), goes to the MyApplication company. He go to the reception to deliver his package and he waits for an answer. He don’t care about what the receptionist do, he only want an answer. When he have the anwser, he go back to home.
The receptionist will checks what there is in the package delivered by mister HTTP and think “Ha, it’s a message for Mr UserRegistration at the contollers department.”. He give the parameters in the package delivered and transmit the answer to mister HTTP.
Mr UserRegistration will check those parameters and says to Mister UserRegistrationForm “create something with those parameters and gives an answer”. Mr UserRegistration checks the anwser and decid “Ha, it’s valid. Mr the receptionist, with what you gave to me, you must to redirect to the user profile with this message (succesfully created).”.
In this case, it’s easy to understand what is the responsability of everyone. The receptionist (route) receive a HTTP request and sends parameters to the correct controller. The controller uses a form object and render a view and a message. All of them don’t care about what their paterners are doing. Mister UserRegistration don’t want to know what does Mister UserRegistrationForm. The form can destroy all the data if he want, if he can call and have a feedback, it’s ok for him.
In another point of view, if have a person, we can have this dialog :
- You : Hi, what’s your name
- Class : I am mister UserRegistrationForm
- You : Ok, what’s your responsability?
- Class : I know how to do all processes when a form is submited
- You : What are the list of you functions?
- Class : I only have method
- You : What is the responsability of the class call
- Class : It do all the process of a user creation
I think the keyword is AND. If there is a AND in the Class answer, you don’t respect the SRP. For example, if the router say “I receive HTTP requests and I send an email when there is an error”. Oops, this is probably a lack a design.
Those scenarios are oriented to know what is the responsabilities, but I think it can be applicable for all kind of patterns and principles. For example, with DIP, we can imagine than the person who sends a message give a tool to his partener. For example, “Hi mister PriceCharger, can you charge the order with this PriceCalculator”. PriceCharger can be like this :
class PriceCharger def intialize(order, charger) @order = order @charger = charger end def call price = charger.call(order) # do something with end end PriceCharger.new(order, PriceCalculator).call
So, it’s a longer message as I expected when I begun. What you think? Do you think it can simplify the comprehension and the creation of a design?