the factory is a design pattern that i use really often when i write software today i'm going to show you an example of how it works and how you can use it to separate creation from use which is really powerful before we dive in i have something for you i wrote down the seven main things that i think about when designing a new feature or new software application you can get it for free at URL_TOKEN design guide it's pretty short so pdf contains a couple of bullet points some notes from me here and there i think you'll find it really useful so get it at URL_TOKEN design guide now let's dive into the factory pattern what we have here is a video and audio exporting example as you can see i did a little bit of preparation work i created a video exporter abstract class that's a basic representation of a video exporting codec and that has two methods prepare export and do the export prepare export gets the actual video data and do export gets a folder i'm using the URL_TOKEN type here to represent paths and that exports the video data to folder at least that's the idea and then we have several specific implementations for this class there is lossless video exporter that export data in lossless format there's also an hNUMBER_TOKEN video exporting codec with baseline profile so that's basically an economic mode lower quality and finally there is a higher quality hNUMBER_TOKEN export format with four to two chroma sampling and NUMBER_TOKEN bit colors and stuff like that it doesn't really matter for this example but it's a higher quality one and a similar thing i did for the audio exporter we have an audio exporter abstract class that has prepare export and duty export but that gets audio data but do export also gets a folder so it looks a bit similar and then there is an aac audio exporting codec and we have a wave audio exporter which is lossless so again also here we have kind of a lower economic quality and a high quality that you would use for masters then there is a main function that first reads the export quality that the user wants so we ask the user whether they want low high or master and then depending on that value that you got we're going to create these specific exporters so we start with these variables video and audio exporter if the quality is low we're creating the base profile hNUMBER_TOKEN video exporter and the aac audio exporter if the quality is high we're using the high quality profile for video and also aac if it's master quality then we're going for lossless video and lossless audio in the form of a wav file so the choice of the kind of exports that we want to use is here then we call the function we prepare the export and we do the actual export to a particular folder so let's run this code and see what happens so now i can enter a desired output quality like if i choose low then it's going to export with the hNUMBER_TOKEN baseline export and ac export and if i run this again and i choose for example master quality then i'm getting the video and audio export in master quality now if you look at this code you see that there's in particular problem with the main function in that it has way too many responsibilities it's responsible for asking the user which quality they want it's responsible for then mapping that desired quality to actual objects
START_SELFPROMO_TOKEN we dive in i have something for you i wrote down the seven main things that i think about when designing a new feature or new software application you can get it for free at URL_TOKEN design guide it's pretty short so pdf contains a couple of bullet points some notes from me here and there i think you'll find it really useful so get it at URL_TOKEN design guide now let's dive into the factory END_SELFPROMO_TOKEN