Java UML图混乱

Java UML图混乱,java,uml,Java,Uml,大家好,这是我关于StackOverflow的第一个问题 我对java有点陌生,我需要解决这个uml图。 我从我的一个同学那里得到了一个解决方案,但我认为这是不对的,我按照自己的方式做了。我的问题是哪一个解决方案是正确的?我知道这种关系是一种联想关系。不是遗产 她的代码 class Sensor { protected int value; protected String location; public Sensor() { // default constru

大家好,这是我关于StackOverflow的第一个问题

我对java有点陌生,我需要解决这个uml图。 我从我的一个同学那里得到了一个解决方案,但我认为这是不对的,我按照自己的方式做了。我的问题是哪一个解决方案是正确的?我知道这种关系是一种联想关系。不是遗产

她的代码

 class Sensor {
    protected int value;
    protected String location;

    public Sensor() { // default constructor
        value = 0;
        location = "North-West";
    }

    public Sensor(int value, String location) { // overridden constructor
        this.value = value;
        this.location = location;
    }

    protected int getValue() { // value getter
        return value;
    }

    protected void setValue(int v) { // value setter
        this.value = v;
    }

    protected void displaySenzorInfo() { // display information on the sensor
        System.out.println("Temperature is " + value + ", located " + location + ".");
    }
}
class Sensor{

     int value;
        String location;
         Sensor(){
        value=0;
        location="Sibiu";
    }
    Sensor(int value,String location){
        this.value=value;
        this.location=location;
    }
    int getValue(){
        return value;
    }
     void setValue(int v){
        this.value=v;
    }
    void displaySenzorInfo(){
        System.out.println("Temperature is " + value + ", located " + location + ".");
    }

}


我的代码

 class Sensor {
    protected int value;
    protected String location;

    public Sensor() { // default constructor
        value = 0;
        location = "North-West";
    }

    public Sensor(int value, String location) { // overridden constructor
        this.value = value;
        this.location = location;
    }

    protected int getValue() { // value getter
        return value;
    }

    protected void setValue(int v) { // value setter
        this.value = v;
    }

    protected void displaySenzorInfo() { // display information on the sensor
        System.out.println("Temperature is " + value + ", located " + location + ".");
    }
}
class Sensor{

     int value;
        String location;
         Sensor(){
        value=0;
        location="Sibiu";
    }
    Sensor(int value,String location){
        this.value=value;
        this.location=location;
    }
    int getValue(){
        return value;
    }
     void setValue(int v){
        this.value=v;
    }
    void displaySenzorInfo(){
        System.out.println("Temperature is " + value + ", located " + location + ".");
    }

}



拜托,伙计们。如果你有一些建议,或者如果你在m程序中看到任何问题,请告诉我。我知道我会有一些错误,因为我没有在任何IDE中进行此练习,因为我正在工作,并且没有任何错误。谢谢你

您的解决方案是正确的。正如您已经提到的,它是一种关联,而不是继承。您可以在wikipedia上看到继承的样子:

尽管给定图表中关系的总体编码(MyCode)是可以的,但我有以下观察结果。(她的代码)-继承不正确。单向关联是正确的

如果这是一个仅用于练习的图表,那么它是可以的,否则它将违反数据隐藏并鼓励客户机类违反封装(直接使用其他人的数据)

  • tempSensor=30对于数据类型不正确
  • 如果(tempSensor>=30)
    对于数据类型不正确,并且即使您纠正了,它也违反了封装(对其他人的数据起作用),这是使实例变量非私有的第一个违反。类应该处理自己的数据
  • 即使出于某种原因我们接受上述违规,checkTemperature(传感器tempSensor)也会使用传感器的新实例(用于每次调用),而不是从关联关系中获得的实例。此方法不应具有参数,它应适用于此.tempSensor(具有可接受的数据泄漏)。理想情况下,这表明数据及其行为正在分离,需要纠正设计
  • 如果无法更改图表,则只需删除checkTemperature()中的参数,并处理如上所示的数据类型

    但是,我建议在设计级别进行更改,如下所示,以更好地封装

    public class SensorNew {
        private static final double UPPER_THRESHOLD = 25;
        private static final double LOWER_THRESHOLD = 20;
        private String location;
        private Controller controller;
    
        public SensorNew(String location, Controller controller) {
            this.location = location;
            this.controller = controller;
        }
    
        public int getCurrentTemp() {
            // obtain from sensor hardware
            return 10; // Just example
        }
    
        private void makePeriodicCheck(){
            double currentTemp = getCurrentTemp();
            if (currentTemp > UPPER_THRESHOLD){
                controller.coolDown();
            } else if (currentTemp < LOWER_THRESHOLD){
                controller.heatUp();
            } else {
                controller.stopIfRunning();
            }
        }
    
        public void displaySenzorInfo() { // replace by toString()
            System.out.println("Temperature is " + getCurrentTemp() 
            + ", located " + location + ".");
        }
    }
    
    public class ControllerNew {
        private String name;
        // Need to maintain the state of Controller
        // either by variable or State design pattern (preferred)
    
        public ControllerNew(String name, Sensor tempSensor) {
            this.name = name;
        }
    
        public void coolDown() {
            // action depending upon current state of controller
        }
    
        public void heatUp() {
            // action depending upon current state of controller
        }
    
        public void stopIfRunning() {
            // action depending upon current state of controller
        }
    }
    
    公共类传感器新建{
    专用静态最终双上_阈值=25;
    专用静态最终双下_阈值=20;
    私有字符串位置;
    专用控制器;
    公共传感器新建(字符串位置、控制器){
    这个位置=位置;
    this.controller=控制器;
    }
    public int getCurrentTemp(){
    //从传感器硬件获取
    返回10;//只是一个示例
    }
    私有void makePeriodicCheck(){
    双currentTemp=getCurrentTemp();
    如果(当前温度>上限阈值){
    controller.coolDown();
    }否则如果(当前温度<较低的\u阈值){
    controller.heatUp();
    }否则{
    controller.stopIfRunning();
    }
    }
    public void displaySenzorInfo(){//替换为toString()
    System.out.println(“温度为”+getCurrentTemp()
    +,位于“+位置+”;
    }
    }
    公共类控制器new{
    私有字符串名称;
    //需要维护控制器的状态
    //通过变量或状态设计模式(首选)
    public ControllerNew(字符串名称、传感器温度传感器){
    this.name=名称;
    }
    公共无效冷却时间(){
    //取决于控制器当前状态的操作
    }
    公共空间升温(){
    //取决于控制器当前状态的操作
    }
    公共void stopIfRunning(){
    //取决于控制器当前状态的操作
    }
    }
    

    优点是我们不必为这些类提供公共getXX()setXX()方法。因此它保持了封装性。

    非常感谢!作为旁注:你的温度传感器永远不会告诉你温度会好的。这是因为没有定义阈值。或者,你可以检查最低/最高温度,只有在两者都在外面的情况下才会发出咯咯声。那么你建议我的控制器类应该是什么样子?@Oliver我已经根据你的问题更新了我的答案哇。。。这是一些很酷的东西。。我知道我理解你提到的封装。但我没有放任何acces修改器,因为我认为“~”表示包,我不需要放任何修改器。
    public class SensorNew {
        private static final double UPPER_THRESHOLD = 25;
        private static final double LOWER_THRESHOLD = 20;
        private String location;
        private Controller controller;
    
        public SensorNew(String location, Controller controller) {
            this.location = location;
            this.controller = controller;
        }
    
        public int getCurrentTemp() {
            // obtain from sensor hardware
            return 10; // Just example
        }
    
        private void makePeriodicCheck(){
            double currentTemp = getCurrentTemp();
            if (currentTemp > UPPER_THRESHOLD){
                controller.coolDown();
            } else if (currentTemp < LOWER_THRESHOLD){
                controller.heatUp();
            } else {
                controller.stopIfRunning();
            }
        }
    
        public void displaySenzorInfo() { // replace by toString()
            System.out.println("Temperature is " + getCurrentTemp() 
            + ", located " + location + ".");
        }
    }
    
    public class ControllerNew {
        private String name;
        // Need to maintain the state of Controller
        // either by variable or State design pattern (preferred)
    
        public ControllerNew(String name, Sensor tempSensor) {
            this.name = name;
        }
    
        public void coolDown() {
            // action depending upon current state of controller
        }
    
        public void heatUp() {
            // action depending upon current state of controller
        }
    
        public void stopIfRunning() {
            // action depending upon current state of controller
        }
    }
    
    public class SensorNew {
        private static final double UPPER_THRESHOLD = 25;
        private static final double LOWER_THRESHOLD = 20;
        private String location;
        private Controller controller;
    
        public SensorNew(String location, Controller controller) {
            this.location = location;
            this.controller = controller;
        }
    
        public int getCurrentTemp() {
            // obtain from sensor hardware
            return 10; // Just example
        }
    
        private void makePeriodicCheck(){
            double currentTemp = getCurrentTemp();
            if (currentTemp > UPPER_THRESHOLD){
                controller.coolDown();
            } else if (currentTemp < LOWER_THRESHOLD){
                controller.heatUp();
            } else {
                controller.stopIfRunning();
            }
        }
    
        public void displaySenzorInfo() { // replace by toString()
            System.out.println("Temperature is " + getCurrentTemp() 
            + ", located " + location + ".");
        }
    }
    
    public class ControllerNew {
        private String name;
        // Need to maintain the state of Controller
        // either by variable or State design pattern (preferred)
    
        public ControllerNew(String name, Sensor tempSensor) {
            this.name = name;
        }
    
        public void coolDown() {
            // action depending upon current state of controller
        }
    
        public void heatUp() {
            // action depending upon current state of controller
        }
    
        public void stopIfRunning() {
            // action depending upon current state of controller
        }
    }