'Using factory pattern inside the strategy pattern?
Assume I have articles that are modified based on certain strategies:
public class Article {
String header;
String bodyText;
}
The processor object:
public class ArticleProcessor {
IModifyStrategy modifyStrategy;
Article article;
...
...
modifyStrategy.modifyHeadline(article.getHeader());
...
modifyStrategy.modifyBodyText(article.getBodyText());
}
The modifier strategy:
public interface IModifyStrategy {
String modifyHeadline(String defaultHeader);
String modifyBodyText(String defaultBody);
}
Now I want to add an author to my article, who needs to be created first, based on some conditions, so I thought about creating a factory and add it to the ArticleProcessor like this:
public class ArticleProcessor {
AuthorFactory authorFactory;
IModifyStrategy modifyStrategy;
Article article;
...
...
modifyStrategy.modifyHeadline(article.getHeader());
...
modifyStrategy.modifyBodyText(article.getBodyText());
...
article.addAuthor(authorFactory.createAuthor(someCondition))
}
However, so far I liked that my IModifyStrategy collected all modifications on an article. By introducing a factory as above, I would have article modifications at different places in the code (e.g. in IModifyStrategy and ArticleModifie). So I thought about adding the logic to the IModifyStrategy:
public interface IModifyStrategy {
String modifyHeadline(String defaultHeader);
String modifyBodyText(String defaultBody);
void createAuthor(someCondition)
}
That way I would have all my modifications on an article gathered at one place in the code, providing me a good overview of WHAT possible modifications are done on an article. So what's the right pattern to choose here? The INTENTION is actually a creational one, but the readability/understandability would be so much better with the strategy pattern. Another option would be to use the factory inside the strategy. Would this be overengineering?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
