-2

I am making a small "search engine app". The app should get three pieces of input from the user:

  1. The name of the search engine as a string (e.g. "googe", "duckduckgo", etc)
  2. The search term
  3. The name of the website the user wants to search on if they want to search only webpages of a particular website (e.g. "reddit.com", "stackoverflow.com", etc)

I am having a SearchEngine class for this which I am thinking it would initialize like so:

se = SearchEngine(url="https://google.com")

However, the user will be entering "google" as input, not the entire https://google.com URL.

Now, I plan to have both a command line and a web interface.

For example, in the CLI the engine name would come through an input functon:

engine_name = input("Search engine name: ")

In a web interface the engine name would come through an HTML input element.

So, the client is sending a name while the SearchEngine class is getting a url. One way to solve that "incompatibility" would it be to initialize a SearchEngine object like below:

se = SearchEngine(name="google")

and convert the name to a url inside the SearchEngine constructor. I am afraid that would be a bad design.

Would it be better to have another intermediate layer (class?) that converts name to a url? That way SearchEngine would be independent from the user/client input format.

What design pattern am I using here? What design pattern should I use?

Andy Gondzer
  • 125
  • 7
  • 5
    Don't over complicate it it, all you need to convert a name to a URL is a Dictionary, a whole class is serious overkill. – Turksarama Apr 13 '20 at 02:46
  • 3
    Does this answer your question? [Choosing the right Design Pattern](https://softwareengineering.stackexchange.com/questions/227868/choosing-the-right-design-pattern) – gnat Apr 13 '20 at 06:48
  • 1
    @Turksarama: the question was not how to implement the mapping, but where to place the implementation. – Doc Brown Apr 13 '20 at 09:26

1 Answers1

3

"What design pattern am I using here?"

None.

"What design pattern should I use?"*

None.

Do yourself a favor and stop searching for reusable solutions (=patterns) before you haven't build a usable solution.

"Would it be better to have another intermediate layer (class?) that converts name to a url?"

Some piece of code will have to do the conversion, of course. To know where it makes most sense to place the code in your app one has to know the overall structure of the app. Since you are probably still developing this structure, I would recommended the following:

  • start with the most simple solution for the problem you can come up with. If that is making the search engine name a constructor parameter of your SearchEngine object, that's fine. How it will work internally (using a switch statement, two pairwise lists or a dictionary) is an implementation detail you should figure out by yourself.

  • when the code base grows and the constructor code becomes "too large" and convoluted, or in case you want to maintain the list of available search engines at a different place, maybe outside of the code at all, then refactor your code, extract the conversion to a different class or layer and call that layer from the SearchEngine constructor.

TLDR; don't overanalyse beforehand, instead refactor as you go.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565