Home > OS >  Use of Singleton
Use of Singleton

Time:01-31

I am building an application that can be used with or without a User being logged-in. There can only be one User logged-in at the same time. If no User is logged-in the App provides basic functionality and if a User is logged in the App provides enhanced functions, depending on the status of the User.

The properties of the User are contained in a (model) class (e.g. name, avatar, status, etc.) and I am using Provider to propagate User properties throughout the application (e.g. show name, avatar, etc.).

My question is: Should I use a singleton for the User class?

Thanks for your time/answer,

Pierre

CodePudding user response:

You should use a singleton design pattern for the user class because of the following reasons:

A singleton pattern is used when we want to have only one instance of a class in the entire app. So if you create a user class a singleton then you can access all the user detail in the entire app with only a single instance.

It will allow you to save some memory of your app. Also, it will ensure that you have only one instance of the class in the entire app.

If you want to know more about singleton pattern you can read it from here: Link

  • Related