The other day, I solved the problem of adding dynamic widgets on the site with the ability to add different types of widgets, as well as with the possibility of further unlimited expansion of the widget set by adding models of specific implementations.
The system is such that there is one class
, which, depending on the type selected when creating it, refers to a specific implementation model, for example
, which contains some special parameters, and also knows about its template for rendering.
in the site template when calling the
refers to the specific implementation and its method
render()** through the type.
This approach allows you to write an extremely simple code for the template, as well as organize the code so that adding new widgets will consist in adding a new data model, as well as correcting a number of places in the code, or rather updating them. Which in essence will become a routine task and will remove from the programmer the need to come up with something new.
That is, in this article I want to show not only the solution to some problem at the level of program code, but also to show the approach to architecture planning when solving such problems.