Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 设计模式-在这种情况下使用哪种设计模式_Java_Oop_Design Patterns_Decorator - Fatal编程技术网

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

Java 设计模式-在这种情况下使用哪种设计模式,java,oop,design-patterns,decorator,Java,Oop,Design Patterns,Decorator,我创建了一个用于存储和操作数据集的Dataset类。另一个称为Dataset Iris的类扩展了Dataset,因为Dataset是主体类,每个不同的数据集(Iris等)都扩展了Dataset,因为它们有自己的特性和不同的加载方法(我可以从.txt文件、.data或database等文件加载) 实际上,我的代码是这样并运行的,但我的老师告诉我,我应该应用“Decorator”设计模式来解决它,但看Decorator UML我不这么认为,因为我没有“具体的组件”(我可以创建)。你怎么看?是Deco

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

实际上,我的代码是这样并运行的,但我的老师告诉我,我应该应用“Decorator”设计模式来解决它,但看Decorator UML我不这么认为,因为我没有“具体的组件”(我可以创建)。你怎么看?是Decorator设计模式还是其他(像模板方法)

数据集

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;}
}
公共类数据集
{
私人内部设施;
私人内部记录;
使用私有ArrayList功能;
私有字符串数据集;
//公共双垫[];
私人ArrayList Mat;
公共双区(int i,ArrayList centroide)
{
双和=0;

例如(int j=0;jI会说这取决于您是否希望附加除Iris之外的其他功能,比如说
DatasetIrisRose
等,如果是,则decorator应该可以。数据集是任何其他真实数据集的骨架。在我的程序中,我创建了一个具体数据集的实例(例如Iris),但数据集将永远不会被实例化。1.如果数据集只是一个骨架,则应将其抽象化。2.我们称之为扩展“is-a关系”。但装饰器模式由“has-a关系”组成。一般来说,您应该更喜欢“has-a关系”而不是“is-a关系”。@TobiasOtto我可以这样做吗?“接口数据集”{/*content主体方法/}'…'抽象类DatasetDecorator实现Dataset{/谁扩展了这个抽象类,使用了这个抽象类的结构数据,或者直接扩展了Dataset*/}'…'类DatasetIris扩展了DatasetDecorator{/*这个添加名称功能,如何加载,格式化Dataset等等*/}“@Giuseppeacardo我认为这不是讨论你的问题的最好方式。也许你可以用一个简化的例子来研究装饰图案。我建议你不要只使用尽可能多的图案来使用设计图案。你必须了解每种图案给你的解决方案带来的力量。
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()));
    }

}