2

I am writing some code that really smells but I can't figure out exactly why.

I have documents that exist in multiple languages. I want to be able to serve a document in its requested language and be able to switch it out for other languages when requested. On my database the documents are separate entries with identical identifiers (doc_id - which is not a unique value). For instance:

tb_documents

id | doc_id | language | other stuff...
---------------------------------------
1  | 10268  | eng      |
2  | 11352  | eng      |
3  | 11352  | jpn      |
4  | 11352  | ger      |

When constructing my document object I am taking a doc_id and a language and constructing my object based on the relevant database entry. An issue I can see here is that in the future we might have other modulations of the document object. Maybe we will want an abridged or maybe start keeping past versions of the same document. I don't want to couple my object creation with all these variables, and I feel like creating new sub-classes would get muddy, as I would want to combine the types - an old version of an abridged translated document for example.

I feel like this is the kind of problem that could be solved with an elegant pattern, but I lack the experience to pick one out. Any suggestions?

6
  • What do you mean when you say "constructing my document"? Do you just copy phrases in different languages or do you actually construct sentences out of words, respecting grammar and all? Apr 16, 2018 at 5:33
  • Sorry, I just mean initializing the object: function __construct($doc_id, $language) Apr 16, 2018 at 6:29
  • Could you elaborate how exactly the objects differ from one another when using different languages? Is it just a dictionary where you switch out words? Do you save whole sentences? Do you make up whole sentences from words, respecting grammar? I would tackle all of these in different ways, so it is hard for me to tell you what to do without this information. Apr 16, 2018 at 8:09
  • The display medium of the documents is being abstracted out as well as it might change. They may be images, PDFs or HTML strings. For now they are simply PDF pages converted to images and we're serving the 'document' as a series of PNG's - different PNG sets for different languages. I guess different resources for different document types. Apr 16, 2018 at 21:51
  • OP, can you explain what you mean by "modulate?" I am not able to find any reference to that with regards to object orientation. Do you mean "modularize?"
    – John Wu
    Apr 17, 2018 at 1:16

2 Answers 2

0

You may want to take a loot at the Builder pattern.

https://en.wikipedia.org/wiki/Builder_pattern or https://sourcemaking.com/design_patterns/builder/php/1

My only concern is that now the construction of your objects is pretty straightforward and this can prove an overkill but if you expect an increase in complexity then it might as well worth investing in the pattern.

1

You already have an elegant pattern. id vs doc_id. The doc_id identifies those documents with a common origin. You can make as many id's with the same doc_id as you like.

Adding modulations like abridged will involve figuring out and setting abridged values for all existing documents before adding new documents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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