2010-12-03 138 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将成为区域变化的观察员。