6 Design Patterns Every Android Developer Must Know

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to a new video in this video i will tell you the six most important design patterns for android developers so make sure to listen and watch this till the end in case you don't know what a design pattern is it's basically yeah it's basically a solution a blueprint for a problem that we get over and over again in programming so they're just typical types of problems we can encounter as programmers and these design patterns are just good ways to solve these problems and i will show you the six most important ones here that you need to understand as an android developer the first pattern i will talk about here is a pattern that you will probably know but maybe you don't know that it's a design pattern and it's nothing else than the singleton pattern so in case you don't know what a singleton is that's basically just an object only a single instance exists in your entire code base so yeah you can't create multiple instances of this object in kotlin that is very easy to accomplish we just use these objects here instead of classes and then we have these functions we can put variables in here whatever and these functions variables we can just call from anywhere in our code just by writing singleton dot do something singleton dot variable and so on in kotlin this is very easy but in java it's not so in java there is no native implementation of the singleton and also if we compile this here to java code or rather decompile the bytecode of this kotlin cotton file here then we can see how this looks like in java for that we can simply go to tools kotlin show kartland bytecode and click decompile here so that's the bytecode that is generated by this class and if we decompile it then we will get the java code so you can see we just have a normal class here in java that's called singleton and this class has a static reference to itself so that is how the singleton pattern works a class contains a static reference to itself so this reference can be accessed from anywhere in your code and with this instance here so with this reference to itself we can then simply access the function do something we can access variables if we would have some and so on that is not so relevant for you if you're a kotlin developer which i hope you are but i think it's still helpful to understand how this actually works behind the scenes here and how this would be implemented in java but that is it for the singleton let's get to the second pattern i want to show you here which is the factory pattern i'm sure you have heard of that as well so for example view model factories if you have dealt with view models then you know these view model factories and they use nothing less than the factory pattern and i have created a very basic factory pattern setup here which doesn't really have a practical use here it's really hard to come up with a practical use case that's understandable without any um context of a project so this you probably wouldn't do it like this in your code but um it shows you how the factory pattern works because in the end what a factory does is it needs to decide and distinguish between similar types of objects and then create the desired one of that so we just have different types of objects and then based on some logic the factory needs to decide okay do i now create class a do i now create class b or class c and you can see that i created an enum class dialog type we have three different dialog types here on the one hand the create chat dialog delete message dialog and edit message dialog and then we have a c class that represents the classes or the objects rather for the actual dialogues then here you can see we have our dialog factory so this dialog factory now has a function create dialog that contains the logic to decide which of these dialogues it should actually create you can see it takes a dialog type which is one of these three and it returns a dialog and the logic here would be that one expression so maybe you know that with viewmodel factories that you have this one expression so depending on the class of the viewmodel you return a different viewmodel and the same it's here depending on the dialog type you return the corresponding dialog that you want to create and i actually should have replaced that here with delete message dialog and edit message dialog of course so that is how it should look like so whenever you need a handy way to create objects that could be of multiple similar types then you want to use the factory pattern next up for the third pattern i want to show you here i want to get to the builder pattern which i'm sure you also have seen before but yeah let's take a look here we have a hamburger class because um yeah that is in the end something that you can use to demonstrate the biller pattern quite well because well on a hamburger you can choose what kind of toppings you want to have so do you want cheese do you want beef onions and feel free to add even more but each of these could be optional so what we will use here is this common builder pattern that we have an inner class in this hamburger class called builder and this class then contains functions to actually assign values to to the local variables here and if we then in the end call the build function we finally create the hamburger here and one of these functions to actually assign the values they just return the builder itself if we hit ctrl q here you can see they all return a hamburger.builder to actually see how this really looks like let's head over to our main activity and you can see this is super readable here we just create a hamburger it's equal to hamburger.builder and then we can say hey we have we want to have beef on our burger we don't want to have cheese on our burger but we want to have onions on our burger and when we're done we just call that build and that will then return a hamburger instance as you can see with exactly these properties so that's just a very cool way to actually assign values to your properties of a class um a common class from android would be the material alert dialog builder you can see here we could pass the contacts and then simply call these set background functions and set title set message and so on however this is still more the java way of doing that so i'm not sure if i would really use my own builder patterns in kotlin so that i if i would really implement it like that because in kotlin we have name parameters and we have default parameters so we could just put all of all of these values here in the constructor so let's quickly do that well we can simply remove all of that make this a public constructor and probably a data class and then we could just assign default values so by default we want to have cheese we want to have beef and we don't want to have onions i mean false of course and then in our main activity we can instead of this right hamburger and here we can just assign the values to those toppings we actually want to have in our hamburger so let's say we don't want to have cheese then we just say cheese is equal to false if we want to have onions we set this to true so this is basically the the easier kotlin way of doing this but i still find that's important to understand how these builders work because even in kotlin you will deal with them all the time because yeah you also deal with java implementations then let's head over to the next pattern which you might not know yet because it's not so frequently used but it's still a very helpful one and that is the the so-called facade pattern that is probably the best if i show you that with a retrofit interface as an example because that's probably the best example to demonstrate that in an android environment so a facade pattern basically is used to hide code that you don't need to see or rather to only show code that you should see and that is exactly what what this retrofit api interface here does you only define this function to get hamburgers from your api but just by defining this interface you don't really define the the underlying functionality you don't need to care about how retrofit actually handles this how retrofit makes the actual network calls because all you really need is this function that just gives you your list of your freaking hamburgers and everything that happens behind the scenes is something you don't really need to know and you don't need to care about in your application so that is what we call a facade because you only see the facade so the the surface of things i could say next up we have something you probably have heard of as well and that is dependency injection let's head over to our app module and here i destroyed my hamburger builder but doesn't matter here the penalty injection is a design pattern every single android developer out there must know it's so important even though if you learn it you won't directly understand why it's so important but once you you get more experience and you use it more often you will realize how important dependency injection is so if you're not even close to familiar with what the penalty injection is all it really means is passing an object its instance variables so that could be something super simple like just passing some constructor parameters that is called constructor injection but usually in bigger applications we use a framework for that like a dagger and dagger will just provide us a very convenient environment to define our dependencies so the dependency is in the end just an object you want to inject into some of your your classes so you can see that could for example just be a hamburger that could be your retrofit instance your room database that you just need all over the place in your application and then you have these modules with dagger in which you define how these dependencies are created so for example how your room database is created so then dagger knows how it's created and then it can simply inject that into your classes so you can use that in your main repository in your other repository whatever wherever you need to database you can simply inject it from the outside and what's the benefit of that well it allows you to super easily swap out your dependencies so in the end all you need to do to change the whole behavior of your app is to change one of these provides functions that just define how a specific object is created and it also makes testing super easy because you can just define your own test modules in which you just have all the dependencies that you use for testing because usually for testing you have some fake dependencies you don't want to use a real api your real database instead yeah you just define some fake ones that just behave similar to the real ones and simulate the behavior and then you can just do that instead of a test module and make sure that dagger just uses these dependencies from the test module for your test cases and the dependencies from the app module for your production app so whatever you do as an android developer learn dependency injection it's super important let's get to the last design pattern here of this video which you surely know even if you're a beginner in android and that is the adapter pattern we all know recycler view adapters but there are also other types of adapters like array adapters for example for spinners and yeah even more an adapter is also nothing less than just a design pattern you can see we have a recyclerview adapter and all it's supposed to do is it should take in this case a list of of data a list of items in this case hamburgers and it should adapt it to ui or to whatever to something else in this case in android it's usually ui so it just takes a list of some kind of items and it adapts this list to actual layout items to items you can see on the screen to views and yeah that is that is very helpful you have this function on bind viewholder in which you can assign values to your views but yeah this video is not about recyclerview i'm sure you know that but it's also one of the most important design patterns here for android because you will stumble over these adapters all the time all right if you like this video then please let me know below if you want more of these videos and if you do then please also tell me what kind of videos you would like to have about having a cleaner code style and apart from that if you're also looking for more advanced android courses then you also want to check out the first link in this video's description which will lead you to my website and there you will find more advanced android premium courses which are a brilliant way to support me and my work but most importantly to to just boost your android knowledge to the next level so i wish you an excellent day and i really hope i can see you in the next video again bye bye
Info
Channel: Philipp Lackner
Views: 12,333
Rating: 4.9667459 out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile
Id: 4cCwuBsqfTI
Channel Id: undefined
Length: 14min 16sec (856 seconds)
Published: Sun Aug 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.