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?
2 Answers 2
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.
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.
Explore related questions
See similar questions with these tags.
function __construct($doc_id, $language)