switch

  • If-else is only suitable for use up to 3 levels
  • If there are too many criteria, you can use the Switch first

interface

  • When the judgment condition may also increase dynamically, it can be consideredswitchFurther optimization, the introduction of interfacesinterfaceThat will beSeparation of code from data:
    • Create a map:
      • Key: case value of the switch
      • Value: indicates the corresponding entity class
    • Abstract out of common methods into an interface, unifying the input and return values
    • The controller class simply passes in the type value, gets the corresponding implementation class, and then calls the abstract method, so that no matter how many cases you add, the main logic code doesn’t change
    • Each class implements interfaces independently of each other

db setting

  • Use db setting table to load the entity class corresponding to type
  • This enables configuration datafication
  • What if the implementation class in a case needs to change dynamically?
  • Solution:caseThe implementation is done in dynamic language, and the code is written indbIn the
    • The db holds the code
    • Initialize all implementation classes at startup time to save time
    • The controller class does not change the main logic code, but provides an additional method to initialize the map
    • If you need new case and implementation classes, modify the DB, call the map update method, and reinitialize (you can even write a background edit script to publish the function).

conclusion

  • Architecture is about requirements, not only technology, but also industry, to know how much requirements change in the visible time, and then according to the degree of different solutions
  • Don’t over design:
    • Just because you have a plan doesn’t mean you have to do it right now, it’s nice to be able to design ahead of time
  • Learning to rate your code is the best way to improve yourself. There are several stages in a person’s growth:
    • Function-oriented programming
    • Performance-oriented programming
    • Reuse oriented programming
    • Programming for the future