0

I have a Flight class which currently does nothing else but hold lots of variables containing details on a given commercial flight.

Most of these details are just passed through the constructor but some of them need to be calculated or formatted. For example I format the flight arrival time in to a string saying "6th June" and sometimes the airport name is missing from the initial flight data so I use an array of airportcodes and airportnames to find that airports name.

Should the methods that calculate this additional information be located within the flight class or a series of other models such as DateProcessor or Airport?

Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
  • 3
    The litmus test is, when you need to make a change; how many places do you need to make that change in. – Chris K Jul 11 '14 at 10:21
  • @Blacklight The alternative I'm thinking of would involve the Activity/Viewcontroller that calculates that data storing this data within the class. It has the downside of me having to keep track of which controller accesses the class first but stops my class becoming too chunky as I'm saving it within a database and making it serializable. – Declan McKenna Jul 11 '14 at 10:28
  • @Deco Sorry I deleted my comment because it was redundant.. Chris K is exactly right, centralize your code and DRY. Intuitively I would say the tasks your described probably belong in the Flight-class, unless maybe they are specific to a certain view. – Blacklight Jul 11 '14 at 10:38

3 Answers3

2

Chris K has correctly pointed out the litmus test i.e when you need to make a change; how many places do you need to make that change in.

OOP concepts encourage decoupling and modular programming to maximize code reuse.

The key is high cohesion and low coupling. Refer to this Cohesion & Coupling

If you thing that the additional flight details that you are deriving from Flight class variables will only the need in one activity and now where else.Then its ok to have these functions in our activity.

But, if these functions may be required by other Activities or classes currently or may require in future.Then, place these functions in your Flight Class

Ideally the best OOP will be to keep these functions that are deriving details from Flight class variables in flight class itself.As, you will be able to re use code if not now , latter in future.I will also suggest the same.

Also, if you use multiple instances of Flight class, the you should place all the functions accessing and modifying the class variables in Flight class only to maintain data consistency of each instance.

If you will stick to OOP programming techniques, it will be easier to maintain code as there will be more code reuse and no duplicate code.

There are four major principles of object oriented programming:

  • Data Abstraction

  • Encapsulation

  • Inheritance

  • Polymorphism

P.S : Can refer to http://en.wikipedia.org/wiki/Object-oriented_programming

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • 4
    This should be a comment instead of answer. – Sanjeev Jul 11 '14 at 10:26
  • 1
    @Sanjeev While that may be true, it's too lengthy to be a comment. – Etheryte Jul 11 '14 at 10:54
  • The MVC design pattern mobile applications follow and the OOP I've been taught seem to contradict each other. – Declan McKenna Jul 15 '14 at 09:17
  • Just to add to this answer, making that function `virtual(overridable)` C# terminology will give better control to child classes to override it with their own functionality. Another use case can be a template pattern http://www.oodesign.com/template-method-pattern.html – Shantanu Gupta May 30 '15 at 19:36
0

It's perfectly normal to have login, data and UI seperate. For example, JavaBeans are convention for having classes that store data. Read more here. MVC fallows similar logic, i.e. View is created from data, taken from Model, and Controller updates the data.

Community
  • 1
  • 1
Marius
  • 810
  • 7
  • 20
0

Good class design encapsulates data and provides methods to access that data. In your case you should ask yourself "is all the data related" ... think if I had a group of employees would I give this data to one of them to remember or would it make more sense to partition the data and give sections of it to number of different employees.

You already have identified the data that should go into one class so it would seem logical to put the methods that act on that data in that class as well.

The MCV Design Pattern is flexible but overkill here I think.

jpd
  • 683
  • 5
  • 8