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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners