So I have a PageItem object that I want to possibly reuse elsewhere.
I’m in ABC framework.
Here’s the PageItem interface :
public interface PageItem {
public abstract String getUrl();
public abstract boolean getIsCurrent();
public abstract PageItem getParent();
public abstract List<PageItem> getPChildren();
public abstract void setParent(PageItem pageItem);
public abstract void addChild(PageItem pageItems);
public abstract boolean getHasSelectedChild();
}
Here’s the implementation :
public class PageItemImpl implements PageItem {
private String title = "";
private String url = "";
private boolean isCurrent = false;
private List<PageItem> children = new ArrayList<PageItem>();
private PageItem parent = null;
public String getUrl() {
return url;
}
public boolean getIsCurrent() {
return isCurrent;
}
public List<PageItem> getChildren() {
return children;
}
public PageItem getParent() {
return parent;
}
public void setUrl(String url) {
this.url = url;
}
public void setIsCurrent(boolean isCurrent) {
this.isCurrent = isCurrent;
}
public void setParent(PageItem parent) {
this.parent = parent;
}
public void addChild(PageItem pageItem) {
this.children.add(pageItem);
}
public boolean getHasSelectedChild() {
return false;
}
}
Here’s the factory :
public class PageItemFactory {
public static PageItem getPageItem(ABCPage page, ABCRequestParams params) throws ABCException {
PageItemImpl pageItem = new PageItemABCImpl();
pageItem.setTitle(page.getTitle());
pageItem.setUrl(page.getUrl());
pageItem.setIsCurrent(params.getUrl().equals(page.getUrl());
return pageItem;
}
}
Is that me or it doesn’t make sense?
PageItemFactory won’t be reusable elsewhere since it has references to ABC framework.
Should I just make a class that overrides PageItem and use a constructor for ABC specific parameters?
That depends. There are two main reasons to have a factory:
The object setup is complex.
You need to translate between two independent parts of your application.
In the first case, the factory knows how to initialize an object so it can be used right away. In the second case, the factory works like a adapter/converter (converts one object type into another).
Reuse doesn’t matter – in this case, it separates concerns.