I have a data structure, a large dictionary, that is mutated by several functions. Each function does a database call, some calculations and then changes the value of a key in the data structure. Each function never changes more than 2 keys in the dictionary with the vast majority of functions changing only one.

I've encapsulated all this code in a class but I'm at a loss as to what to call it. I thought about going with Strategy but my colleague rightly pointed that there's never more than one implementation of that class.

Is there even an applicable design pattern?

  • 3
    With so little info it's impossible to tell whether you are using a certain documented pattern. In the other hand it doesn't matter. – Tulains Córdova Jul 4 '16 at 13:20
  • It is a bad idea to name your classes after patterns. When someone comes along and sees a class called "Singleton" for example that tells them nothing about what the class does in the system. What does the object do in your system. – Cormac Mulhall Jul 5 '16 at 9:54

You could call the class a Facade for the database and key/value dictionary.

A facade is a class which encapsulates operations which consist of multiple complex calls to different objects behind a single object with a much simpler interface.

| improve this answer | |
  • +1 Because with so little info given in OP's question the only reasonable pattern one can think of is Facade. – Tulains Córdova Jul 4 '16 at 13:30

Before applying a pattern, clarify what you want to accomplish!

  1. Do you want to reduce the side effects of those functions? Make sure your functions have proper input parameters and output their result as a return value. Then let the caller mutate the dictionary.

  2. Do you want to reduce duplicate (or similar looking code)? Start with 1, but make sure the functions have a uniform interface, so you can use the template method pattern for calling those functions. Also, look if you can refactor common parts into reusable helper functions.

  3. Do you want to make your functions more unit-testable? Start with 1, but also remove the direct database call from the functions. Either pass the input data through a more or less generic repository into the functions (which does the DB queries), so it can be mocked out. Or, if the database queries are very uniform, you might think about constructing a solution where the caller of that functions does the db query beforehand and pass the result directly into the functions.

You might have another problem in mind, since you wrote something about "large code blocks" in your title, but your question text speaks of "several functions" (which is more an indicator for small code blocks). So maybe you should consider to rethink your question and add additional information, if the points above do not address your problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.