Command Pattern comes under behavioral pattern. It encapsulates all the information in an object for a particular action, that action to be performed at a later time. Cab Booking System can be developed using Command Design Pattern and using some scheduling algorithm along with.
Problem Statement
Develop an application for scheduling algorithms for such a cab service, provided, you have a list of future bookings, and a list of cabs in your fleet.
Let's say, XYZ is a user and wants to travel by cars to the different locations on different scheduled dates and time. Below are the different scenarios:
Cabs available : Cab A, Cab B, Cab C
The user sees the available cabs - Cab A, Cab B, Cab C and schedules booking as below.
- User XYZ books Cab A and schedules for 18/01/2018 15:30 PM
- User XYZ books CabB and schedules for 20/01/2018 12:30 PM
- User XYZ books CabC and schedules for 20/01/2018 16:30 PM
Note
The user can cancel the scheduled cab before the arrival of the cab at the scheduled time.
The user can cancel the scheduled cab before the arrival of the cab at the scheduled time.
This problem statement can be solved by using "Command Design Pattern" which can use some scheduling algorithm because cab booking and cab cancellation and scheduling information will be encapsulated and these activities will be performed based on the user demand for a later time.
So, cab cancellation and cab scheduling algorithms will be written separately and encapsulated into one object for the execution based on the Command Design Pattern.
Dependency Diagram

And another Dependency Diagram,

Class Diagram,

ICabService Interface
- namespace CabScheduling.CabBooking
- {
- public interface ICabService
- {
- void Execute();
- }
- }
BookCab Class,
- namespace CabScheduling.CabBooking
- {
- public class BookCab : ICabService {
- public readonly BookingRequest _bookingRequest;
- public BookCab(BookingRequest bookingRequest) {
- _bookingRequest = bookingRequest;
- }
- public void Execute() {
- _bookingRequest.Book();
- }
- }
- }
CancelCab Class,
- namespace CabScheduling.CabBooking
- {
- public class CancelCab : ICabService
- {
- public BookingRequest _bookingRequest;
- public CancelCab(BookingRequest bookingRequest) {
- _bookingRequest = bookingRequest;
- }
- public void Execute()
- {
- _bookingRequest.Cancel();
- }
- }
- }
BookingRequest Class,
- using System;
- using CabScheduling.BookingModel;
- namespace CabScheduling.CabBooking
- {
- public class BookingRequest {
- public CabBookingModel CabBookingModel { get; set; }
- public void Book() {
- Console.WriteLine("\n{0} Cab Book for Time {1}", CabBookingModel.CabName, CabBookingModel.BookingTimeScheduled);
- Console.WriteLine("\n{0} Cab Book for Time {1} is Arriving", CabBookingModel.CabName, CabBookingModel.BookingTimeScheduled);
- }
- public void Cancel() {
- Console.WriteLine("\n{0} Cab cancelled for Time {1}", CabBookingModel.CabName, CabBookingModel.BookingTime);
- }
- }
- }
BookingService Class,
- using System.Collections.Generic;
- using System.Linq;
- using CabScheduling.CabScheduler;
- namespace CabScheduling.CabBooking
- {
- public class BookingService
- {
- public readonly List<BookCab> BookedCabList = new List<BookCab>();
- public readonly List<CancelCab> CanceledCabList = new List<CancelCab>();
- private readonly ICabScheduler cabScheduler = new CabScheduler.CabScheduler();
- public void ScheduleCab(BookCab cabService)
- {
- BookedCabList.Add(cabService);
- }
- public void CancelCab(CancelCab cabService)
- {
- CanceledCabList.Add(cabService);
- }
- public void ConfirmService(bool booking) {
- if (booking) {
- foreach (var bookingItem in BookedCabList)
- {
- bookingItem.Execute();
- cabScheduler.StartCabScheduler(bookingItem._bookingRequest.CabBookingModel);
- }
- } else {
- foreach (var cancelItem in CanceledCabList)
- {
- cancelItem.Execute();
- var cancelledCabId = cancelItem._bookingRequest.CabBookingModel.CabId;
- var removeCab = BookedCabList.FirstOrDefault(e => e._bookingRequest.CabBookingModel.CabId == cancelledCabId);
- if (removeCab == null || !BookedCabList.Any()) continue;
- cabScheduler.CancelCabScheduler(removeCab._bookingRequest.CabBookingModel);
- BookedCabList.Remove(removeCab);
- }
- }
- }
- }
- }


Abdul naafiPosted Feb 26, 2019, 10:23 AM
What IDE are you using to execute the code ?
Humayun Kabir MamunPosted Jan 22, 2018, 12:35 AM
Thanks Ershad for this nice and informative article...
Naresh SinghalPosted Jan 21, 2018, 11:20 PM
Very Good example with deep explanation @Gul Md Ershad