July 11 2024
using System; public class TravelTimeCalculator { public void CalculateTravelTime(string travelMode, double distance) { switch (travelMode) { case "Car": Console.WriteLine($"Travel time by Car: {distance / 60} hours."); break; case "Bus": Console.WriteLine($"Travel time by Bus: {distance / 40} hours."); break; case "Bike": Console.WriteLine($"Travel time by Bike: {distance / 15} hours."); break; case "Walking": Console.WriteLine($"Travel time by Walking: {distance / 5} hours."); break; default: throw new ArgumentException("Invalid travel mode"); } } } public class Client { public static void Main(string[] args) { var calculator = new TravelTimeCalculator(); calculator.CalculateTravelTime("Car", 120); calculator.CalculateTravelTime("Bus", 120); calculator.CalculateTravelTime("Bike", 120); calculator.CalculateTravelTime("Walking", 120); } }
public interface ITravelStrategy { void CalculateTravelTime(double distance); }
public class CarTravelStrategy : ITravelStrategy { public void CalculateTravelTime(double distance) { Console.WriteLine($"Travel time by Car: {distance / 60} hours."); } } public class BusTravelStrategy : ITravelStrategy { public void CalculateTravelTime(double distance) { Console.WriteLine($"Travel time by Bus: {distance / 40} hours."); } } public class BikeTravelStrategy : ITravelStrategy { public void CalculateTravelTime(double distance) { Console.WriteLine($"Travel time by Bike: {distance / 15} hours."); } } public class WalkingTravelStrategy : ITravelStrategy { public void CalculateTravelTime(double distance) { Console.WriteLine($"Travel time by Walking: {distance / 5} hours."); } }
public class TravelContext { private ITravelStrategy _travelStrategy; public void SetTravelStrategy(ITravelStrategy travelStrategy) { _travelStrategy = travelStrategy; } public void CalculateTravelTime(double distance) { _travelStrategy?.CalculateTravelTime(distance); } }
public class Client { public static void Main(string[] args) { var travelContext = new TravelContext(); // Travel by Car travelContext.SetTravelStrategy(new CarTravelStrategy()); travelContext.CalculateTravelTime(120); // Travel by Bus travelContext.SetTravelStrategy(new BusTravelStrategy()); travelContext.CalculateTravelTime(120); // Travel by Bike travelContext.SetTravelStrategy(new BikeTravelStrategy()); travelContext.CalculateTravelTime(120); // Travel by Walking travelContext.SetTravelStrategy(new WalkingTravelStrategy()); travelContext.CalculateTravelTime(120); } }
public class TrainStrategy : ITravelStrategy { public void Travel(string destination) { Console.WriteLine("Traveling to " + destination + " by train."); } }

1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 18,000+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 18,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.