Placeholder Image

字幕表 動画を再生する

  • Hello, everybody.

  • Kyle here from Web does simplified In this video, I'm going to be going over the no object pattern, which, in my opinion, is one of the easiest to understand and implement design patterns out there.

  • I'm going to be talking about what it is and the different scenarios you should be using it in.

  • And then at the end of the video, I'm going to be going through an example of me converting some existing code into code that uses the no object pattern.

  • So make sure you stick around till the end.

  • Let's get started now, in order to talk about what the no object pattern is, we first need to talk about the scenarios that you would want to use this pattern in.

  • So we can better understand why this pattern is being used and how it works.

  • Essentially, the no object pattern is going to be used any time that you have a knoll object being returned.

  • So the key word no, in a programming language, that's his job script being returned.

  • So normally when you do that, you have to check if something is equal to know before you actually access the different properties on that no proper object because if you access a method, for example, on a no object, you're going to get it.

  • No air being thrown in your code, which is something you don't want, so you need to have bunch of if checks in your code to check to see if something is equal to know or not before you proceed to use the rest of the coat.

  • The idea behind the no object pattern is that you create an object that you return instead of know that has the exact same signature.

  • So the same properties and the same methods as the object you would already be returning from that method, and it will just have default values for all these different properties and methods.

  • So now, instead of having to check for Noel every single time and then if it's no, do something and if it's not know we'll do something else, you can just treat the object that's being returned from this method.

  • For example, there's no object as if it was just a normal object of that same type.

  • For example, if you have an item and item may or may not have an image.

  • So when you get the image of an item, it'll either be knoll or it'll be an image you would have to check every time you use that image.

  • Before you did anything with it, you'd have to check if it's no.

  • And if it's no show some default image, for example, or if it's not, no, then just show the image itself.

  • But if you're using a metal object pattern, this image property will just return a knoll object of the image, which would just be a default image, for example, that you would be displaying instead of the actual image.

  • So every time you got the image of an item, you would return this knoll image, which would act exactly the same as a normal image object.

  • But it would just have default values, such as a default image that it would be displaying instead of the actual image of the item.

  • A commonplace that this is used is with user accounts, because many websites have an option for you to sign into account.

  • And if you're not sign in your being treated as a guest user, so little show that you're a guest.

  • Maybe in the corner of the display of the website, and you'll have certain permission levels where you can access some things but not other things, because you're only a guest and not signed it.

  • So let's take a look at an example of what we have a user system.

  • We'll we'll have guessed users and actually users that are signed in.

  • I just have a simple script open up in visual studio code with a user class here at the top, and there's User has an I.

  • D.

  • And a name property that will be accessed as well as a has access method, which returns whether or not this user has access to this certain criteria.

  • In our case, this is just going to be If the name is equal to Bob, I then have a list of our two users here.

  • User number one is Bob and yours.

  • Number two is John, and then, finally, a method that is going to be getting a user given an I.

  • D.

  • And I just find the user from this list based on the I D that we passed this method and a return that user, and if it can't find that user, it is going to return?

  • No.

  • Then down here, I'm going to be printing that user based on I d.

  • So you call print user.

  • Give it I d.

  • It'll get that user from the I D.

  • And then down here, it'll get the name of the user and prints, say hello name.

  • But if the user does not have a name, for example, this user does not exist.

  • It'll just print guest and then down here.

  • It'll check if the user has access or not, and then print whether they have access or not.

  • And this is using not the no object pattern.

  • And as you can see in the comments here, there's quite a few problems with this.

  • First of all, we need to explicitly tell the consul dot log to print this extra name of Geste if the user's not walked in and every time we either print or display the user's name, we need to put this check will be check if it's no and then tell it to print guest, which means we have to always make sure e type guest exactly the same in all these different locations.

  • And if we ever want to change this guest default name to something else.

  • We have to change it everywhere else in our code, which is a terrible thing to do because it's so easy to miss a single place where it's used, and it's very hard to find that error.

  • If you do make a WR, it's also very easy to forget to check for no for the user.

  • And then you start to get weird things where you may return undefined as the name, which never looks good, or you'll just throw on air where your sight will crash.

  • Same thing down here with the user has access.

  • We need to check first if the user's know and if they have that has access property on the user object before we can actually call it.

  • Otherwise, we're going to get in there and this will happen.

  • Ah, lot, and we will very easily forget to do this.

  • No check, which again means that are website could air out and completely crashed for our user, which we do not want.

  • So what's click?

  • Look at how this code actually functions by calling this print user function with the different ideas of the years is that we have.

  • So if we just call print user and we call it, for example, with the user idea of one which is the first user on list of Bob, you'll see that it prints.

  • Hello, Bob, You have access, which is exactly what we want.

  • If we then want to print with our second user of John, you'll see there says Hello, John, you're not allowed here.

  • That's because John's name does not equal Bob.

  • So then, if we print 1/3 user, which is going to be a user that does not exist, you'll see that Prince.

  • Hello, guest.

  • You're not allowed here.

  • And that's because down here in our code, we have all of our different knoll checks, checking to make sure if we have a no user to treat it in a special case by printing out guest as the name and to make sure to say that they don't have access instead of calling a function that doesn't exist on that no object.

  • So what we want to do is we want to remove all the different knoll checks that we have throughout our code here by using no object pattern.

  • So what we want to do is we want to convert this code to be using a knoll object that gets returned from this get user function of here instead of just returning.

  • No.

  • So the first thing to do that, we just create this no object class and it's no object class was going to be very similar to our user class.

  • Well, just copy Our user class paced it down here, and instead of calling it user, what is going to call it?

  • No user.

  • So we know that this is a no object.

  • This is a very common thing to do with the no object pattern by pre fixing the name of the class with no to say, This is the Knoll version of the object or trying to represent and are no object is not going to take any I D or name because it's just going to have some default.

  • Properties will give it a defoe i d of negative one.

  • And then the name is guest.

  • Since that's the name we're trying to print out down here.

  • We want guest to be our default name, and a guest user will never have any access on our website.

  • So we're just going to return falls for them.

  • Having access to anything now with that taken care of on we need to do is instead of our get user function.

  • If we don't find that user, we just need to return a knoll user instead of an actual user.

  • So we could just set a user variable here to what we find from our users not find.

  • And if that user is no, So if we don't actually find any user inside of that list, I only need to do is return a no user.

  • And then if we do find a user extra example, So if the user does exist, we then just want to return that user.

  • So now there's get user function, takes care of the knoll check itself, and now we never have to check if the user's know anywhere else inside of our code.

  • So if we scroll down here, we don't actually need any of these other checks here.

  • We could just instead print the name of the user from the user variable.

  • We got here from our get user function and same thing down here.

  • We don't need to check anything about it.

  • You know we could just check if the user has access.

  • It'll say you have access or you are not allowed.

  • So now if we go into our browser here and we run this print user function, so I say Print User and we give it an idea of one, for example, which, as we know, is our user Bob, who will have access due to this function.

  • We run this, you see it says, Hello, Bob, you have access.

  • We try to do printing user, and we do, too, which is going to be our John user.

  • It'll say, Hello, John.

  • You're not allowed here.

  • And then finally, if we try to print with User three and in our array, we don't have a user number three.

  • So it'll default to that Noel object of guest.

  • If we run that, Lucius says, Hello, guest.

  • You're not allowed here.

  • And that's as easy as the Knoll object pattern is.

  • It really only requires you to add one extra class or object to your actual code.

  • And then, instead of returning, no, you return that no object that you created, and it saves you from having to do all those extra an old checks throughout all of the rest of your code, and instead you only have to do that in old check once when you're actually getting the object itself.

  • And then you can either return the no object or you can return the actual object that is being found.

  • And I have all these different code examples on my get hub page that you can view, which is linked in the description and my pinned comment below.

  • So make sure you check that out.

  • If you're interested in dissecting this code in more depth, I have a bunch of comments in there explaining how everything works.

  • And if you're still confused after looking at all that, please make sure to leave a comment down below, asking me any of the questions that you guys have problems with.

  • So thank you guys very much for watching this video.

  • I hope you learned something about the no object pattern, and I hope this what did your appetite a little bit for?

  • Wanted to learn more about how you can create cleaner style code that will save you a bunch of headaches in the future, and if so, make sure to check out the rest of my design pattern videos coming in the future.

  • Thank you guys again for watching this video.

  • Have a good day.

Hello, everybody.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

ヌルオブジェクトパターン - デザインパターン (Null Object Pattern - Design Patterns)

  • 1 1
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語