Design Pattern is a very wonderful part of object-oriented programming. It provides flexibility, extensibility, and clean code features. Railway Ticket Price can be calculated and implemented with the help of design patterns.
Problem StatementA passenger travels from one station to another station by train and he/she needs a ticket. He pays the fare for the traveled stations and, Railways has some particular rule related to the fare. It is based on stations, senior citizens, festivals, etc.
Railway rule says "If a passenger travels up to 5 stations, then fare is only Rs. 10. But, after the five stations, for every five or less than five stations, an extra fare of Rs. 5 will be charged. If the passenger is a senior citizen, then Railway will offer them some discount. And, some additional fare will be added on the occasion of some particular festivals."
Example
Let us consider there are 15 stations named, one, two, three, four...... fifteen and the passengers are traveling with different cases.
- If passenger A travels from one to five stations, then he will be charged only Rs. 10.
- If Passenger B travels from one to six stations, then he will be charged Rs. 15.
- If Passenger C travels from six to ten station, then he will be charged Rs 15.
- If Passenger D travels from one to fifteen station, then he will be charged Rs 20.
This problem statement can be solved by using "Strategy Desing Pattern." Because different types of fare strategy can be applied based on the context. Below are the different types of fare strategies,
- Station Fare Rule Strategy
- Age Fare Rule Strategy
- Festival Fare Rule Strategy
- Distance Fare Rule Strategy
- Class Type like Sleeper, General, First Ac, Second AC, etc. Fare Rule Strategy
- Basic Fare Rule Strategy
- Train Type Fare Rule Strategy
So, each fare calculation algorithm will be written separately for each Fare Rule Strategy and these will not interfere with each other. New fare rule strategy can be added and written in case of new fare rule. So, it will also follow the concept of "Open for Extension and Close for Modification".
Class Diagram
Code Explanation
IFareStrategy Interface
This interface defines the common strategy for the calculation of fare so that implementing a class can implement fare algorithm based on the context.
This interface defines the common strategy for the calculation of fare so that implementing a class can implement fare algorithm based on the context.
- using TrainFair.Models;
- namespace TrainFair.FareCalculator
- {
- public interface IFareStrategy {
- float GetFare(IFareRule ruleValues, float basicFare);
- }
- }
StationRuleFareCalculator class
This class calculates the fare based on the number of stations traveled and some ruleset which has been defined in the section of the problem statement.
This class calculates the fare based on the number of stations traveled and some ruleset which has been defined in the section of the problem statement.
- using System;
- using TrainFair.Models;
- namespace TrainFair.FareCalculator
- {
- public class StationRuleFareCalculator : IFareStrategy
- {
- public float GetFare(IFareRule ruleValues, float basicFare) {
- var stationFareRuleModel = ruleValues as StationFareRuleModel;
- if (stationFareRuleModel == null || stationFareRuleModel.StationsCounts <= 0)
- return 0;
- int chargingStations = (int)Math.Ceiling(stationFareRuleModel.StationsCounts / 5.0);
- if (chargingStations <= 1)
- return basicFare;
- int restChargingStations = chargingStations - 1;
- var totalFare = basicFare + restChargingStations * stationFareRuleModel.IncrementalPrice;
- return totalFare;
- }
- }
- }
AgeRuleFareCalculator class
This class implements the Fare algorithm, especially for the senior citizen. If the traveler is a senior citizen, then a particular dicount will be given to him/her. So, this class implements that algorithm.
- using TrainFair.Models;
- namespace TrainFair.FareCalculator
- {
- public class AgeRuleFareCalculator : IFareStrategy
- {
- public float GetFare(IFareRule ruleValues, float basicFare) {
- var ageFareRuleModel = ruleValues as AgeFareRuleModel;
- if (ageFareRuleModel == null)
- return 0;
- if (ageFareRuleModel.Age < 60) return basicFare;
- var totalFare = basicFare - (basicFare * ageFareRuleModel.Discount);
- return totalFare;
- }
- }
- }
Saravanakumar SekaranPosted Jan 17, 2018, 6:35 AM
Wow, reallly superb article very well explained
Kasam ShaikhPosted Jan 16, 2018, 10:21 AM
Great...very nicely explained! Awaiting similar articles with different design patterns..
Upendra Pratap ShahiPosted Jan 16, 2018, 9:41 AM
Nice one article.................
Rathrola Prem KumarPosted Jan 15, 2018, 9:33 PM
Good Concept Selection (y) ...Thanks for sharing also :)