The right design for global access and data modification

I’m sure that this is what appears regularly, but I don’t know how to deal with it, and I don’t know the corresponding terms for the search. I am creating a search engine utility that adds heuristics to incoming search jobs using Java. (So, for example, if a user searches for a “couch”, the utility will add terms such as “sofa” and “bedroom furniture” to provide more relevant results.)

I have problems processing global information. In this particular case, heuristic terms are global information that can be changed by someone working on the backend, or that can be accessed by a search client. So, I would like to know what the best practices will be in this case: I can create a class with almost purely static methods called "HeuristicSearchEngine" that can be run at the start of the server, load the heuristic into memory and have methods that can be used to access to heuristics for searching or changing conditions. It just seems sloppy as it takes no advantage of OOP. So another approach is to use singleton to instantiate the current heuristic. Thus, whenever a search task starts, the task can load an instance of the current state, add heuristics to the search, and move on. However, the singleton did not seem to be appropriate here, and I would like to know how others would handle this.

Let me know if you would like more information about any other information.

+4
source share
1 answer

The singleton / factory method that you describe is functional and easy to implement. But singles are dangerous, because if you want to implement the options of your services to work with you on the road, you may have to reorganize everything to accommodate them. In addition, singletones are usually transmitted with static factories, which in themselves can inhibit extensibility.

Instead of a singleton / factorial approach, consider dependency injection. This is more cumbersome to implement, but extends more gracefully.

When you implement dependencies, you expect the component needs for data / services, passing them to the dependent components before they need them. (While the alternative leaves the job of detecting data / services for this component — functionality that you could achieve with something like a static factory.) In general, you deactivate components and isolate their functionality; This is generally accepted as an effective development approach.

As a more specific example, instead of your servlets calling com.me.Herusitic#getInstance() , a Heuristic instance will be passed in their place for their constructors. This Heuristic reference is stored inside the object and is called as needed. Later, when you extended Heuristic to FancyHeuristic and FakeHeuristicForTesting , and you want your servlets to use them instead, you don’t need to change the servlet code: just switch to a new type of heuristic. With the factory approach, you have to rewrite the logic of the factory.

This answer only scratches the surface of this topic. Fortunately, there is no end to the discussion of the DI design pattern (both in StackOverflow and elsewhere), so I will leave it to continue the research.

+2
source

Source: https://habr.com/ru/post/1393673/


All Articles