Source: blog.mollie.com

Object Encapsulation
Once upon a time, we had just finished lunch and, as usual, I was heading to prepare a cappuccino at the office coffee machine, running away from Pietro (our Italian mate), I could hear him saying “Hey… Well, this day Albert and Robin were putting on their jackets and Albert invited me to have a coffee outside the office, a slightly different one, made of coconut milk.

Same story, our goal was to have a coffee and we achieved it, we had the coffee, we paid, and we got back to work.

Now let’s write some code to represent this process: As we can see in the second example, now the wallet balance is not exposed and it guarantees the right amount is being deducted, also now we have the Customer playing its role, in this way the barista will not have direct access to the wallet, and the customer could have some additional logic in it, for example, transferring money from saving to checking account or running away as fast as possible ¯\_(ツ)_/¯

We can very easily change and add new business rules to it, for instance: support multi-currencies, or discount/cashback policy, or we can publish an event and have other handlers listening to it.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners