簡體   English   中英

兩種不同的實現

[英]Two different implementations

我想對過濾器進行兩種不同的實現,它僅返回那些得分至少為50%的學生。 我的問題是如何正確實現這兩種方法,首先我不明白為什么它不起作用,因此我需要您的幫助。

具有兩種實現的過濾器類:

package u7a1;

import java.util.ArrayList;

class Filter implements IFilter {
    public ArrayList filterRaw(ArrayList groups)
    {
        ArrayList<Boolean> allstudents = new ArrayList();
        ArrayList<Student> students = new ArrayList();
        ArrayList pass = new ArrayList();
        for (int i = 0; i < groups.size(); i++)
        {
            if ((Integer)groups.get(i) >= 50) allstudents.add(true);
            else allstudents.add(false);
        }
        for (int i = 0; i < groups.size(); ++i)
        {
            if ((Boolean)allstudents.get(i) == true) pass.add(groups.get(i));
        }
        return pass;
    }

    public ArrayList<Student> filterGeneric(ArrayList<ArrayList<Student>> groups)
    {
        ArrayList<Boolean> allstudents = new ArrayList();
        ArrayList<Student> students = new ArrayList();
        ArrayList pass = new ArrayList();
        /*for (int i = 0; i < groups.size(); i++)
        {
            Integer mystudentpoints = new Integer(0);
            mystudentpoints = Student.getPoints();
            if (Student.getPoints() >= 50) allstudents.add(true);
            else allstudents.add(false);
        }*/
        for (int i = 0; i < groups.size(); ++i)
        {
            if ((Boolean)allstudents.get(i) == true) pass.add(groups.get(i));
        }
        return pass;
    }
}

過濾器工廠已經正確:

package u7a1;

/**
 * Factory for "Testat" filters 
 */
public class FilterFactory {
    /**
     * Create a "Testat" filter
     * @return a "Testat" filter
     */
    public static IFilter create()
    {
        // TODO
        return new Filter();
    }
}

這是由類實現的接口,其中方法無法正常工作:

package u7a1;

import java.util.ArrayList;

/**
 * Filters for students who obtain the "Testat".
 * 
 * The requirements for the testat are to have at least {@link IFilter#criteria} percent 
 * of {@link IFilter#maxNumberofPoints} points.
 */
public interface IFilter {
    public final int maxNumberofPoints = 80;
    public final double criteria = 50;

    /**
     * Filter all students that achieved enough points for the "Testat".
     * 
     * @param groups an ArrayList of groups, where a group is an ArrayList of students
     * @return the ArrayList of all students who achieved enough points for the "Testat".
     */
    public ArrayList filterRaw(ArrayList groups);

    /**
     * Filter all students that achieved enough points for the "Testat".
     * 
     * @param groups an ArrayList of groups, where a group is an ArrayList of students
     * @return the ArrayList of all students who achieved enough points for the "Testat".
     */
    public ArrayList<Student> filterGeneric(ArrayList<ArrayList<Student>> groups);
}

我不對主類進行更多的編輯:

/**
 * Main class of the Java program. 
 * 
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("-- Array-Listen und Generics --");
        /* you can make function calls from here*/
    }
}

最后,我有了不需要任何更改的Student類:

package u7a1;

public class Student {
    private String name;
    private int legi;
    private int points;

    public Student(String name, int legi)
    {
        this.name = name;
        this.legi = legi;
        points = 0;
    }

    public int getLegi()
    {
        return this.legi;
    }

    public String getName()
    {
        return this.name;
    }

    public Student setPoints(int points)
    {
        this.points = points;
        return this;
    }

    public int getPoints()
    {
        return points;
    }

    public String toString()
    {
        return String.format("%s (%d)", name, legi);
    }
}

項目的核心是Main.class,將執行所有操作。 僅聲明其他類,但從未使用過。 當您運行程序時,它以main方法中的命令開始。 當前,您唯一要打印的是帶有文本的控制台字符串:“-Array-Listen und Generics-”

現在,您需要准備一些要過濾的數據,例如

Student student1 = new Student("John", 1);
student1.setPoints(51);
Student student2 = new Student("Alice", 2);
student1.setPoints(48);

List<Student> studentList = new ArrayList<>();
studentList.add(student1, student2);

假設您有測試數據,現在需要使用過濾器。要使用過濾器,我們需要使用其實現

IFilter myFilter = FilterFactory.create();

現在,myFilter是實現的實例,要使用它,您需要調用帶有聲明數據的方法

List<Student> result = myFilter.filterRaw(studentList);

該方法的示例實現將是

public List<Student> filteredStudents(List<Student> input) {
    return input.stream().filter(s -> s.getPoints() > 50).collect(Collectors.toList());
}

但是您可能會注意到,接口的聲明也需要更改以具有相同的類型。

實現2種類型的過濾行,策略設計模式將幫助您這是一篇很好的文章http://www.oodesign.com/strategy-pattern.html ,我認為您必須像

  public ArrayList filterRaw(ArrayList groups)
{
    ArrayList pass = new ArrayList();
    for (int i = 0; i < groups.size(); i++)
    {
        if ((Integer)groups.get(i) >= 50) pass.add(groups.get(i));

    }
    return pass;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM