The goal of this lab is to use the strategy design pattern to solve a real-world problem.
Design patterns are proven solutions to solve recurring design problems to design flexible and reusable object-oriented software. That is it, objects that are easier to implement, change, test and reuse. Behavioral patterns deal with the communication between objects and structuring the responsibilities between objects to keep them extensible, flexible and efficient.
The strategy design pattern (a.k.a. policy pattern) is one of the twenty-three well-known Gang of Four (GoF) design patterns. It is classified under the category of behavioral patterns, and it allows us to select an algorithm at runtime. Instead of implementing a single algorithm and using it, the strategy pattern allows us to select one from family of algorithms. It defers the decision about which algorithm to use until runtime allowing the algorithm to vary independently from clients that use it.
In this lab, we will work on creating a system that needs to support both multiple payment methods (e.g., Mada debit card, Visa credit card, PayPal, Google Pay, Apple Pay, etc.) and multiple receipt and invoice generation methods (paper, pdf, email, etc.)
The system will allow clients to decide which payment method and invoice method to choose at runtime.
Payment Methods:
Invoice Methods:
Video
Objectives
In this lab you will
understand a real-world scenario and choose when to apply the appropriate design pattern.
design and implement the strategy design pattern.
learn how to send an email from your application in Java.
learn how to generate a PDF file from your application in Java.
If your instructor is using GitHub classroom, you will need to accept the assignment using the link below, clone the template repository, and import it as a project into your IDE.
If your instructor is not using GitHub classroom, clone and import the template project at https://github.com/cpit252/lab-10 ↗.
Create an account on Resend
You also need to create an email account to send the receipt as an email message. We’re going to use a developer friendly email service called Resend. Please sign up with your GitHub, generate an API key and save it.
Store your Resend’s API key in the environment variables
You should avoid storing your secrets (e.g., API keys, passwords, etc.) in the source code as hardcoded values. Instead, you should use environment variables. This lab makes use of two environment variables: email and key. Create and set the values of these two environment variables. If you do not know how to set environment variables, please refer to the lecture note on Environment Variables under Miscellaneous.
Problem Statement
A developer is working on an app that accepts a payment. The payment can be made in a number of payment methods (e.g., Mada debit card, Visa credit card, PayPal, Google Pay, Apple Pay, etc.). The app also supports generating payment receipts or invoices in multiple methods (paper, pdf, email, etc.).
The developer needs to add these two features (payment processing and receipt generation) in such a way that adding more payment and receipt delivery options won’t require major refactoring of the code base. She wanted to have the user select a payment or receipt option at runtime in a more extensible, flexible, and efficient fashion. She thought of using the Strategy design pattern to make her payment and receipt implementations as loosely coupled as possible.
Below is an example of an email receipt generated by this program:
Questions:
Complete the implementation of the strategy design pattern as shown in the code above?
Explain how the strategy design pattern simplified the process of adding a new payment and receipt delivery options?
If your instructor is using GitHub classroom, then you should click on your class submission link,
link your GitHub username to your name if you have not already done so, accept the assignment, clone the
repository into your local
development environment, and push the code to the remote repository on GitHub. Please make sure that your
written
answers are included in either a README (Markdown) file or a PDF file.
Lab dues dates are listed on GitHub classroom unless otherwise
noted.
If your instructor is using GitHub classroom, your submission will be
auto-graded
by running the included unit tests as well as manually graded for correctness, style, and quality.
How to submit your lab to GitHub Classroom
The video below demonstrates how to submit your work to GitHub classroom
Extra Task [Optional]
If you are done with this activity, you may enable a continuos integration tool such as CircleCI ↗ to automatically run your JUnit test upon code changes. You may also add more unit tests to increase code coverage. Please embed the badge that shows the status of your build and test (passing/failing) as well as the coverage percentage into your README file (e.g.,
and ). Please be sure to fork the repository or push to a remote repository under your own account, so you can enable the integration of CI tools in your own account.