- Overview
- Transcript
3.7 Facade
When integrating systems, you'll sometimes find that what should be a simple operation is actually quite complex, with a number of different steps. The facade pattern is here to help, by abstracting all of that complexity behind an easy-to-use interface.
1.Introduction2 lessons, 04:27
1.1Introduction01:39
1.2Prerequisites02:48
2.Creational Patterns5 lessons, 52:47
2.1Factory10:16
2.2Abstract Factory12:24
2.3Singleton09:09
2.4Prototype09:18
2.5Builder11:40
3.Structural Patterns7 lessons, 1:05:54
3.1Adapter10:07
3.2Flyweight10:33
3.3Proxy05:53
3.4Bridge10:35
3.5Decorator11:44
3.6Composite09:43
3.7Facade07:19
4.Behavioral Patterns9 lessons, 1:25:42
4.1Iterator09:32
4.2Command07:48
4.3Chain of Responsibility13:47
4.4Mediator08:16
4.5Memento08:53
4.6Interpreter14:31
4.7Observer08:58
4.8Strategy07:36
4.9State06:21
5.Conclusion1 lesson, 02:42
5.1Conclusion02:42
3.7 Facade
The final structural design pattern that i wanna spend a little bit of time talking about is a very important one.and it's known as the facade pattern. Now the facade pattern, the idea is very simplistic but I tell you from experience that the consumers of the code that you write. If you design it in such a way to use a facade, or instruct them on how to use a facade, if it's a complex interface that you're writing, They will definitely sing your praises. Now the facade is a very simple idea, it's basically a larger version of abstraction where we have maybe multiple different systems, or services, or repositories, or operations in general that we need to complete. But instead of us having to know and have an in-depth understanding of all of these different systems and how to use them and when to use them and what to send to them. It would be nice if we could just create a unified experience in front of it that made all of this complexity seem very simple, and that's where the facade pattern comes in. So let's go ahead and create a facade patterned playground, and now what I want to do to give you a little bit of an example is I'm going to drop some code in here. And this is going to look a little bit overwhelming and to be completely honest with you, it really is. So let's imagine for a moment that we're creating an e-commerce site, or something where we have to handle shipments and orders and all that sort of stuff. So the problem here is that, if we've done a good job designing our system, then we have separated the concern of all these different operations into their own components. So, because of that, we have an inventory system, it can check inventory, reserving the inventory during the checkout process, and it can remove inventory after a successful sale. Then we have a shipping system that can validate addresses, it can ship orders. We have a cost calculator because ultimately, we need to know how much it's gonna cost the end customer to place this order, then we have a billing system so we can charge that person. And then ultimately we have an email system, or maybe a receipt system, that can send verification and notification to that customer when a successful order was placed. And these are all all great things, and if we have done a good job in separating these things out, it should be very, very clean. But the problem is there's just a lot of it, and all I really want to do is I want to place an order, and placing an order is a very simple idea. But behind the scenes there are an awful lot of things that have to take place, so a facade is something that we can place in front of all this complexity. So that from the outside world, it all looks a little bit simpler, so let's start by kind of taking a look at all the things we're dealing with here. We're dealing with product IDs, we're dealing with quantities, more product IDs and quantities and order IDs, more product ID. So it looks like we're dealing with a lot of product IDs and quantities for the most part. And then we have some basic address information, so we've got some order information or some things that we're trying to do, or some sort of product we're trying to order. Then we have some address information, and it all kind of boils down to those two things. So what we're gonna do to begin the facade, is we want to encapsulate all of that information into some simple objects that the outside world can deal with. So let's go ahead and drop some of that in here, so as you can see I've created a of couple very simplistic classes, one to represent an address and one to represent an order. And it's got all the pieces of the puzzle that I need, street, city, state, postal code for the address, and then for the order, we have a customer ID so we know who's ordering it. We have an address which is going to use our address class, the product ID, the quantity, and then the item cost cuz obviously prices can change, we can have sales, all that sort of stuff. And just to keep things simple right now, you can only order a single product at a time, and you can order multiple quantities of that product but you can only have one type of product. So obviously you can extend this a little bit to make it a little bit more robust, but you'll get the picture. So here now we're starting to create this idea of a facade, so basically after this point is where our facade is starting to happen. We're starting to build all of these things to make our experience into all these systems much simpler and much easier for the outside world to use. Now to continue saving you the time in watching me type, I'm going to drop an example facade in here, now obviously you can design this any way that you want, but I'm gonna paste some code in here. So now I've created this order facade that's going to kind of hide all of that complexity behind a simple interface. So as you can see in my order facade, I have all of these systems, I have all of these systems that I need to place orders or do whatever I need to do with respect to orders. Now, all I wanna do from the outside world is place an order, so I'm gonna create a simple interface, a simple function here called, placeOrder and it's gonna take in an order. So it's going to take in this class structure that I've built in here to kind of simplify and encapsulate all of this data, and then finally inside place order, I'm gonna handle everything that I need to handle. I'm gonna generate an order ID because every order needs a unique ID, and then I'm gonna use all those different systems. I'm gonna check for inventory, I'm gonna reserve inventory, I'm gonna validate the address, I'm gonna calculate the total cost, then I'm going to attempt to charge the customer. I'm gonna remove that inventory, I will initiate the shipment to that address, and then finally I'm gonna send a receipt. So all of those things which in any of themselves may not be complicated activities but when you put all those things together, things get very complex. So if we can wrap all of those things and place them behind a facade, it will make the consumer's life much, much easier. Now we're very simply going to create a couple of objects, let's create a couple instances of our address and of our order. So as you can see here, we are dealing with an address in 123 Main Street in Los Angeles, California and we're creating an order. And for a particular customer for that address for a standard product ID, for certain quantity and item cost. So then all I need to do now in order to place that order, is I need to create an instance of my order facade, just like that, and then I can simply say order facade place order. And I'm going to pass in my order, and if everything works correctly, at the tail end we should all the different operations happening down at the bottom of the screen. So as you can see, we checked for a quantity of two of product 1, 2, 3, 4, 5, 6, 7, 8, 9, we reserved the quantity for that, for order we generated an order ID. We validated the address, we calculated the total cost of $27.18, and we successfully charged the customer, we remove that quantity of that product from inventory. We shipped the order, and then finally, we sent the customer a receipt for a total of $27.18. So like I said before, the concept of a facade is not really a new concept if you've done any sort of object orient programming in the past. But what it does is it kind of extends that idea of abstraction and encapsulates a lot of complex functionality from different systems, all behind a very simple interface.







