Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 enum 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

like image 383
Steve Avatar asked Jan 29 '10 16:01

Steve


People also ask

What is the factory method pattern explain with examples?

Example. The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate. Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes.

What is the use of Factory Method?

Factory Method Pattern allows the sub-classes to choose the type of objects to create. It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code.

What is Factory Method in Python?

Factory Method is a Creational Design Pattern that allows an interface or a class to create an object, but lets subclasses decide which class or object to instantiate. Using the Factory method, we have the best ways to create an object.

Is enum primitive type?

In some ways, an enum is similar to the boolean data type, which has true and false as its only possible values. However, boolean is a primitive type, while an enum is not.


2 Answers

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.

like image 180
Lucas de Oliveira Avatar answered Oct 17 '22 21:10

Lucas de Oliveira


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.

like image 21
sateesh Avatar answered Oct 17 '22 20:10

sateesh