设计界面现在需要比

时间:2016-04-09 19:11:36

标签: design-patterns

解释用例:

public class Course {
    Mathematics math;
    Physics physics;
    English english;
    String teacher;
    String schoolName; //Just want explaing modelling hence keeping
}

public class PhysicsHelper extends Helper {
    public void help(Physics physics, String teacher) {
        Course course = // create course
        help(course);
    }
}

public class MathematicsHelper extends Helper {
    public void help(Mathematics math, String teacher, Address address) {
        String schoolName = address.getSchoolName
        Course course = // create course            
        help(course);
    }
}

public abstract class Helper {
    public void help(Course couse) {
        // defualt set of operations to be called by all helpers.
    }
}

现在,我有一个类似的要求:

PhysicsHelper也开始了解英语, 所以现在PhysicsHelper有方法

public class PhysicsHelper extends Helper {
    public void help(Physics physics, String teacher, English english) {
    }
}
  1. 这是建模此用例的正确方法吗
  2. 如果将来PhysicsHelper需要更多参数,是否应该对help()方法进行更改?

1 个答案:

答案 0 :(得分:2)

正如@Charles Follet建议的那样,最好在help(...)中重载PhysicsHelper.class方法,而不是在一种方法中处理所有可能的主题。另外,您可以使Course.class仅包含所有当前主题的共享字段。

public class Course {
    String teacher;
    String schoolName; //Just want explaing modelling hence keeping
}

public class EnglishCourse extends Course {
    English english;
}

public class PhysicsCourse extends Course {
    Physics physics;
}

public class PhysicsHelper extends Helper {
        public void help(Physics physics, String teacher) {
        }
        public void help(English english, String teacher) {
        }
}