Question about design pattern

I am developing an application and I cannot specify the correct design for it. I have one in my mind, but it doesn't seem to be part of the GOF template, and so I'm not sure if this is a great way to go.

My project creates data from any of the possible 15-20 documents (all documents are of the same type, but the data can vary widely). As soon as the data is received, it must be formatted in any of the supported 4 formats and displayed. In addition, to complicate matters, although the documents themselves are widely classified into 4-5 types, some of the documents (according to these classifications) are formatted in a similar way.

Now I break it as follows:

  • Data creation
  • Data display

Creating data creates an interface data object with a common interface that can process all of these documents.

The data mapping reads the data object and displays it as required.

My first question: I have not seen such an interface object in the GOF template set. Is it a good design decision to have such a thing?

As I mentioned earlier, only two documents are formatted in the same way - according to classifications. The problem here is that other documents that should have been formatted in a similar way are not. So, I found that I was cloning the code in one script, getting data, which I donโ€™t want.

So my second question is: what's the best way to handle this?

I would be very grateful if someone could help me here.

+4
source share
4 answers

Do not try to promote too hard a template in advance. Select some patterns, and then try to identify patterns in them. Templates are intended for communication and can be considered as reusable for any specific problem.

So your common problem is that you have X documents and Y rendering.

  • Try creating a class hierarchy for documents that make sense. You can probably define some logic in the base class or use the interface
  • If you cannot define an interface for abstracting all types of documents, you can rely on adapters to adapt various documents to this interface.
  • To have multiple renderers, you can take a look at the visitor template, decorator template, or strategy template or use simple inheritance / polymorphism with Y, which implements the same interface. It depends on the nature of the variations.
  • To get the desired renderer according to the use case, you can use the factory to embed the decision logic and instantiate.

GoF samples have less granularity than your problem. You will need to develop a design that meets your specific requirement. When in doubt, always choose the simplest and most intuitive design. They do not have one with most templates and a stylish class hierarchy.

My 2 cents

+5
source

Sounds like a strategy template, with a common application being MVC with a degenerate controller.

+1
source

Your interface object can be described using Facade or Adapter . Don't worry too much if there is no direct fit to the existing design pattern - your described solution is the best way to go.

In the second question, you can have a base class that implements functionality common to all classes. Then expand it for all special needs. I know that books recommend preferring composition over inheritance, but this is not a strict rule, and there are cases where inheritance is a good solution.

0
source

Have you ever thought about a builder template for creating data?

Constructor Intention

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

you can use various decorators for the display

0
source

Source: https://habr.com/ru/post/1300012/


All Articles