Lab7-Adapter Pattern
Lab7: Adapter Pattern
UML Diagram for Adapter Pattern.
Client: This is the class which wants to achieve some functionality by using the
adaptee’s code.
ITarget: This is the interface which is used by the client to achieve functionality.
Adapter: This is the class which would implement ITarget and would call the
Adaptee code which the client wants to call.
Adaptee: This is the functionality which the client desires but its interface is not
compatible with the client.
Lab7-Adapter Pattern
Lab Exercise
Let us implement the adapter pattern for an online store who wants to calculate
the shipping costs for the packages for customer orders. The shipping costs are
however determined by the shipper- Canada Post in this case. The client (online
store) cannot directly call the PostalCost() method of the adaptee (Canada Post)
so we will implement the Adapter Pattern.
Submission Process:
* PEASE DO NOT SUBMIT THE C# PROJECT AND INSTEAD
Please copy the code for the following three classes in MS Word (or pdf) and
upload that single file through the Lab7-submissionLink in Blackboard;
- Adapter.cs
- ITarget.cs
- Vendor.cs
Adaptee (Canada Post)
I have created a very simple method to calculate the shipping costs based on
package weight and have provided the code at the end of this document (you can
copy this code in the adaptee class).
Lab7-Adapter Pattern
Client (Online Store)
When the user will enter the weight of the package on online stores website
(client), it will use the adapter pattern to call the PostalCost() method from
Shipper’s class(Canada Post) through The ITarget interface and its implementing
class; the Adapter
Screen shot for the Project
Provided below is a screenshot for the adaptee classes (CanadaPost) and you will
create the other two classes and one Interface (based on the following example).
Lab7-Adapter Pattern
CODE
class CanadaPost
{public double PostalCost(double packageWeight)
{
double shipcost;
if (packageWeight <= 5)
{ shipcost = 3.50; }
else if (packageWeight <= 10)
{ shipcost = 5.00; }
else if (packageWeight <= 15)
{ shipcost = 7.50; }
else if (packageWeight <= 20)
{ shipcost = 10.00; }
else
{ shipcost = 15.00; }
Lab7-Adapter Pattern
return shipcost;
}
}
References:
1-https://www.codeproject.com/Articles/774259/Adapter-Design-Pattern-Csharp
2- https://books.google.ca/books?id=pD2XMZLGUAYC&pg=PA
3- https://en.wikipedia.org/wiki/Adapter_pattern