Java 如何为简单的学校应用程序设计对象

Java 如何为简单的学校应用程序设计对象,java,oop,object,Java,Oop,Object,我想创建一个简单的学校应用程序,为学生、老师和家长提供分数、笔记、状态等。我正试图为这个问题设计对象,但我有点困惑——因为我在课堂设计方面不是很有经验。我目前的一些目标是: class PersonalData() { private String name; private String surename; private Calendar dateOfBirth; [...] } class Person { private PersonalData

我想创建一个简单的学校应用程序,为学生、老师和家长提供分数、笔记、状态等。我正试图为这个问题设计对象,但我有点困惑——因为我在课堂设计方面不是很有经验。我目前的一些目标是:

class PersonalData() {
    private String name;
    private String surename;
    private Calendar dateOfBirth;
    [...]
}

class Person {
    private PersonalData personalData;
}

class User extends Person {
    private String login;
    private char[] password;
}

class Student extends Person {
    private ArrayList<Counselor> counselors = new ArrayList<>();
}

class Counselor extends Person {
    private ArrayList<Student> children = new ArrayList<>();
}

class Teacher extends Person {
    private ArrayList<ChoolClass> schoolClasses = new ArrayList<>();
    private ArrayList<Subject> subjects = new ArrayList<>();
}
class PersonalData(){
私有字符串名称;
私有字符串名称;
私人日历出生日期;
[...]
}
班主任{
个人资料;
}
类用户扩展Person{
私有字符串登录;
私有字符[]密码;
}
班级学生延伸人{
私人ArrayList顾问=新建ArrayList();
}
班主任延伸人{
private ArrayList children=new ArrayList();
}
班主任延伸人{
private ArrayList schoolClasses=新建ArrayList();
private ArrayList subjects=new ArrayList();
}
这当然是一个总体思路。但我肯定这不是最好的办法。例如,我希望一个人可以是老师,也可以是家长(辅导员),而现在的方法使我有两个人的对象。我希望该用户在成功登录后获得其拥有的所有角色(学生或教师或(教师和家长))。我认为我应该制作和使用一些接口,但我不确定如何正确地做到这一点。也许是这样:

interface Role {
}

interface TeacherRole implements Role {
    void addGrade( Student student, Grade grade, [...] );
}

class Teacher implements TeacherRole {
    private Person person;
    [...]
}

class User extends Person{
    ArrayList<Role> roles = new ArrayList<>();
}
接口角色{
}
接口教师角色实现角色{
无效添加年级(学生,年级,[…]);
}
班主任实施教师角色{
私人;
[...]
}
类用户扩展Person{
ArrayList角色=新建ArrayList();
}
如果有人能帮我纠正这一点,或者只是给我指一些涉及实用物体设计的文献/文章。

没有“正确”的设计;这完全取决于您计划如何与这些类/接口交互。试着以最自然的方式勾勒出你打算调用的方法,并从中了解什么是好的类布局。如果你觉得勇敢,试着学习测试驱动的开发方法;在“真正的”代码之前编写实际的单元测试有助于确定类结构


作为一个一般性建议,尽量避免继承,而倾向于组合。拥有一系列
角色
元素是朝着这个方向迈出的一步;尝试了解您计划与这些角色交互,并相应地添加方法。

在这样的系统中,您似乎可以创建一个包含所有个人属性和帐户信息的用户类:

public class User
{
   // personal properties
   private String name;
   private String surname;
   private Calendar dateOfBirth;

   // account properties;
   private String login;
   private String password; // note that string may be more convenient than char[]

   // role properties
   public ArrayList<Role> roles;
   ...

   public bool hasRole(Role role) // or isInRole(Role role)
   { // implementation here. }
}
请注意,只有一个角色类可以是教师、学生、家长等中的任何一个。由于角色类是泛型的,因此我们没有addGrade()之类的函数,因为它是特定于教师的

当用户使用正确的凭据登录时,这样的系统将已经知道与用户关联的角色。通常,特定于角色的选项卡、链接和其他UI元素会根据角色显示(或不显示)。在这里,您可以检查登录的用户是否处于特定角色(user.hasRole(…)。对于其可见性由角色决定的每个UI元素,必须有一个if(user.hasRole(…)

在组合问题上,这个系统严重依赖对象之间的关系。让我们考虑学生和辅导员之间的关系——一个辅导员把学生分配给他/她。同样,每个学生都有很多辅导员。你有很多关系,需要一个结构来跟踪独特的学生顾问组合:

public class StudentCounselor
{
     public User student;
     public User counselor;
}
谁来跟踪这一切?很可能是系统本身,而不是其他用户

public class SystemAdministration
{
     public static ArrayList<StudentCounselor> studentCounselors = new ArrayList<StudentCounselor>();

     public static void addStudentCounselor(User student, User counselor)
     {
         //  Check to see first that the student-counselor combo doesn't exist

         studentCounselors.addItem(student, counselor);
         // addItem may not be the precise name of the function in ArrayList.
     }

     // function to obtain all students of a counselor
     public static ArrayList<User> getStudentsOfCounselor(User counselor)
     {
         // iterate through the studentCounselors ArrayList and pick only
         // the Student-Counselor whose counselor is the same counselor
         // as the one passed into this function.
         // Then extract the student property out of the fitting 
         // Student-Counselor.
         // Return the list of students. 
     }

     public static ArrayList<User> getCounselorsOfStudent(User student)
     {
         // Similar as above, but the other way around.
     }

}
您必须创建时间段和地点类。这个结构,当放入ArrayList中时,将能够回答以下问题:“作为一名教师,我将教授哪些部分?”这回答了“我将在何时何地教授哪些科目?”

对于学生,您需要学生部分的“组合”课程:

当放入SystemAdministrator类的ArrayList中时,现在您可以遍历该列表以提取分配给学生的部分(也称为学生的时间表),同样,也可以提取指定部分的学生


请注意,除了角色之外,用户类中没有相关项的列表。要获取任何数据,只要您拥有全局(在本例中为系统管理)结构中的所有数据和访问功能,有关登录用户及其角色的信息就足够了。

这不应该在programmers.stackexchange.com上吗?
public class SystemAdministration
{
     public static ArrayList<StudentCounselor> studentCounselors = new ArrayList<StudentCounselor>();

     public static void addStudentCounselor(User student, User counselor)
     {
         //  Check to see first that the student-counselor combo doesn't exist

         studentCounselors.addItem(student, counselor);
         // addItem may not be the precise name of the function in ArrayList.
     }

     // function to obtain all students of a counselor
     public static ArrayList<User> getStudentsOfCounselor(User counselor)
     {
         // iterate through the studentCounselors ArrayList and pick only
         // the Student-Counselor whose counselor is the same counselor
         // as the one passed into this function.
         // Then extract the student property out of the fitting 
         // Student-Counselor.
         // Return the list of students. 
     }

     public static ArrayList<User> getCounselorsOfStudent(User student)
     {
         // Similar as above, but the other way around.
     }

}
public class Section
{
    public User teacher; // who teaches it
    public Course course; // what is the subject, because > 1 teacher might teach the same one.
    public TimeBlock timeBlock; // when is this section administered?
    public Venue venue; // what room or what facility
}
public class StudentSection
{
    public Section section;
    public User student;
}