In this video Emmanuel Henri explains the singleton pattern with an example.
- [Instructor] The singleton pattern can be intimidating…when you first hear about it,…but you'll see how simple it is once you understand…that it's simply preventing our class…from creating more than one instance…of the blueprint we've defined.…In other words,…we use the same principle we've used…since the beginning of this chapter…except we allow only one instance of the class…to be created.…Let me demonstrate.…So let's go back to our file here,…and let's go at the top and return twice.…And we'll create a variable called instance with a let…and make it null.…
So this is how we're going to track if there's an instance…of something created.…And then in the car, and let's comment that for now…so I'm going to do command forward slash to comment this…and remove the cx5.…And then instead of creating a car instance right away,…we'll check if there's an instance of that class…and if not, then you can go ahead an initiate one.…So we'll do an if,…and check if there's an instance,…and then open and close curly braces,…and then put the initiation of the new instance inside.…
- What's a pattern?
- What's the role of callbacks?
- Creational patterns, including Singleton and Factory
- Structural patterns, including model-view-controller
- Behavioral patterns, including Memento and Mediator
Skill Level Intermediate
2. Creational Patterns
3. Structural Patterns
4. Behavioral Patterns
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.