2010-12-03 144 views
1

我有2個對象Area和SurfBreak。 Area有許多SurfBreaks,SurfBreak根據Area的風,浪潮和潮汐信息發佈其條件。這一點我做了一個很好的工作:-)面向對象編程方法問題

我現在有區域的預測數據列表 - 未來更改區域的屬性。

什麼是使用區域預測數據顯示Surfbreaks條件的最佳OOP方法?

非常感謝 安迪

----更新---

它的一個Rails應用程序

class Spot < ActiveRecord::Base 
    belongs_to :area 
    has_many :forecasts, :through => :area 

def has_swell 
     wind = "#{area.swelldir}" 
     beachstart = "#{breakstr}" 
     beachend = "#{breakend}" 
     if ( ((wind.to_i) + 360 - (beachstart.to_i)) % 360 <= ((beachend.to_i) + 360 - (beachstart.to_i)) % 360 ) 
        "#{area.swelldir} Has Incoming swell " 
      else 
        "#{area.swelldir} No Swell" 

     end 
    end 

class Area < ActiveRecord::Base 
    has_many :spots 
    has_many :forecasts 

class Forecast < ActiveRecord::Base 
    belongs_to :area 

的數據庫表在軌的對象。我有Area和Spot很好地工作,但我現在想要顯示某個地點的預測。這是我不確定的一點。

mysql> desc areas; 
+----------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+----------+--------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| name  | varchar(255) | NO |  | NULL |    | 
| descrip | varchar(255) | YES |  | NULL |    | 
| winddir | int(11)  | NO |  | NULL |    | 
| windspd | int(11)  | NO |  | NULL |    | 
| swelldir | int(11)  | NO |  | NULL |    | 
| swellhgt | float  | NO |  | NULL |    | 
| tide  | int(11)  | NO |  | NULL |    | 
| lat  | float  | YES |  | NULL |    | 
| lng  | float  | YES |  | NULL |    | 
+----------+--------------+------+-----+---------+----------------+ 
10 rows in set (0.00 sec) 

mysql> desc spots; 
+----------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+----------+--------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| name  | varchar(255) | NO |  | NULL |    | 
| descrip | varchar(255) | NO |  | NULL |    | 
| breakstr | int(11)  | NO |  | NULL |    | 
| breakend | int(11)  | NO |  | NULL |    | 
| offstr | int(11)  | NO |  | NULL |    | 
| offend | int(11)  | NO |  | NULL |    | 
| besttide | int(11)  | NO |  | NULL |    | 
| area_id | int(11)  | NO |  | NULL |    | 
+----------+--------------+------+-----+---------+----------------+ 
9 rows in set (0.00 sec) 

mysql> desc forecasts; 
+--------------+----------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+--------------+----------+------+-----+---------+----------------+ 
| id   | int(11) | NO | PRI | NULL | auto_increment | 
| forecastdate | datetime | YES |  | NULL |    | 
| area_id  | int(11) | NO |  | NULL |    | 
| winddir  | int(11) | NO |  | NULL |    | 
| windspd  | int(11) | NO |  | NULL |    | 
| swelldir  | int(11) | NO |  | NULL |    | 
| swellhgt  | float | NO |  | NULL |    | 
| tide   | int(11) | NO |  | NULL |    | 
+--------------+----------+------+-----+---------+----------------+ 
8 rows in set (0.00 sec) 

所以說一個區域在數據庫中有24個預測行,在未來每小時一個。在我的應用 什麼是輸出點預測條件的最佳方式。在不改變「區域」相關值的情況下,「區域」保持現有條件。我可以通過改變Area對象的數據將所有的預測數據放到一個數組中循環,但是這對我來說看起來不太符合OOP?

由於輸出I像

Current Spot Details (Using spot methods on Area attributes) 
xxx 


Forecast Details for this spot (Using spot methods on Forecast attributes) 
Hour 1 xxx 
Hour 2 xxx 
Hour 3 xxx 
.. 

對不起後我這是不是很好的解釋。

問候 安迪

+0

我們需要查看代碼才能幫助您。 – hvgotcodes 2010-12-03 17:30:43

+0

我已經添加了一些更多的信息 – AndyM 2010-12-03 19:36:20

回答

1

您的班級區域聽起來像是在做太多的事情,而且由於不同的原因正在改變。將它分開,這樣Area有一個WeatherData列表或其他東西,所以你的預測代碼可以遍歷WeatherData而不必更改Area。您的WeatherData對象可以包含一個標誌,說明它是真實數據還是預測。

1
Class Area{ 
Wind wind; 
Wave wave; 
Tide tide; 
} 

Class SurfBreak extends Area{ 
//some SurfBreaks' field 

public ForecastDetail getForecastDetail(){ 
//operate directly onwind wave tide fields and calculate 
} 

} 
+0

@ org.life.java比我的回答要好得多;) – hvgotcodes 2010-12-03 17:35:20

0

你沒有解釋你究竟是如何發展的問題的第一部分,但(面積和SurfBreaks之間的關係),我會考慮在這裏使用Observer設計模式。因此,SurfBreaks將成爲區域變化的觀察員。