dimanche 2 août 2020

How to implement a general class for singleton?

I've been trying to implement a state management project for my design patterns course. I have implemented the singleton because I know that's essential for keeping state of a class. What I would like to do is: Create a general class, so that others could use it in their projects. How do I do that? My code so far:

class StateManager{
  static final StateManager _instance = StateManager._singleton();

  StateManager._singleton();

  factory StateManager(){
    return _instance;
  }

}

My other solution to try and make it general:

class AppProvider extends StateManager<AppProvider>{

  int i = 10;
  String data = "adas";

  


}

class StateManager<T extends AppProvider>{
  static final StateManager _instance = StateManager._singleton();

  StateManager._singleton();

  factory StateManager(){
    return _instance;
  }


}

I want the AppProvider class to be the client class, and I want the StateManager to automatically handle the fact that AppProvider should be a singleton, and maintain the state of AppProvider.. I really don't know how to do that.

Aucun commentaire:

Enregistrer un commentaire