views:

90

answers:

4

Hi,

I am designing an application, and I am unable to point out the correct design for the same. I have one in my mind, but it does not seem to be part of the GOF pattern and so I am not sure whether it is a nice way to go.

My project creates data from any of the possible 15-20 documents (the documents are all of same type, but the data can vary widely). Once the data is obtained, it needs to be formatted in any of the supported 4 formats and displayed. Also, to complicate matters, even though the documents itself are broadly classified to 4-5 types, few of the documents (across these classifications) are formatted in a similar way.

Now, I split it in the following way:

  • Data Creation
  • Data Display

Data creation creates an interface data object with common interface which can handle all these documents.

Data display reads through the data object and displays it in the way it is required.

My first question is that - I did not see about such an interface object in the GOF pattern set. Is this a good design decision to have such a thing?

As I mentioned before, just two documents are formatted the similar way - across classifications. The problem here is that other documents - which should have been formatted the similar way - are not. So, I find myself cloning the code in one scenario while getting the data, which I dont want to.

So, my second question is - what is the best way to handle this?

I will be very thankful if someone can help me out here.

A: 

Your interface object could be described with the Facade or the Adapter design pattern. Don't worry too much if there isn't a direct match with an existing design pattern - your described solution is the best way to go.

For the second question, you could have a base class that implements the functionality that is common to all classes. Then extend it for all special needs. I know books recommend to prefer composition over inheritance, but this isn't a strict rule and there are cases that inheritance is a good solution.

kgiannakakis
+1  A: 

Sounds like Strategy pattern, with the overall application being MVC with a degenerate Controller.

Ignacio Vazquez-Abrams
Agreed. Set up strategies for dealing with the various formats of documents, then access them through the model, using the controller to orchestrate the creation of your data into a view.
Austin Hyde
One problem in that is that I have only two pages which are similar (and they are across the base classifications). Everything else is dependent on the classification and some other document type itself. So I am not sure of how to use strategy pattern here.
Koran
A: 

Have you thought about the builder pattern for the data creation

builder Intent

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

for the display you can use a different decorators

jojo
+4  A: 

Don't try to push too hard pattern in advance. Figure out a few designs and then try to reveal patterns in them. Patterns are meant to communicate and can be seen as reusable for some specific concern only.

So your broad problem is that you have X documents and Y rendered.

  • Try to generate a class hierarchy for the documents that make sense. You can probably factor some logic in a base class or use an interface
  • If you can't figure out interface to abstract all types of document, you may rely on adapters to, well, adapt the various document to a given interface
  • To have multiple renderers, you can have a look at the visitor pattern, the decorator pattern or the strategy pattern, or use just plain inheritance/polymorphism with Y rendered that implements the same interface. It depends on the nature of the variations.
  • To obtain the right renderer according to the use case, you can use a factory to embed the decision and instantiation logic.

GoF patterns are at a lower granularity than your problem. You will have to figure out a design that matches your very specific requirement. In case of doubt, always pick the design that is the simplest / more intuitive. No them one with the most patterns and fancy class hierarchy.

My 2 cents

ewernli
I actually thought about having an adapter instead of the interface object - but I stopped because it seemed an overkill. I am currently relying on factory pattern for the multiple renderers part. Would you suggest that I go for an adapter instead of an interface object?
Koran
Go for the simplest, which is probably the interface in your case. Note however that without knowing what the documents and what the rendering really are, it's kind of hard to pronounce. But if you have something working, and it's consistent and relatively simple (which seem to be your case), then that may be *good enough*. There is no *perfect* design.
ewernli