Strategy Design Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part 3 of my design patterns video tutorial today I'm gonna do something a little bit different for this tutorial this should be self-contained 100% just on the strategy pattern unless you do not understand basic olp concepts if that's true refer to part 1 and 2 of these tutorials but from this point on all of these tutorials will be 100% self-contained and to teach you the strategy pattern I'm gonna write bad code so you can see in what situations you would need the strategy pattern I'm also going to demonstrate it in code format just like always and then I'm also going to explain it using a presentation as well as a UML diagram and like always all of the code is heavily commented and this is another great way to learn this concept download the code and read the comments it's free so why not so let's get into it okay so we don't emm over here we have a couple classes to find we have an animal dot Java and then we have two subclasses underneath of it dog Java and bird Java and what we're trying to try to do here is figure out how we can give bird Java and other animals that are subclasses of animal dot Java the capability to fly now this is where the bad code part comes in because I'm gonna come in here and just think about how we could accomplish this using techniques that we've already learned well one thing I could do is just come in here and go public void fly and create a method inside of animal dot Java and then have it do something like I am flying print that out on the screen and there you go well why is this bad well you never want to add any methods to a superclass like animal dot Java if they do not pertain to any subclasses you need to separate what is different between subclasses and their superclass but your answer to that might be like oh you're being silly because inside of dog I can just simply come in here and override just like we've always done in the past and just create my own fly method and everything is going to be dandy and you could do that but again we're learning rules here on how to write good code we need to continue to abstract out what is different and put just those things that are different inside of the classes however that would not make sense to come in there and just put it in the burgh java class because in that situation because we would be creating a lot of duplicate code if we expect a lot of our animals to be able to fly so we would go in there and continuously every time we created a new animal and it just so happened to fly we would go in and add in the method fly over and over and over again so we also want to avoid that issue let's go back over here and let's delete this because that's just wrong don't want it that and you're also gonna find if you would come in here and create an interface let's say called flies that would then force all animals to figure out exactly how they're gonna use the fly method that is also going to create a massive amount of duplicate code to basic principles that you really have to get into your head is you want to always eliminate duplicate code and you also want to eliminate any techniques that cause one class to affect others a superclass change shouldn't break code in a subclass and vice versa if there is a situation on which that seems to be happening you definitely want to rectify that however using the strategy pattern we are going to use an interface but we're gonna use it in a completely different way in this situation I'm going to create this interface flies just like we did before except in this situation it's going to return a string that's not really important so don't really focus on that what is important is we are going to create separate classes it flies which is going to implement the Flies interface and then in this situation because it's going to implement that we're gonna go add on the implemented methods and get rid of all this extra nonsense we don't need and then in this situation we're gonna go into return and I'm going to type something like flying high and then I'm gonna create another class so I'm just gonna copy this and it is also going to implement the Flies interface except it's gonna be called can't fly yes I know that's spelled incorrectly and then here we're just going to change this to return I can't fly and there you go so now what we did was we created an interface and we're then going to use this interface inside of animal as an instance variable and then we're going to dynamically change that instance variable to be either of type class it flies or class can't fly and why this is a great idea is the interface is implemented by many other different subclasses and what this is going to allow us to do is create many different types of flying without affecting animal or any of the subclasses and then from that point on classes that implement this new fly interface are going to allow those classes to use that code while also eliminating code duplication and this is known this is a technical term as decoupling which means that we are encapsulating the concept or the behavior that varies and that behavior or concept is the capability to fly you're not quite getting everything don't worry about it I'm gonna show that to you in multiple different ways so let's jump over into animal and implement this and it's gonna be quite easy to implement remember we're just gonna create a new instance variable I'm gonna make it public I'm gonna call it flies and it's going to be of type flies and I'm just gonna go flying height like that so instead of using an interface in a traditional way we use instance variable that is a subclass of the flies interface an animal doesn't care what flying type does it just knows the behavior is available to all of its subclasses and this is also known as composition instead of inheriting an ability through inheritance the class is composed with objects with the right ability built-in and another great thing about composition is it allows you to change the capabilities of objects at runtime so if you create an object an animal object and it starts off as a non flying object but then it grows a set of wings and all of a sudden can fly dynamically you can go in and say okay I know this object didn't fly before but now it does so it gives you a never-ending supply of different capabilities so now that we've created this guy we're gonna be able to just zoom down inside of animal java and type in public string try to fly like this and then it's going to return flying tight and it's going to call the fly method right like that and then another thing we're gonna make this all dynamic which of course we want to if possible we're gonna go public void set flying ability and it's going to be passed a fly's object which is gonna be new fly type and then we can set flying-type dynamically new fly type by setting it to that object so let's save that so now what are we gonna have to do with dog Java and bird dot Java to make those work actually very little I'm just gonna go into the constructor area whenever our dog object is built and we're gonna say flying type is equal to new and in this situation we're gonna call can't fly this is gonna set the fly interface polymorphically and this is also of course going to set it as a non flying animal and by polymorphically of course i just mean we're going to refer to the flies interface but we're going to set it to use can't fly they can't fly class underneath of interface that's it that's all we need to do with dog to give it the capability to not fly and you might say to yourself well the bird must be really hard to get it to fly of course nope I'm just gonna go in and go flying type is equal to new it flies so we're using polymorphism we're doing all kinds of crazy stuff and that's it you're done so now we try this tricked-out program let's just file save that and we're gonna jump into animal play dot java we're just gonna create some things so i'm gonna create an animal called Sparky and he's gonna be a dog and then I'm gonna create another animal called Tweety and she's gonna be a bird and then go system.out.print line and let's say we go dog and then have Sparky figure out if he can fly or not and see what bricks print out on the screen and then we're gonna do the same exact thing with Tweety but we're just gonna say bird here Tweety file save it execute it see what happens there you can say dog I can't fly bird flying high just the way we want it and then I'm gonna come over here remember animal dot job I created this method here that's gonna allow me to dynamically set my flying ability let's say our dog here all of a sudden gets this amazing ability to fly flying ability I'm just gonna create new object inside of the dog object by going in and set in the instance variable for animal doctor so I'm doing is passing it I knew one of these flying types inside of here and to do so just go it flies just create it all safe and now after doing that oh I'll say oopsie and there you see now the dog flies so that's the code for it now I'm gonna jump over and show you a UML diagram sort of make sure that I completely cement this in your head okay so here is our UML diagram basically we have our animal in the plus here means that this is a public instance variable or a field and its name is flying type and it is of type flies and flies is the interface over here and the actual strategy involved in the strategy pattern is to define a family of algorithms these are the algorithms it flies or it can't fly there they are just those to encapsulate each one and make them interchangeable so we're going to encapsulate them inside of this interface called flies and then we're going to allow the animal over here as well as all of its subclasses to dynamically switch between either you being a it flies or a can't fly what's great about the strategy pattern is it lets the algorithm vary independently from clients that use it so it's really cool all this different stuff is all inside of that so let's review you are going to use the strategy pattern when you want to define a class that will have one behavior that is similar to all the other behaviors in a list so we have animals that fly and animals that don't fly the similarity is that flying is involved whether it is they can't fly where they can fly or to explain in another way you want a class object to be able to choose from not flying fly with wings fly super fast by adding in the strategy pattern you're going to be able to dynamically create whole new different types of flying typed animals so to get right down to it you're gonna use the strategy pattern when you need to use one of several behaviors dynamically other good reasons use the strategy pattern it often reduces long lists of conditionals so if you start seeing that using many many different types of conditionals chances are strategy patterns gonna help you out of course like we said a hundred times it avoids duplicate code keeps class changes from forcing other class changes it also allows you to hide complicated or secret code from the user and pretty much there is only one negative you're going to have an increased number of objects and classes if you use the strategy pattern so there is a strategy pattern leave any questions or comments below chances are though the code is gonna explain it all to you so go and get it otherwise till next time
Info
Channel: Derek Banas
Views: 703,523
Rating: undefined out of 5
Keywords: Strategy Design Pattern, Design Pattern, Design Patterns, Strategy, Design Pattern Video Tutorial
Id: -NCgRD9-C6o
Channel Id: undefined
Length: 11min 31sec (691 seconds)
Published: Fri Aug 24 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.