在java代码中重构太多参数

时间:2016-09-21 17:19:55

标签: java refactoring

我想重构下面的程序。它有太多的参数气味。我怎么能重构它?

    int n = rmv2.lod[0].VerticesCount[0];

    float[] posX = new float[n];
    float[] posY = new float[n];
    float[] posZ = new float[n];
    float[] w = new float[n * 3];
    for (int i = 0; i < n; i++)
    {
        posX[i] = rmv2.lod[0].Mesh[0].Vertex[i].X;
        posY[i] = rmv2.lod[0].Mesh[0].Vertex[i].Y;
        posZ[i] = rmv2.lod[0].Mesh[0].Vertex[i].Z;
        w[i * 3 + 0] = rmv2.lod[0].Mesh[0].Vertex[i].X;
        w[i * 3 + 1] = rmv2.lod[0].Mesh[0].Vertex[i].Y;
        w[i * 3 + 2] = rmv2.lod[0].Mesh[0].Vertex[i].Z;
    }

1 个答案:

答案 0 :(得分:0)

在你的情况下,我会建议Builder模式。它很容易在java中实现。它有助于封装对象的创建过程。只需将PDFExportData构造函数声明为private(为方便起见)并在其中添加PDFExportDataBuilder静态类。

以下是一个例子:

public class PDFExportData extends ProjectExportData {

    final String myStylesheet;

    public static class PDFExportDataBuilder {

        private GanttExportSettings myExportSettings;
        private File myOutputFile;
        private String myProjectName;
        private String myProjectDescription;
        private String myOrganization;
        private HumanResourceManager myResourceManager;
        private TaskContainmentHierarchyFacade myTaskHierarchyFacade;
        private GanttChartExportProcessor myGanttChartExportProcessor;
        private ResourceChartExportProcessor myResourceChartExportProcessor;
        private String stylesheet;

        public PDFExportDataBuilder setMyExportSettings(GanttExportSettings myExportSettings) {
            this.myExportSettings = myExportSettings;
            return this;
        }

        public PDFExportDataBuilder setMyOutputFile(File myOutputFile) {
            this.myOutputFile = myOutputFile;
            return this;
        }

        public PDFExportDataBuilder setMyProjectName(String myProjectName) {
            this.myProjectName = myProjectName;
            return this;
        }

        public PDFExportDataBuilder setMyProjectDescription(String myProjectDescription) {
            this.myProjectDescription = myProjectDescription;
            return this;
        }

        public PDFExportDataBuilder setMyOrganization(String myOrganization) {
            this.myOrganization = myOrganization;
            return this;
        }

        public PDFExportDataBuilder setMyResourceManager(HumanResourceManager myResourceManager) {
            this.myResourceManager = myResourceManager;
            return this;
        }

        public PDFExportDataBuilder setMyTaskHierarchyFacade(TaskContainmentHierarchyFacade myTaskHierarchyFacade) {
            this.myTaskHierarchyFacade = myTaskHierarchyFacade;
            return this;
        }

        public PDFExportDataBuilder setMyGanttChartExportProcessor(GanttChartExportProcessor myGanttChartExportProcessor) {
            this.myGanttChartExportProcessor = myGanttChartExportProcessor;
            return this;
        }

        public PDFExportDataBuilder setMyResourceChartExportProcessor(ResourceChartExportProcessor myResourceChartExportProcessor) {
            this.myResourceChartExportProcessor = myResourceChartExportProcessor;
            return this;
    }

        public PDFExportDataBuilder setStylesheet(String stylesheet) {
            this.stylesheet = stylesheet;
            return this;
        }

        public PDFExportData create() {
            return new PDFExportData(this);
        }
    }

    private PDFExportData(PDFExportData.PDFExportDataBuilder b) {
        super(b.myExportSettings, b.myOutputFile, b.myProjectName, b.myProjectDescription, b.myOrganization, b.myResourceManager, b.myTaskHierarchyFacade, b.myGanttChartExportProcessor, b.myResourceChartExportProcessor);

        myStylesheet = b.stylesheet;
    }
}

在此之后,实例化很容易安静,而不是

public static void main(String [] args)抛出异常{

    PDFExportData pdf = new PDFExportData.PDFExportDataBuilder()
            .setMyExportSettings(new GanttExportSettings())
            .setMyGanttChartExportProcessor(new GanttChartExportProcessor())
            .setMyGanttChartExportProcessor(new GanttChartExportProcessor())
            /// ....
            .create();
}

在这种情况下,您必须确保将所有必需的参数传递给构建器。

作为替代方法,您可以创建一些Bundle,它存储必须传递给PDFExportData构造函数的所有值。