Best Design for Conditional Booking System

I am building a flight booking system from the specifications of my OOP class at college. The system must be written in C #. I am wondering what is the best way to solve the following problem:

The company is currently using a discount scheme. Westerners get a 10% discount. Scotia also registers the island of residence of these passengers for marketing purposes. Business travelers receive a 25% discount and must provide the name of their company. Regular passengers do not usually receive a discount if it is not part of the current promotion, in which case they receive a 5% discount.

Do I have to have a passenger class from which each individual client type is inherited? Any help on this would be appreciated.

+6
source share
4 answers

In my opinion, you do not want to inherit from the Passenger. Inheritance implies that the object is changing in some way, but the passenger is a passenger no matter what discount he receives. In other words, its functionality does not change just because it gets a big discount.

This example that you use is very close to the examples usually indicated for the Decorator template, although this is usually explained by the fact that it demonstrates that a lot of discounts can be applied to the decorated object (passenger in your case). Take a look at the coffee example on the wiki here

Another possibility is the Strategy template, it gives you a clean interface for creating a ticket for a passenger, and inside it it switches DiscountStrategy depending on what type of passenger is requesting a ticket.

+7
source

You want the discount policy to be flexible, the passengers are actually not so different.

You may have a Customer base class from which Passenger and Company are derived, but this does not affect how you apply the discount logic.

Inheritance implies different behavior, but the behavior of passengers does not change, only the applied discounts and discount should not be part of the passenger’s state, therefore he does not fit into this class. In addition, many discounts have been active only for some time, and it makes no sense to code this in the Customer class, you should be able to add and remove them dynamically.

Since discount logic is actually a business rule that the client applies, it must be encapsulated in a separate set of classes. The general template used to implement it is strategy . Depending on your design, since you want to apply different policies for different types of users, you may find the visitor template useful or not.

0
source

I would use a strategy template as described in previous posts. "Another possibility is a strategy template, it gives you a clean interface for creating a ticket for a passenger, and inside it it switches DiscountStrategy depending on what type of passenger is requesting a ticket."

0
source

when it comes to shemes skims, it can be quite simple to use a decorator template as described by others.

Then you should check the type of interface, not the passenger, if you want, for example, indicate the number of business travelers or place of residence for marketing purposes, etc.

-1
source

All Articles