Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Classes Methods to create?

I'm looking to create a test application which can check various flight information from an airline provider I'm struggling with the concept of classes and methods and which ones to create. My current thought process is as follows: The data is downloaded from a website, due to the size of data, I only want to download the data once. My current thinking is:

Class

BritishAirwaysFlightData()

Constructor

BritishAirwaysFlightData // Used to download the BA Flight database and store in the object (Assumging his is only small i.e. 500kb)

Methods

getStartDate(String source_airport, String dest_airport) // Takes source and destination airport and return date when flights start
getEndDate(String source_airport, String dest_airport)   // Takes source and destination airport and return date when flights finish
getDestAirports(String source_airport)                   // Takes source airport name and returns a list of destinations
getSourceAirports(String dest_airport)                   // Takes source airport name and returns a list of sources
getNumofDestinations()                                   // Returns total number of destinations

Hopefully, you get the general idea of what I'm trying to implement, but I'm just not sure if its the right way. I'd basically create an object from the Class, the constructor would then auto download the data and store in a suitable object array of some kind.

A main program would be created to allow a user to query flight info etc.

It would effectively query this object from the main program to find specific information about flights, dates etc.

Would this be the best way to implement this type of functionality bearing in mind I don't want to access the data directly and I have a restricted bandwidth so need to implement some kind of local cached version?

I'm concerned more with the actual makeup of the class/constructor/methods as opposed to the actual functionality of each method and calling parameters.

Hope this makes sense, any pointers would be much appreciated and also any reference sites with lots of real world examples like this which could improve my initial analysis of problems,

Thanks,

like image 966
bazza jones Avatar asked Apr 24 '13 12:04

bazza jones


People also ask

Which is the correct way to create a class?

When you create an object, you are creating an instance of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.

What are classes and methods?

Definition. A class is a template for creating or instantiating objects within a program while a method is a function that exposes the behavior of an object. Thus, this is the main difference between class and method.

What are classes methods in Java?

Class methods are methods that are called on the class itself, not on a specific object instance. The static modifier ensures implementation is the same across all class instances. Many standard built-in classes in Java (for example, Math) come with static methods (for example, Math.


3 Answers

The methods you outline appear to be more suited towards being on a service.

The idea is this: you want to separate your concerns.

One concern is modeling the data. That means creating a container for your data, with methods that make sense to manipulate the data. You can create a base class FlightData and a subclass BritishAirwaysFlightData if it makes sense (if the data doesn't change from airline to airline, then you just need a generic object). Looking at your methods, it might make sense to have a class for Airport and Flight as well.

Another concern is getting your data. So you probably want a class that does nothing but interact with the source of the data. These classes are typically called Data Access Objects (DAOs).

The service is yet another class that will use the data access class and the data model class together to perform a unit of work.

So think about the method getSourceAirports, which I will assume means 'get all airports from which a flight originates'. If you have a data model called Flight, you can envision that this class would have departureAirport and arrivalAirport fields (as well as others). To get the source Airports, you would query your datasource for all unique departureAirports found in the Flight table (corresponding to the Flight class).

like image 109
hvgotcodes Avatar answered Nov 01 '22 22:11

hvgotcodes


The way you're suggesting would work but I wouldn't recommend it, it scales poorly. I agree with the concept of downloading the data once, but that doesn't mean you can't implement a rich domain model and have a proper tiered architecture.

As a start I would suggest that you have a general class for FlightData:

public class FlightData {
    private FlightOperator operator;
    ...
}

Which contains the information on the flight for a specific operator (BA in your example). You can then create a service and DAO layer to separate the concerns of your application:

public interface FlightDataService {
    public FlightData find(FlightOperator operartor);
    public List<FlightData> find(List<FlightOperator> operartors);
    ...
}

Here is another question that elaborates on layered architectures:

DAO and Service layer design

like image 24
StuPointerException Avatar answered Nov 01 '22 22:11

StuPointerException


Struggling with the concept of classes is a natural part of learning the object oriented paradigm. If you're truly at an introductory level, then I wouldn't worry about ORM or Hibernate or Entity / Service classes just yet.

To help develop a roadmap of the kinds of classes you want to create, think about your project carefully. Enumerate the kinds of things that are involved in your design, eg. flights, flight operators, flight times. Developing a list of things is really the best, most important first step in class design because getting an accurate and comprehensive list will enable you to write the most useful and scalable classes.

Once you have a list of things, think carefully about how the things are organized. Which things stand alone and which are dependent, eg. flight times are dependent on flights. Dependent things, eg. flight times, are usually properties or fields of classes. Independent things, eg. flights, are often classes. Developing the relationships between the things in your project is really a critically important step in class design. Getting it right makes things so much easier for you.

As you've seen, the nouns in your project (things) are the classes and class fields or properties. Then imagine the what you want your project to do. The verbs, eg. createScheduleList, are your methods and they are written into the classes that they operate upon (or are written as static methods if they do not depend upon the fields or properties of a particular thing (or object)).

This is very noobish advice to be sure, but I can't emphasize strongly enough how important it is to begin with a good foundation of class design. Good, well-organized classes make implementing your overall project much, much more straightforward.

like image 26
scottb Avatar answered Nov 01 '22 22:11

scottb