• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How should I approach this problem using OO?

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I have just taken over a code from a colleague. The scenario is this:

We are a software company which deals with air tickets. We accept airlines from all over the world.

A class has been written for a supplier who supplies tickets only for an airline.

My colleague has modified the class method to suit another supplier for another airline. Using this:



Question is: Is this the best approach to suit airline A? What if another supplier comes in and provides for Airline C? Will there be a lot of "if" statements?

How should I approach future implementations? Use a subclass? Override the method?

Please advise. Thanks.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds like a Policy to me, which is another name for the Strategy design pattern. Search for Refactoring replace conditional with strategy. Coincidentally, another recent thread here is about Strategy design pattern.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More concretely, I would probably try something like this:

If you inject the policies, you can easily add new AirlinePolicies externally, perhaps via Spring Framework DI. Whatever this class is, it provides the context in which the Policy operates, which is why you would pass a reference to this in the call to the applyTo method. Each Policy could potentially change the state of this object or if the conditions are not right to apply the policy, it can do nothing and simply return.

You could also pass in something like a Context object that will provide the information each Policy needs to operate. It all depends on the surrounding code how you end up refactoring.

 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic