When to use Spring prototype scope?

后端 未结 3 634
太阳男子
太阳男子 2020-12-05 00:28

I want to know when should i exactly use the prototype scope in Spring? I have understood that singleton returns the same object instance if the be

3条回答
  •  旧时难觅i
    2020-12-05 01:00

    There are some interesting use cases by using scope prototype you will build a better and reliable application design/architecture, for example, a real-time system.

    Imagine that you must build a real-time system for vehicle tracking, and you will have 2.000.000 cars sharing information every 5 seconds, In the server side, you will work with two or more distinct group of configurations, one for Cars and another one for Trucks.

    Based on this simple example, if you design your application to work with distinct configuration groups in memory through the prototype pattern you will achieve a better performance.

    So, in this case, whenever the server receives a new message from a Truck, for example, the server will get the instance of the configuration in memory from a hashmap of instances of VehicleGrupConfiguration and then apply the configuration behavior that this message must have, e.g: like time-out, retry... and etc.

    I would like to highlight that there are many ways to implement this situation, but this example shows that a prototype pattern is very powerful in matters of performance and design patterns.

提交回复
热议问题