Two thing to notice about private constructor :1. Class with private constructor can not be inherited 2. We can not create object of the class which has private constructorUse of Private constructor: Many times we do not want create instance of some of the classes like Helper, utility and common routine classes.
1. Private Constructor: Private Constructors are used to restrict the instantiation of object using 'new' operator. 2. It is commonly used in classes that contain static members only. 3. This type of constructors is mainly used for creating singleton object. 4. If you don't want the class to be inherited we declare its constructor private. 5. We can't initialize the class outside the class or the instance of class can't be created outside if its constructor is declared private. 6. We have to take help of nested class (Inner Class) or static method to initialize a class having private constructor.
private constructor don`t allow users to create an instance of class , mainly we are using private constructor whenever we need singleton class.
Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static. For more information see Static Classes and Static Class Members.
The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category. A utility class, that only contains static methods.Generally, they are used in singleton design patterns, where the code ensures that only one instance of a class can ever be created.
First of all, .subscribe is not an Angular2 thing.That's a method that comes from rxjs library which Angular is using behind the scene.If you can imagine yourself when subscribing to a newsletter and after the subscribing, every time that there is a new newsletter, they will send it to your home ( the method inside subscribe get's called).That's what happens when you subscribing to a source of magazines ( which they call it Observable in rxjs library)All the AJAX calls in Angular is using this library behind the scene and in order to use any of them, you've got to use the method name, e.g get, and then call subscribe on it, because get returns and Observable.Also, when you're doing this Angular is using Observables behind the scene and subscribes you to that source of thing, which in this case is a click event.Back to our analogy of Observables and newsletter stores, after you've subscribed, as soon as and as long as there is a new magazine, they'll send it to you unless you go and unsubscribe from them which for that to happen you've got to remember the subscription number or id, which in rxjs it would be like :let subscription = magazineStore.getMagazines().subscribe((newMagazine)=>{console.log('newMagazine',newMagazine);}); And when you don't want to get the magazines anymore:subscription.unsubscribe(); Also, the same goes forthis.route.paramMap which is returning an Observable and then you're subscribing to it.My personal view is rxjs was one of the greatest things that were brought to JavaScript world and it's even better in Angular.There are 150~ rxjs methods ( very similar to lodash methods) and the one that you're using is called switchMap
The private constructor is generally used in classes that contain static members only and also used to prevent creating instances of a class when there are no instance fields or methods.
Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.
It is mostly used to create a singleton class.
Private constructor is used to preventing creating instance of class.
Main use of creating Private constructor is that we cant create the object of that class which contains Private Constructor and the another use is that you can not inherit that class. Private constructor is also use to implement singleton pettern where you just want create the object of the class just once throughout the execution of the application.
private constructor restricts developers from creating instance outside of that class. Thus can be used to implement "Singleton" design pattern.
private constructor is used for limit instantiation...or stop the instantiation..
the main advantage of private constructor is to prevent the instantiation of class and also we use private constructor when we implement singleton pattern and also when we have helper classes or utility class in those we cases we don't to create instance of class rather we want to call the class by name
Private constructor:- 1.it is use in singleton design pattern in which create single object that is in it. 2.Class with private constructor cannot be inherited.3.also cannot create instance in another class.
1- It doesn't allow developer to create Class Constructor. 2- When you want required class should be inhered
Use of private constructor: 1. To prevent creating instances of a class when there are no instance fields or methods, such as the Math class. 2. Use if all the methods in the class are static, consider making the complete class static.
When you don't want users to create instances of your class, then create private constructor of that class. Then how to use that class? Make a static function/property/field which will return instance of that class, and return it.