The Module Reveal Pattern
While the Module design pattern was very powerful it had a few limitations and they related mainly to the fact that we had to treat the public and private members differently. More over then that, this split made it hard for us to call and use the public members from the private areas of our code. The Module Reveal Pattern comes to solve this issue.
Video Transcript
00:00In the last lesson,
00:01we were introduced to the module design pattern.
00:04In this one, we're moving into the reveal pattern,
00:06which is literally a upgraded version of the module pattern.
00:11The problem with the module pattern was is that
00:13we treated the public and private members differently
00:16by separating them.
00:18That separation made it a little bit more complex
00:20to code in general.
00:21Behind that complexity, the private members
00:24couldn't interact with the public ones
00:26because for them to interact with them,
00:27we would have to name the object
00:29that we're sending out.
00:30But then again, we're creating a relatively complex code.
00:33And that complexity adds to our core structure's complexity.
00:38The goal of the module reveal pattern
00:40is to improve that complexity and solve those problems.
00:43It creates some of its own problems,
00:45and you could read up on them
00:46in the documentation that we provided.
00:49But let's jump right into it,
00:50and let's learn about how could we work
00:52with the reveal pattern that is a more simplistic
00:54and easy to work with pattern.
00:58So one of the biggest and most clear disadvantages
01:01of the module design pattern
01:04is that if we needed that our private methods and properties,
01:09if our methods needed to communicate with the public methods,
01:12if they needed to use them,
01:13we would have to actually create here an object
01:16and refer back to them,
01:17and it would add complexity to our code.
01:20Beyond that, the way we would approach them
01:22would be completely different.
01:23So something about the language
01:25and the difference between the two is so sharp
01:27that is on its own a bit of a negative.
01:30So let's look at the reveal design pattern.
01:32And the reveal design pattern
01:34is almost the same as the regular design pattern
01:37that we had, the regular module design pattern,
01:41but the reveal module design pattern
01:43makes it the reveal process
01:45separate from the actual creation process.
01:48So if I go ahead here,
01:49what's actually happening is that all of our methods
01:52would then be living,
01:53and let me just go ahead here
01:54and just delete that and put that there.
01:57So first of all, we're creating the function itself,
01:59defining it just with everything else.
02:01Probably we'd wanna separate it in some level,
02:04so it'll be easy for us to access it.
02:06I'm just gonna go ahead here
02:06and just copy all those methods
02:09and just move them out of that return value
02:11and place them literally as just functions
02:14that are living within that scope of that reveal model,
02:18basically the chat module that we've created.
02:21Now I'm gonna just continue this process, converting them,
02:25and really, once I've done that,
02:26now my methods are more or less ready to be used.
02:29There's only one problem here
02:31is that it's gonna be very hard for me to recognize
02:33between the public and private methods.
02:35So one way that I could approach this
02:37is just set an underscore for everything that is private,
02:41so it's very clear to me always
02:42that these are intended to be private,
02:45so the echo is intended to be private as well.
02:47Everything that is intended to be private,
02:49I'll go ahead and use an underscore next to it.
02:53Then I could go ahead
02:54and just update all of our variables here
02:56to make sure that they're using their signature,
02:59oh, that doesn't need that,
03:00and just go ahead here quickly
03:02and try to find which ones do need it,
03:04and the lead itself would need it,
03:07and the yes no message would need it,
03:11and the lead here would need it,
03:13and our echo call would also need it
03:15because it's a private method,
03:17and a sassy stuff needs it,
03:20and this lead needs it,
03:23and yeah, I might miss one or the two,
03:24but here we go,
03:25so I created a clear separation
03:27where everything that is private starts with an underscore,
03:30so it's very easy for me to recognize
03:32that these methods or these properties
03:34or these elements are intended to stay private.
03:38Now that I'm done with that,
03:39what I could do is list out all of the private members
03:42and literally go ahead here and say,
03:44okay, so the talk method,
03:45I'm gonna just send that talk method through,
03:47so now it has access to that talk method.
03:50I'm also gonna reply yes, no,
03:54I'm gonna go ahead and just define that right there,
03:57and for our say sassy stuff,
03:59I'm just gonna continue down that line,
04:01literally exposing only what I want to expose.
04:04Now the big advantage here is that
04:06I could now refer to this talk method
04:08also from the private variables,
04:10and I could refer cross refer back and forth
04:12without any problem between the members,
04:14even if they're public, it doesn't really matter,
04:16and that way I'm separating that process
04:19from exposing what is public
04:21from the fact that I could use this
04:22as just any normal function mechanisms as I would want to,
04:26so down the line I might say,
04:27okay, I wanna convert this into a public method,
04:29and I could very easily do that without any problem,
04:33without changing a lot of stuff,
04:34unless I wanna change also the name very quickly,
04:36but that could be a very quick search and replace.
04:38All right, so before we wrap this up,
04:40I just wanna talk about the negatives.
04:42So one of the big negatives here is that it is final,
04:45which means that I can't go here and just override talk
04:48once the document is complete.
04:51The main reason is because I wouldn't have access
04:53to all these properties that live only inside
04:55of the scope of this function,
04:57nor do I have any access to the private variables.
05:00So that's one of the biggest negatives,
05:02but the biggest positive here
05:04is that it's a much cleaner process,
05:05and with this cleaner process,
05:07it's gonna be much easier for us to maintain our code.
05:09So if I go ahead here and just take a little refresh
05:12and see everything should continue working,
05:15we have here a little error,
05:15so we're gonna fix it in line 35.
05:18So let's go ahead there, line 35.
05:19What is our problem here is that we put here a comma there,
05:24which wasn't needed.
05:25And let me just go ahead and save that, click on refresh,
05:28and we should see that our code,
05:29we're getting closer.
05:30Well, ASET does not exist anymore
05:31because we need to make it private.
05:33We need that underscore, which is in line 14.
05:36We missed line 14, which is very logical.
05:39Here we go.
05:40Let's give it another shot.
05:41Maybe now we converted everything, let's hope.
05:45There we go.
05:46And that's it.
05:47We just upgraded our module design pattern
05:51into the reveal module.
05:53And the reveal module has these benefits,
05:55and the disadvantages,
05:56we saw that we can't override those public methods anymore
05:59because we don't really have access to them,
06:02we have access to our reference to them,
06:04and we can't update the internal mechanisms inside there.
06:07So it's a much more fragile design
06:09where it's intended to be complete
06:12and not necessarily extensible once created.
06:16In this lesson, we met the reveal pattern
06:18and saw its advantages.
06:20In the next lesson, we're going to wrap up this section
06:24by talking about how to control global access completely.
06:27Until now, we created a reveal pattern or a module pattern
06:31and use them for every single group of code that we wanted.
06:34But when it comes to when we have tens or 20
06:36or 30 different objects that we want to work with,
06:39we're going to talk about that now
06:40and, well, in the next lecture,
06:43and we're going to see how to take advantage of that
06:44in the most simplistic and nicest way.
