Design Patterns — Singleton

  • Database repository — If you have an object which accesses the database, and it loads up the database in the constructor into memory and then gives out information about the content of the database once it’s loaded. So once you read the database you do not need to read it again (especially when you are testing the code).
  • Object factory — If you have a factory that creates some components, then it’s not necessary to have more than one instance of it, because a factory is not supposed to have any state.
  • Handle files — If you have a class that process files, and you have many processes happen at the same time, instead of having many instances to deal with this processing, you can have a single instance of this object, this way it will save memory consumption.
Objects are the same instance
Same instanceDispatch request to: ServerI
Dispatch request to: ServerI
Dispatch request to: ServerII
Dispatch request to: ServerI
Dispatch request to: ServerII
Dispatch request to: ServerIV
Dispatch request to: ServerIII
Dispatch request to: ServerIII
Dispatch request to: ServerI
Dispatch request to: ServerV
Dispatch request to: ServerI
Dispatch request to: ServerI
Dispatch request to: ServerI
Dispatch request to: ServerIII
Dispatch request to: ServerV
https://github.com/henriquesd/DesignPatterns

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store