设计模式 - 在这种情况下使用哪种设计模式

时间:2017-02-01 10:41:50

标签: java oop design-patterns decorator

我创建了一个用于存储和操作数据集的数据集类。另一个名为Dataset Iris的类扩展了Dataset,因为Dataset是Principal类,其中每个不同的数据集(iris等)都扩展了它,因为它们有一个特性和不同的加载方法(我可以从文件加载.txt, .data或数据库等......)。

我的实际代码是这样并运行,但我的老师告诉我,我应该应用"装饰"解决它的设计模式,但看着装饰UML,我不这么认为,因为我没有"混凝土组件" (我可以创造。你怎么看待它?是装饰设计模式还是其他(比如模板方法)?

数据集

public class Dataset
{
    private int nFeature;
    private int nRecord;
    private ArrayList<Integer> featureUsed;
    private String nomeDataset;

    //public double Mat [][];
    private ArrayList<ArrayList<Double>> Mat;


    public double Distanza(int i, ArrayList<Double> centroide)
    {
        double Sum=0;
        for(int j=0; j<nFeature; j++)
            Sum+=Math.pow((centroide.get(j) - Mat.get(j).get(i)), 2);
        return Math.sqrt(Sum);
    }

    public double getCell(int i, int j)
    {
        return Mat.get(j).get(i);
    }

    public void initMat()
    {
       Mat = new ArrayList<ArrayList<Double>>();
       featureUsed = new ArrayList<Integer>();

       for(int i=0; i< nFeature; i++)
       {
           Mat.add(new ArrayList<Double>());
           featureUsed.add(i); 
       }
    }


    public void writeDataset()
    {
        for(int i=0; i< nRecord; i++)
        {
            for(int j=0; j < nFeature; j++)
            {
                System.out.print( Mat.get(j).get(i)+ " ");
            }
            System.out.println("\n");
        }
    }

    public ArrayList<Double> getRecord(int i_r)
    {
        ArrayList<Double> record = new ArrayList<Double>();
        for(int i=0; i<nFeature; i++)
            record.add( Mat.get(i).get(i_r));

        return record;
    }

    public Dataset(int nFeature, String Nome) 
    {       
        setnFeature(4);
        setNomeDataset(Nome);
        initMat();
    }


    public Dataset(ArrayList<ArrayList<Double>> MatInput, ArrayList<Integer> featureSelected, int nRecord)
    {
        Mat = new ArrayList<ArrayList<Double>>();
        this.featureUsate = new ArrayList<Integer>(featureSelected);
        this.nRecord = nRecord;
        this.setnFeature(featureSelected.size());

        for(int i=0; i<featureSelected.size(); i++)
            setCol( MatInput.get( featureSelected.get(i)));

    }

    public Dataset() {
        // TODO Auto-generated constructor stub
    }


    public void setCol( ArrayList<Double> colVal)
    {
        this.Mat.add(colVal);
    }

    public ArrayList<ArrayList<Double>> getMat()
    {
        return this.Mat;
    }
    public int getnFeature() {
        return this.nFeature;
    }
    public int getnRecord() {
        return this.nRecord;
    }
    public void setnFeature(int nFeature) 
    {
        this.nFeature = nFeature;
        return;
    }
    public void setnRecord(int nRecord) {
        this.nRecord=nRecord;
        return;
    }

    public void setTable(int Colonna, Double Valore) 
    {
        Mat.get(Colonna).add(Valore);
    }
    public String getNomeDataset() {
        return this.nomeDataset;
    }

    public void setNomeDataset(String nomeDataset) {
         this.nomeDataset = new String(nomeDataset);
    }

    public double[][] toMatrix()
    {
        double[][] matrix = new double[this.getnRecord()][this.getnFeature()];
        for(int i=0; i< nRecord; i++)
        {
            for(int j=0; j < nFeature; j++)
            {
                matrix[i][j] = Mat.get(j).get(i);
            }
        }
        return matrix;
    }

    public ArrayList<Integer> getFeatureUsed()
    {return this.featureUsed;}
}

DatasetIris

public class DatasetIris extends Dataset
{


    private String[] nomiFeature = {"Petal Length",
                                    "Petal Width",
                                    "Sepal Length",
                                    "Sepal Width"
                                    };  

    public DatasetIris(String NomeFile) throws IOException
    {
        super(4,NomeFile);
        super.setnRecord( CaricaDataset(NomeFile) );
    }

    // Other DatasetIris with their load (database or other type of files)?
    protected int loadDataset(String pathFile) throws IOException
    {
        int iRecord = 0;
        BufferedReader bufferLetto = null;
        String line = "";
        String cvsSplitBy = ",";

        try {
            bufferLetto = new BufferedReader(new FileReader(pathFile));

            while ((line = bufferLetto.readLine()) != null) 
            {
                if (line.length() > 0) 
                {
                    String[] cell = line.split(cvsSplitBy);
                    this.addRow(cell);
                    iRecord++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferLetto != null) {
                try {
                    bufferLetto.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return iRecord;
    }

    // New File.data to Mat
    public void addRow(Object cell[]) 
    {
        for(int i=0; i<getnFeature(); i++)
            super.setTable(i, Double.parseDouble(cell[i].toString()));
    }

}

0 个答案:

没有答案