This question already has an answer here:

I have been handed a very cluttered, "One Ring" object (one object to rule them all). The OR class has 40 fields. These fields map to 16 different objects (the OR has all the fields from the 16 objects concatenated together. Some of the fields are common to all objects, some are unique to individual objects [all of the 16 objects are descended from a common parent]). The OR points to a catch-all table in a denormalized SQL database. I've been asked to write an adapter for this that will take random instances of the 16 objects, map the fields from a given object to an instance of the OR, and send it on its way. Oy.

Changing the back end or the code is not an option (I tried).

As I'm sure is no surprise, this beast is a nasty thing to manage. I would love to hear some thoughts about ways to help make working with this a bit more manageable. I'm primarily concerned with code maintenance: comprehension and readability. I know whatever gets implemented will be far from perfect, because I'm basically trying to make garbage look pretty. That said, for now, I have to live with it, so I'm trying to make my side of the train wreck a little bit more livable.

marked as duplicate by Doc Brown, user40980, gnat, Bart van Ingen Schenau, jwenting Jun 19 '14 at 11:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    What do you mean by "Changing the back end or the code is not an option"? What CAN you change? – BobDalgleish Jun 18 '14 at 21:17
  • 1
    If you can't refactor the class itself, you're screwed. – Casey Jun 18 '14 at 21:26

This is an example of a facade pattern, which may provide some starting points for your research. I've also seen it called an anti-corruption layer.

Basically, you create your dream API, the one you would create if starting from scratch today, but implement it by making calls to the current horrible back end. Then you can slowly move calling code to your new API, and when everything is moved, you can much more easily replace the back end.

This is sort of the rock climber's approach. You get a safety in place before you make the next move.

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