Today I want to tell you about different point of views: Project Manager and Software Developer. In theory system design should be independent of the technology. But is it really true? The first thing that comes to my mind is Spring MVC model, where the view is often JSF page and Spring @Controller (or any other backing bean type, for example managed bean), with @Repository as a model and @Sevice as real system controller. It's not such standard, is it? Things are even more complicated if we haven't got any standard architecture in our code. Here is the story:
![]() |
| Image from: http://sekelebetinfo.blogspot.com |
I received a task to design the reporting system for one of our applications. A report consisted of several parts, and each part consisted of some elements. There were 3 types of elements, possible to be generated by report system: entry, list and table. Each of them extends the same abstract class and implements the same interface:
public interface ReportItem {
String getName();
Object getValue();
}
public abstract class AbstractReportItem<T> implements ReportItem {
protected String name;
protected T value;
@Override
public String getName() {
return name;
}
@Override
public T getValue() {
return value;
}
// ... some stuff here.
}
The simplest ReportItem implementation was then:
public class EntryReportItem extends AbstractReportItem<String> implements ReportItem {
// ... something more inside.
}
My report system was based on abstract factory design pattern, so again I had an interface and an abstract class my concrete factories were based on:
public interface Reporter {
String generateReport();
}
public abstract class AbstractReporter implements Reporter {
protected Report model;
protected StringBuilder result;
public AbstractReporter(final Report model) {
this.model = model;
}
}
Here is some concrete factory ready to use:
public class TextReporter extends AbstractReporter implements Reporter {
public TextReporter(final Report model) {
super(model);
}
@Override
public String generateReport() {
// ... Something generates here.
return result.toString();
}
}
Because in my Report object I have three different types of ReportItem to be represented: list, table and entry, I want to avoid structural code with "instanceof" in my concrete factories I decided to do it in object-oriented way (or maybe because I'm code quality and OOP freak). My ReportItem interface got additional method. My EntryReportItem was then:
public class EntryReportItem extends AbstractReportItem<string> implements ReportItem {
@Override
public String getAsText() {
// ... something here.
}
// ... something more inside.
}
Instead of checking in concrete Reporter what type my ReportItem is, I can generate report like this:
@Override
public String generateReport() {
result = new StringBuilder();
for (final ReportItem item : model.getItems()) {
result.append(item.getAsText());
}
return result.toString();
}
If I have to generate other type of reports, it's enough to add a new method in ReportItem, implement it in each type of object and create concrete factory class (for example XmlReporter). In my opinion I did something like that:
At the end of the day I talked to my project manager, who decided it was nice solution, but it's not correct. I asked why he think so, and he told me: "Michal, for me this classes, abstract and interface are all part of the model" - and he was right - same as me when I decided to do it in OOP way. In fact, for other parts of our system, created functionality was a model. Because of that each reporter has to manage type of ReportItem by itself and has a method similar to this (and there is no getAsText() method anymore):
public String getItemAsSting(ReportItem item) {
final String result;
if(item instanceof EntryReportItem) {
result = this.getEntryAsString(item);
} else if(item instanceof ListReportItem) {
result = this.getListAsString(item);
} else {
result = this.getTableAsString(item);
}
return result;
}
And generateReport method:
@Override
public String generateReport() {
result = new StringBuilder();
for (final ReportItem item : model.getItems()) {
result.append(this.getItemAsSting(item));
}
return result.toString();
}
Conclusion:
Each used technology, not only Java, changes something in our ideal project design. We can accept it or not, make our code simpler, more object-oriented or easier to extend. On the other hand we can keep application code as close to the project and standards as possible (but what with Scrum reaction on changes?). I'd really like to know your opinion who was right, PM or Developer? Whether we should favor the project over OOP and good programming practices? How do you resolve such a problem?







As I understand well, the getItemAsString() is placed in ReportItem, is that correct?
ReplyDeleteNope, it's currently a part of AbstractReporter (with the three additional abstract methods: getEntryAsString(...), getListAsString(...) and getTableAsString(...)).
Delete"If I have to generate other type of reports, it's enough to add a new method in ReportItem, implement it in each type of object and create concrete factory class (for example XmlReporter)." - oh man, that sounds like bridge pattern (in fact, you could also get rid of that instanceof monster then)
ReplyDeleteHi, thanks for your comment. You can say so if you treat the abstract class as an real abstract (not just as a supporting java object). But if you look at the interface + abstract as an abstract part of factory, and each Report...Item as a concrete factory you can imagine each getAs...() method as a concrete product creation. I believe bridge pattern can be a right solution. On the other hand it's quite possible to resolve that kind of problems with a strategy design pattern also.
Delete