开发者

OOP programming method question

I have 2 objects, Area and SurfBreak. Area has many SurfBreaks and a SurfBreak publishes its conditions based on wind,wave,tide info from Area. This bit I've done an it works well:-)

I now have list of forecast data for Area - future changes to Area's attributes.

Whats the best OOP method to show the Surfbreaks conditions using forecast data for Area ?

Many thanks Andy

----Updated---

Its a rails app

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

The DB tables are the objects in rails. I've got Area and Spot working nicely but I now want to display forecasts for a spot. This is the bit I'm not sure about.

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    |          开发者_StackOverflow中文版      |
| 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)

So say an Area has 24 Forecast rows in a DB , one for every hour in the future. In my app what is the best way to output a spots forecast conditions. Without changing the relevant values in the Area as Areas hold the current conditions. I could just pull all the forecast data into an array an loop through it changing the Area object data, but this doesn't seem very OOP to me ?

As output I'm after something like

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
..

Sorry if this is not very well explained.

Regards Andy


Your class Area sounds like it is doing too many things, and it is changing for different reasons. Separate it out so the Area has a list of WeatherData or something, so your forecasting code can iterate through the WeatherData without Area having to change. Your WeatherData object can include a flag saying whether it's real data or a forecast.


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
}

}


You haven't explained exactly how you developed first part of the problem (relations between Area and SurfBreaks), but, I would consider using of Observer design pattern here. So SurfBreaks would be Observers of Area changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜