Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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
Design patterns 现在比以前需要更多参数的设计接口_Design Patterns - Fatal编程技术网

Design patterns 现在比以前需要更多参数的设计接口

Design patterns 现在比以前需要更多参数的设计接口,design-patterns,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(Ph

解释用例:

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) {
    }
}
  • 这是建模这个用例的正确方法吗
  • 如果PhysicsHelper将来需要更多参数,是否应该更改help()方法

  • 正如@Charles Follet所建议的,最好在
    PhysicsHelper.class
    中重载
    help(…)
    方法,而不是在一个方法中处理所有可能的主题。此外,您还可以制作
    课程。课程
    只包含所有当前科目的共享字段

    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) {
            }
    }
    

    你最好延长英语和物理课程。然后你可以打两次电话求助。每门课程一次。我希望您不要为助手理解的每一件事情添加参数。