Refactoring Singleton Overuse

后端 未结 7 1959
一个人的身影
一个人的身影 2020-12-14 00:06

Today I had an epiphany, and it was that I was doing everything wrong. Some history: I inherited a C# application, which was really just a collection of static methods, a co

相关标签:
7条回答
  • 2020-12-14 01:01

    Alright, here's my best shot at attacking this question:

    (1) Statics

    The Problem with static that you may be having is that it means different things in .NET and say, C++. Static basically means it's accessible on the class itself. As for it's acceptability id say it's more of something you'd use to do non-instance specific operations on a class. Or just general things like Math.Abs(...). What you should use for a global config is probably a statically accessed property for holding the current/active configuration. Also maybe some static classes for loading/saving setting the config, however the config should be an Object so it can be passed around manipulated, etc. public class MyConfiguration { public const string DefaultConfigPath = "./config.xml";

      protected static MyConfiguration _current;
      public static MyConfiguration Current
      {
        get
        {
          if (_current == null)
            Load(DefaultConfigPath);
          return _current;
        }
      }
    
      public static MyConfiguration Load(string path)
      {
        // Do your loading here
        _current = loadedConfig;
        return loadedConfig; 
      }
    
      // Static save function
    
      //*********** Non-Static Members *********//
    
      public string MyVariable { get; set; }
      // etc..
    }
    

    (2) Controller/Hardware

    You should probably look into a reactive approach, IObserver<> or IObservable<>, it's part of the Reactive Framework (Rx).

    Another approach is using a ThreadPool to schedule your polling tasks, as you may get a large number of threads if you have a lot of hardware to pool. Please make sure before using any kind of Threading to learn a lot about it. It's very easy to make mistakes you may not even realize. This Book is an excelent source and will teach you lots.

    Either way you should probably build services (just a name really) for managing your hardware which are responsible for collecting information about a service (essentially a model-pattern). From there your central controller can use them to access the data keeping the program logic in the controller, and the hardware logic in the service.

    (3) Global Configuration

    I may have touched this subject in point #1 but generally that's where we go, if you find yourself typing too much you can always pull it out of there assuming the .Instance is an object.

    MyConfiguration cfg = MyConfiguration.Current
    cfg.Foo // etc...
    

    (4) Listening For data

    Again the reactive framework could help you out, or you could build up an event-driven model that uses triggers for incoming data. This will make sure you're not blocking on a thread till data comes in. It can reduce the complexity of your application greatly.

    0 讨论(0)
提交回复
热议问题