jeudi 28 mai 2015

Java Design Pattern Apply

I am developing one API, with following snaps of code.

RowMappable.java

package com.api.mapper;
import org.apache.poi.ss.usermodel.Row;
public interface RowMappable<T> {
  T mapRow(Row row);
}

Issue.java

package com.api.pojo;

import org.apache.poi.ss.usermodel.Cell;

/**
 * It will contain all the fields related to Issue.
 * 
 * @author vishal.zanzrukia
 * 
 */
public class Issue {

  private Cell description;

  /**
   * @return
   */
  public String getDescription() {
    if (description != null) {
      return description.getStringCellValue();
    }
    return null;
  }

  /**
   * @param description
   */
  public void setDescription(Cell description) {
    this.description = description;
  }
}

ExcelColumn.java

package com.api.excel;

import org.apache.poi.ss.usermodel.Row;
import com.api.mapper.SimpleExcelIssueMapper;
import com.api.pojo.Issue;


/**
 * @author vishal.zanzrukia
 * 
 */
public class ExcelColumn {

  private int descriptionColumnIndex;

  /**
   * This is inner class to protect visibility of mapRow method
   * 
   * @author vishal.zanzrukia
   *
   */
  class InnerSimpleExcelIssueMapper implements RowMappable<Issue> {

    @Override
    public Issue mapRow(Row row) {
      Issue issue = new Issue();
      issue.setDescription(row.getCell(descriptionColumnIndex));
      return issue;
    }
  }

  /**
   * set issue description column index<BR>
   * <STRONG>NOTE :</STRONG> index starts from <STRONG>0</STRONG>
   * 
   * @param descriptionColumnIndex
   */
  public void setDescriptionColumnIndex(int descriptionColumnIndex) {
    this.descriptionColumnIndex = descriptionColumnIndex;
  }
}

Here, ExcelColumn is the class which end user (API user) will use for mapping the excel column index with it's purpose (here, it's description for example).

Now, ExcelColumn can implements directly to RowMappable rather than inner class (InnerSimpleExcelIssueMapper), but if I do so, end user (API user) will be able to call mapRow method. I don't want to call mapRow outside the package because it will create confusion for end user (API user). So I have achieved it using inner class concept.

Is this correct way to do it? Is there any better way to achieve the same?

Is there any design pattern applicable here?

Aucun commentaire:

Enregistrer un commentaire