Design Patterns in life and Ruby — gain an intuitive understanding of OO design patterns by linking them with real-life examples.
The Facade Pattern is about making complicated things simple.
The Facade Pattern:
- provides a unified interface to a set of interfaces in a subsystem.
- defines a higher-level interface that makes the subsystem easier to use.
You will know exactly what the definition means after we do some shopping on Amazon.
Shopping on Amazon is similar to shopping anywhere else online.
You first add an item to your shopping cart.
You then proceed to the checkout process, which has four steps:
- Enter Shipping Address
- Enter Payment Method
- Review Items and Shipping
- Place Order
Here is what the code will look like:
class CheckoutProcess attr_reader :order def initialize(order) @order = order end def set_shipping_address(address) order.shipping_address = address end def set_payment_method(payment_method) order.payment_method = payment_method end def review_order puts "Order contains: #{order.items}" puts "Shipping Address is #{order.shipping_address}" puts "Payment Method is #{order.payment_method}" end def place_order order.confirm end end
And the checkout process will look like:
The checkout experience is not ideal for people who shop on Amazon a lot. They have to go through all four steps each time they buy something.
1-Click Ordering
Luckily, Amazon has a 1-Click Ordering feature that shortens the entire checkout process from four steps to 1-Click.
One single click will place the will place the order with your default shipping address and default payment method.
Since we already have the CheckoutProcess
class and we want to keep the regular checkout process as an option, we can reuse methods in CheckoutProcess
for the 1-Click Ordering:
class OneClickCheckout attr_reader :checkout_process, :default_address, :default_payment def initialize(order, default_address, default_payment) @checkout_process = CheckoutProcess.new(order) @default_address = default_address @default_payment = default_payment end def click @checkout_process.set_shipping_address(default_address) @checkout_process.set_payment_method(default_payment) @checkout_process.place_order end end
With OneClickCheckout
, placing an order is only a matter of a click
.
From a user perspective, all I need to do is click the “Buy now with 1-Click” button. And the 1-Click will handle all the necessary steps to place an order for me.
Facade in the light of 1-Click Ordering
The Facade Pattern
- provides a unified interface to a set of interfaces in a subsystem.
- Facade defines a higher-level interface that makes the subsystem easier to use.
In our 1-Click Ordering example,
- the unified interface is the
OneClickCheckout
, - the set of interfaces includes
set_shipping_address
,set_payment_method
,place_order
, - the subsystem is `CheckoutProcess`
The higher-level interface, OneClickCheckout
, makes ordering and checking out much easier: four steps turn into 1-Click.
Takeaways
The Facade Pattern:
- provides a unified interface to a set of interfaces in a subsystem.
- defines a higher-level interface that makes the subsystem easier to use.
Don't forget to subscribe so you won't miss the next post. ?
Enjoyed the article?
My best content on Software Design, Rails, and Career in Dev. Delivered weekly.
Next time we will take a look at some movies.