开发者

Enum factory-style method

In my application, several different reports can be generated (CSV, HTML, etc).

Instead of creating a traditional factory-style method pattern, I was planning on adding a method to the body of enum constants that would create and return the appropriate report object.

public e开发者_Python百科num ReportType {
 CSV {
  @Override
  public Report create() {
   return new CSVReport();
  }
 },
 HTML {
  @Override
  public Report create() {
   return new HTMLReport();
  }
 };

 public abstract Report create();
}

With a specified ReportType enum constant, I could then easily create a new report by executing a statement like the following:

ReportType.CSV.create()

I wanted to get the opinion of others on using this approach. What do you think of this? Would you prefer any other approach, and if so, why?

Thanks


I think both approaches are ok, but if you don't want to know which kind of report you're generating then I believe that the enum approach is the best. Like so:

public class Person { 
    private String name;
    private ReportType myPreferedReportType;

    public ReportType getMyPreferedReportType(){
        return this.myPreferedReportType;
    }
    //other getters & setters...
}

supose you persist a Person instance on a database and retrieve it later on - if you use polimorphism you won't need any switch wathsoever. The only thing you'll need to do is to call the create() method. Like:

Person person = null; 
//... retrieve the person instance from database and generate a 
//report with his/her prefered report type...
Report report = person.getReportType.create();

So if you rely on polimorphism you won't need to ask a factory to get you the CVS/HTML/PDF explicitly, leaving that work to the Enum itself. But of course, there are situations that you may need to use one or another, although I tend to use the enum approach regularly.


What is the advantage you gain by using enum for instance of Report creation ? If you had factory-method you would have created an instance of CSVReport (say) like below:

Report csvReport = ReportFactory.createCSVReport();

which I think conveys the intent better than the enum. As I understand Enumerations represent a fixed set of constants, and using it as a factory for instance creation (though works) seems to me misuse of the intent of Enumeration.


Look at the Enum with Visitor Pattern. With that approach you'll be able to dynamically add functionality to an enum without having to pollute the enum itself.


Joshua Bloch (recognized Java expert) actually recommends this approach in his book Effective Java 2nd Edition on page 17: Enforce the singleton property with a private constructor or an enum type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜