2017-08-07 111 views
2

嗨,大家好,这是我在计算器上UML图混乱

第一个问题,我是一种新的Java和我需要解决这个UML图。 我从我的同学那里得到了一个解决方案,但我认为这不是正确的,我是这样做的。我的问题是哪一个解决方案是正确的?我知道关系的类型是一种关系。没有继承

the uml diagram I want to solve

她的代码

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中都没有在这个练习中工作,因为我在工作,而我没有任何工作。谢谢!!!

+0

正如旁注:你的温度传感器永远不会告诉温度会很好。这是因为你没有定义阈值。或者,您可以检查最低/最高温度,并且只在两者之外发出cro cro声。 –

回答

1

您的解决方案是正确的。正如你已经提到的那样,它是一种关联而不是遗产。您可以在维基百科上看到继承的外观如何:https://en.wikipedia.org/wiki/Class_diagram

+0

非常感谢! – Oliver

+0

http://www.uml-diagrams.org也是查找参考示例的好源。 –

0

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

如果这图是只为锻炼目的的确定,否则会违反数据隐藏,并鼓励客户端类违反封装(直接使用别人的数据)

  1. tempSensor=30;不是数据类型是正确的。
  2. if (tempSensor>=30)对于数据类型是不正确的,即使您更正了,它也会违反封装(对其他人的数据有效),这是第一次违反非私有实例变量的效果。类应该在他们自己的数据上工作。
  3. 即使由于某种原因我们接受上述违规,checkTemperature(Sensor tempSensor)利用传感器的新实例(针对每个呼叫),这不是从关联关系获得的传感器。这个方法不应该有参数,它应该在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()方法。因此它保持封装。

+0

那么你认为我的Controller类应该是什么样的? – Oliver

+0

@Oliver我根据你的问题更新了我的答案 –

+0

WOW ...这是一些很酷的东西..知道我理解你提到的封装。但是我没有加入任何修改器,因为我认为“〜”是指包,我不需要放任何修改器。 – Oliver