【问题标题】:UML diagram confusionUML 图混淆
【发布时间】:2017-08-08 22:02:31
【问题描述】:

大家好,这是我在 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 Controller extends Sensor {
    protected String name;

    public Controller(String name) { // overridden constructor
        this.name = name;
    }

    public Controller(String name, int value, String location) { // overridden
                                                                    // instructor
        this.name = name;
        super.value = value;
        super.location = location;
    }

    public Controller() { // default constructor, which creates a new Sensor()
        //Sensor s = new Sensor();
    }

    protected void checkTemperature() { // checks temperature of sensor
        System.out.println("Temperature of " + name + " is " + super.value + ", located at " + super.location + ".");
    }
}

public class E3 {

    public static void main(String[] args) {
        Controller control = new Controller();
        control.displaySenzorInfo();

        Controller c = new Controller("Pizza", 30, "North");
        c.checkTemperature();
    }
}

我的代码

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 Controller{

    Sensor tempSensor;
    String name;
    Controller(){
        name="Sibiu";
        tempSensor=30;
    }
    Controller (String name,Sensor tempSensor){
        this.name=name;
        this.tempSensor=tempSensor;
    }
    void checkTemperature(Sensor tempSensor){
       if (tempSensor>=30)
           System.out.println("the temperature is too high!");
           else
           System.out.println("the temp is too low" );

   }

}

public class E3{

     public static void main(String []args){
        Sensor s1=new Sensor();
        Controller c1=new Controller();
        c1.displaySenzorInfo();
        Controller c2=new Controller(30,"Oliver");

     }
}

请各位。如果您有一些建议或者您在 m 程序中发现任何问题,请告诉我。我知道我会遇到一些错误,因为我没有在任何 IDE 中进行此练习,因为我在工作并且我没有任何 .谢谢!!!

【问题讨论】:

  • 附带说明:您的温度传感器永远不会告诉温度会很好。这是因为您没有定义阈值。或者,您可以检查最低/最高温度,只有在两者都在外面时才发出呱呱叫声。

标签: java uml


【解决方案1】:

您的解决方案是正确的。正如您已经提到的,它是一个关联而不是继承。您可以在 wikipedia 上查看继承的样子:https://en.wikipedia.org/wiki/Class_diagram

【讨论】:

  • 非常感谢!
【解决方案2】:

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

如果此图仅用于练习目的,则可以,否则将违反数据隐藏并鼓励客户端类违反封装(直接使用其他人的数据)

  1. tempSensor=30; 的数据类型不正确。
  2. if (tempSensor>=30) 的数据类型不正确,即使您更正了,它也违反了封装(适用于其他人的数据),这是第一次违反将实例变量设为非私有的结果。类应该处理自己的数据。
  3. 即使出于某种原因我们接受了上述违规行为,checkTemperature(Sensor tempSensor) 也会使用新的 Sensor 实例(对于每个调用),这不是从关联关系中获得的。这个方法不应该有参数,它应该在 this.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
    }
}

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

【讨论】:

  • 那么你建议我的 Controller 类应该是什么样子?
  • @Oliver 我已根据您的问题更新了我的答案
  • 哇......这是一些很酷的东西......知道我理解你提到的封装。但我没有放任何访问修饰符,因为我认为“~”表示包,我不需要放任何一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 2020-02-23
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多