switch

  • If-else is only suitable for use up to 3 levels
  • When there are more conditional judgments, switch can be considered first

    interface

  • When the judgment conditions are likely to increase dynamically, switch can be further optimized by introducing an interface to separate code from data:

    • Create a map:

      • Key: case value of switch
      • Value: The corresponding entity class
    • It abstracts the common method into an interface that unifies the input and return values
    • The main implementation class Controller simply passes in the type value, gets it to the corresponding implementation class, and then calls the abstract method, so that no matter how many more cases are added, the main logic code will not change
    • Each class implements the interface separately and does not affect each other

      db setting

  • Load the entity class corresponding to Type with the DB setting table
  • This does the configuration data

    • What if the implementation classes in the case need to change dynamically?
    • Solution: Implement the case in a dynamic language and write the code in DB

      • DB holds the code
      • Initialize all implementation classes at startup to save time
      • The main implementation class Controller does not change the main logic code, but provides an additional method to initialize the map
      • If you need to add new cases and implementation classes, modify the DB, call methods to update the Map, and reinitialize (you can even write a backend edit script release function).

    conclusion

  • Architecture design is related to requirements. It is necessary not only to know the technology, but also to understand the industry. It is necessary to know to what extent the requirements will change in visible time, and then give different solutions according to different degrees
  • Don’t Overdesign:

    • Just because you have a plan doesn’t mean you have to do it right now. It’s good to get the design one step ahead of the requirements
  • Learning to rate your own code is the best way to improve yourself. A person’s growth is divided into several stages:

    • Function-oriented programming
    • Performance-oriented programming
    • Reproduction-oriented programming
    • Future-oriented programming