views:

447

answers:

4

Hi Guys

I've got a problem where I need to generate a Word based reports on object “Customer”.

I usually do this by passing Customer to a class which knows how to create a Word document, insert bookmarks etc.

The problem with this, is that I find myself placing the logic for retrieving and formatting information about the Customer object in this class.

I understand this is bad and violates the single responsibility principal.

I though about creating another class like “CustomerReportInfo” which accepts a Custom and has all the logic concerning the formatting and retrieving of data, that way, the Word generating class is small and the two can change irrespective of one another.

I'm just wondering what this class should be called? I've gone through a list of patterns and I can't really see anything which fits the description. It doesn't sound like a strategy, or proxy... It sounds like DTO, but DTOs are usually dumb classes with no functionality, aren’t they?

Any ideas on which pattern this resembles?

+1  A: 

I prefer CustomerView or CustomerWordView if it's word specific. You're touching on the MVC pattern here, in which case the third concept is the controller class that loads the Customer from the database (perferably asks someone else to load it) and constructs the view object.

krosenvold
A: 

I would call it

CustomerReportGenerator

I know it sounds poxy but it says what it does without having to comment or anything,

Nathan W
A: 
eed3si9n
A: 

You're on the right track, but not quite there. Instead of creating one new class that is responsible for both retrieving data from the Customer object and containing that data, you want each class to truly have only one responsibility. So you create two new classes.

Based on this article, it seems like you would want an architecture like this:

Data > Assembler > DTO > Word Report Generator

The Word Report Generator is the class you're currently using to generate the report. The DTO is an object that is as simple as you can make it while still containing all of the data you will need for your specific report. The Data is your information about the customer object. Then there is a special Assembler class that contains "the logic for retrieving and formatting information about the Customer object" and populating the DTO with that information.

StriplingWarrior