Factory Design Pattern in Laravel and PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi I'm Nick and I'm here to help you master building applications and PHP and laravel let's talk about the factory design pattern here we have a diagram describing what the factory design pattern is and how we can use it so in laravel we might have a job a controller or a command that'll utilize this formatter Factory this formatter Factory will have a function on it called get formatter which in turn will return a four matter interface now this interface has two concrete implementations one for formatting an array to Json another to format to XML so let's go ahead and start building out what this looks like and the first thing that I'm going to do is build the console command that we're going to use to just output stuff to the console to so that we know that what if it's working or not so PHP has a command to make console commands and that is PHP artisan make command and we're going to do array formatter command and this console command is going to say format and then we need to pass in the format that we want to use to format this array so we're just going to call this formatter and then let's change the description so that we know what it is so format and array to the specified formatting type and then down here is where we're going to do all the magic so now in order to do anything in here we need to First create the factory the interface and then the two concrete implementations that are going to implement the interface so let's go ahead and create that interface I'm going to go ahead and I'm going to create a folder called Factory pattern and in here we need to create an interface so this interface we're going to call formatter and for this particular formatter when we start to talk about the different types of formatting when we say we're going to format to Json and we're going to format to XML the one thing that they will all have in common is they will all need a format function and in that format function they're each going to take an array of data and they're all going to return a string so again depending on you know the data type that you're going to be formatting to these are all the same so if you're going to be formatting to Json you still need the data in order to format to the Json string and likewise you need that for XML as well so regardless of the format that's being um returned we still have this that should be identical among all the formatters which is why we're creating this interface so let's say we want to format Json from this array data well we need to create that concrete implementation that's going to implement this interface so we have our Json formatter and we're going to say it implements this formatter and so this function was brought over from the interface formatter this one's super simple we just need to return Json encode data and we already know that the Json encode function is going to return a string the next one that we have to do is XML and this one's also going to implement the same interface again this is the most important part here is that both the XML formatter and the Json formatter are implementing the same interface this is going to be very important when we get to the actual part where it's we're using the factory so for this we're going to say we have XML and this expects any any opening tag here which is just going to be the start and then what we want to do is we want to walk the entire right now this this is just meant to show you kind of the difference between having multiple formatters in different ways there are plenty of ways that we could format this XML into a string this is just one way that I'm that I'm choosing to do that so the first thing that we're going to need is we're going to need the data and then we're going to use another array and we're going to say XML and then add child and then we're going to return XML as XML so let's go through this really quick so we're creating a new simple XML element this is how we can do this in PHP and then over the entire data array we're calling ADD child on this simple XML element this is just how PHP allows us to call a function on this XML variable for each piece for each item in this data array and then this is how we can return a string from the XML this is how PHP will convert the XML into a string for us okay so now we've got the formatter interface and we've got the two different types of formatting that we're going to use now we need to create the actual Factory that's going to return these items when we need them so now we need to create the actual Factory class that's going to return one of these concrete implementations whether it's Json or XML to us when we ask for it given a specific formatting type so let's go ahead and create that class now that class is going to be called formatter Factory this is going to have one function called get formatter and it's gonna ask for a string and this is going to be the type that you want to convert the data to so we're going to say formatter and then another important detail is that we're going to guarantee that this function is always going to return the formatter interface so no matter which implementation is getting back it needs to return this formatter again this formatter has this one function on it called format and in this get formatter we're gonna we're gonna use the match function this match function is going to be matched on the formatter and what we can do is if somebody gives Json as the formatter we can return a new Json formatter somebody gives XML we can simply do XML formatter and if somebody gives something to us that we don't know how to process we can provide a default and throw a new invalid argument exception okay so to recap on this function we're going to be able to call this on this formatter Factory this is the actual Factory design pattern right here it says that when we have a factory we're able to give a particular type and in that type we're guaranteed this interface and in this interface we can have one of these two implementations and if it doesn't get one of those two implementations it's going to throw an exception so now we need to actually implement this in our Command so if we go back to our Command and remember we're going to be passing in the formatter in the command so what we can do is we can let's new up a new formatter Factory so if we go formatter Factory equals new formatter Factory and then if we do string output equals new formatter Factory now remember that formatter factory has one function called get formatter that get formatter expects a string and that string is remember XML or Json so what we can do is this argument for matter that's getting the argument from right here that the person's passing in from the command line now we can use this info and display the output so let's go ahead and give that a try but first remember get formatter is returning this formatter interface so we actually need to call format on the interface so this is actually the formatter here and this is the formatter and we can call format and then we need to give it some data so we can say data one data let's go like this so it's easier to read data one data to and then let's provide a key pair so a key value and so remember this is the data that's going to be formatted and because when we call get formatter we're guaranteed that interface and that interface has the format function on it and then on that format function it's expecting an array and then we can just call this info because we know this format function is guaranteed to return a string so let's try this command out it is PHP Artisan formats and then if we give Json it gives us that array data 1 data 2 key value as Json data 1 data 2 key value let's try that same thing with XML here's data one data two and then value in the key and this is just how the PHP interpret interprets the key value pair for the simple XML element let's say we gave it something that doesn't exist like um something else now this is the important part that we're throwing this invalid argument exception because we always want to guarantee that the return type will be satisfied otherwise PHP will throw an error and we don't have a formatter for this we could by default if we wanted to we could return Json or XML that's also another option to use there so again let's recap the flow of this in our console command now this could be a console command this could be an event this could be a controller this could be anywhere in the application we're going to new up a factory we're going to call the get formatter on the factory we're going to give it a string and that string is going to give us back one of those two concrete implementations that we can then use to do whatever we want and we're guaranteed to have the interface back and then since we have the interface we can call format on the interface the job controller the command calls the formatter factory the formatter Factory's given us the formatter interface and then again we've got these two concrete implementations that do the same exact thing but in their own respective ways which is important because this console command should never really know how to format this array if we wanted to add another formatting type we can do so without touching this console command this is a very important concept because what this allows us to do is only specific parts of the code base need to be changed and they're only changed for one specific reason this starts to get into the whole solid design principles and because of that it makes our code a lot less risky to update and then this also allows us to write tests easier so if we wanted to write a test for the Json formatter well we could simply just pass in an array and then match the output of the string with what we expect and the same thing with XML formatter but we don't have to new up any other object we don't have the new up a factory we don't have to new up the console command to do this you know a lot of people get stuck in putting all of the meat and potatoes in their console commands in their event listeners in their controllers this is a way we can completely decouple how we interact with these types of things with our application making updates maintaining and testing easier thanks for watching this video I hope it was enjoyable and easy to follow I look forward to making more videos in the future if you could help me out by liking subscribing on this video and I'll continue to make more
Info
Channel: EscoTech
Views: 6,974
Rating: undefined out of 5
Keywords: factory design pattern, php factory design pattern, laravel design pattern, design patterns in php
Id: cCRZGBQH9o4
Channel Id: undefined
Length: 16min 17sec (977 seconds)
Published: Sat Oct 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.