This project demonstrates the implementation of the Singleton Design Pattern in a Flutter application. The Singleton pattern ensures that only one instance of a class is created and provides a global point of access to it.
The project consists of the following files:
database.dart: Contains theDatabaseclass, which implements the Singleton pattern.client.dart: Contains theClientclass, which tests the Singleton implementation.main.dart: The entry point of the Flutter application.
- The
Databaseclass uses a private constructor (_internal) and a static instance (_instance) to ensure only one instance of the class is created. - A factory constructor (
factory Database()) checks if an instance already exists. If it does, it returns the existing instance; otherwise, it creates a new one. - The
createDatabase()method simulates the creation of a database.
- The
Clientclass creates two instances of theDatabaseclass using theDatabase()factory constructor. - It checks if both instances are the same using the
identical()function. - If the instances are the same, it confirms that the Singleton pattern is working correctly.
- The
main.dartfile initializes theClientclass and calls thecheckSingletonPattern()method to verify the Singleton implementation.