Name: ID: Section:
Imagine that you’re creating a logistics management application. The first version of
your app can only handle transportation by trucks, so the bulk of your code lives
inside the Truck class. Currently your code-base looks like the following:
#include <iostream>
#include <string>
using namespace std;
// Interface Or Abstract class
class Transport
public:
virtual ~Transport() {}; // Destructor
virtual string Delivery() const = 0; // Operation
};
class Truck : public Transport
public:
string Delivery() const override
return "Truck will be delivering your product!";
};
class RoadLogistic
public:
virtual ~RoadLogistic() {};
virtual Transport *Transportation() const = 0; // virtual function
Name: ID: Section:
string Order() const
Transport *transport = this->Transportation();
string result = "Your goods and products are shipped now ----- \n" +
transport->Delivery();
return result;
};
class TruckLogistic : public RoadLogistic
public:
Transport *Transportation() const override
return new Truck();
};
void ClientCode(const RoadLogistic &logistic)
cout << "Don't worry about your products and the transportation\n"
<< logistic.Order() << endl;
int main()
RoadLogistic *logistic1 = new TruckLogistic();
Name: ID: Section:
ClientCode(*logistic1);
Current Output:
Don't worry about your products and the transportation
Your goods and products are shipped now -----
Truck will be delivering your product!
After a while, your app becomes pretty popular. Each day you receive dozens of requests
from sea and plane transportation companies to incorporate sea and air logistics into the app.
Great news, right? But how about the code? At present, most of your code is coupled to the
Truck class. Adding Ships and Plane into the app would require making changes to the entire
code-base. Moreover, if later you decide to add another type of transportation to the app, you
will probably need to make all of these changes again. Which design pattern can we use to
solve this problem and our progarm output will be looking like the following:
Expected Output:
Don't worry about your products and the transportation
Your goods and products are shipped now -----
Truck will be delivering your product!
----------------------------------
Don't worry about your products and the transportation
Your goods and products are shipped now -----
Ship will be delivering your product!
------------------------------------
Don't worry about your products and the transportation
Your goods and products are shipped now -----
Plan will be delivering your product!
--------------------------------------