Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.4 Facade

The facade is probably the most commonly used pattern. You've undoubtedly implemented it already, maybe without even knowing it!

1 lesson, 01:40


2.Creational Patterns
2 lessons, 11:52



3.Structural Patterns
4 lessons, 33:47





4.Behavior Patterns
4 lessons, 36:32

Chain of Responsibility




1 lesson, 01:06


3.4 Facade

The facade pattern is probably the most used pattern in software development, because, we use it to hide the complexity of a series of processes, in order to make our client code a lot simpler to use. So for example let's think about placing an order on an online store front. Well, the first thing that need to happen, is the inventory system needs to ensure that there is an item in the inventory. So the first step in this whole process is checking the inventory. The second thing is, if there is an item in inventory, then the user needs to pay for that item, so, user pays. And then finally, there is the shipping. So if there is an item in the inventory, if the user paid, then we need to ship the item. Now of course, this is a gross oversimplification, but this is a pretty good example of the facade pattern. What we want to do is take these individual steps, and then just make it so that we call a single method. To place an order. So, the first thing we're going to do is to create a class for our inventory. In fact, we'll go ahead and create classes for all of our other parts of this process. So we have the inventory, we need the payment gateway. Which is what the user will use to make their payment. We need a class for representing our shipping, so let's just call this ShippingService. And then we need a class for the order, or placing the order. And the order is going to be our facade, so we could say Facade, but I really don't like using the name of the pattern in the class name. So I'm just gonna say that this is the Facade. So as far as our other classes are concerned, we're just gonna have a single method for performing their intended processes. Like for our inventory, we are just going to have a function called checkInventory and we need to know the item that we want to check, so we're going to have a string item ID. And then we are just going to echo that we are checking the inventory for item, and then our $itemId. And let's also add a line break there, and we could have an item in the inventory or we couldn't. So in order to add some dynamicism to this, we could return a random Boolean value. But, in order for everything to line up, we would have to return true for checkInventory, we would have to return true for the PaymentGateway, because the user might not actually pay for the item, in which case this whole process breaks down. So, for the sake of making everything work so that we don't have to run this file multiple times in order to get the magic Boolean values that we need, we're just going to return true here. So we're going to say that the amount being passed is a float, and then we will simply echo Paying for item with $amount. And let's also add a line break there, and then we will return true as well. So then as far as our shipping is concerned, we really only need to know what the item is that we are going to ship. So we are going to just call this ship item and we will accept a string item ID that matches up the item ID for our inventory class, and then we will just echo another statement that says shipping item, and then the item ID. So, now when it comes to our order, we want to take each one of these classes and use them in order to actually place an order. So, we need references to the inventory, to the payment gateway, and we also need the shipping. So let's just call this shipping. And then, we will have a public constructor. And ideally, we would accept the the inventory, the payments, and the shipping services here. But, we're just going to take the easy way out, and just new them up inside of the constructor, so that we will say this inventory is equal to a new Inventory. We also want the payments equal to new paymentGateway. And then finally, the shipping is equal to new shippingService. So those are our three services that we would use in order to make an order. So let's go ahead and write that function. We'll just call this method placeOrder. We need two things. We need the itemId. And then, we also need to know the amount of money that the user is going to pay. We are just going to simply check to see if there is an item in the inventory, so we will do that, inventory. And then checkInventory, we will pass in our itemId. But we can go ahead and check to see if the user has paid as well because if this is false that's just going to short circuit everything out so that the payment isn't even going to be done. So now this payments and then we will call the pay method, we will pass in the amount. And if both of these are true, then we will ship the item. So we will say shipping, and then shipItem. We'll pass in the itemId and we are good to go. So as far as our client code is concerned, this is all that we would need to do. We would new up our Order. And then we would simply say, order placeOrder. And then we would need our itemId, which is just going to be a set of alpha numeric characters. And then the amount. Let's make this 150. So if we hop on over to the console, we can say php. This is the facade, and there we go. We have checking the inventory for the item. We are paying for the item, and then we are shipping the item. So the whole idea behind the facade pattern, is so that we can take a complex series of processes, and hide them within a single class. And in this case, a single method on that class. And this is something that we use on a daily basis, anytime that we are writing our applications, and especially refactoring our applications. The facade pattern is one that we employ all the time.

Back to the top