2017-08-07 124 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