Design Patterns Week 5 assignments
Week 5 assignments
When creating the program code, you must apply the following basic principles:
• create a separate project for each assignment;
• use name ‘assignment1’, ‘assignment2’, etcetera for the projects;
• create one solution for each week containing the projects for that week;
• make sure the output of your programs are the same as the given screenshots;
CodeGrade auto checks
Make sure all CodeGrade auto checks pass (10/10) for your assignments. The manual check will be done by the practical
teacher.
Design Patterns Week 5 assignments
Assignment 1 (‘Singleton Pattern’)
In an application logging is done at several places (in several classes) using a special Logger class. To save resources this
Logger is implemented as a ‘Singleton’. Each line in the log is preceded by a line number, starting at 1 and continously
increased by 1 when the next line is written (see screenshot below).
Implement the Logger class, a MainSystem class, a SubSystem class, and a simple Start method that (together) generates
the output shown above. The Start method calls the logger with: logger.Log("Program", "starting"); and
creates the MainSystem object. Do not pass the logger object around!
In the classdiagram you can see how the classes relate to each other.
Design Patterns Week 5 assignments
Assignment 2 (‘State Pattern’)
Create an application with an ATM machine (cash dispenser) that has 4 different states: 1) no card present, 2) card
present, 3) entered correct pincode and 4) no cash available. Use the ‘state pattern’ to implement the ATM machine and
its states. Use the classdiagram below to implement the interface and classes.
In the Start method, create an ATM machine object containing 2000 euro, and make a loop for reading a command
(valid commands are: "insert card", "reject card", "enter pincode", "withdraw cash"). When the user enters "stop", the
program ends.
void Start() {
ATMMachine machine = new ATMMachine(2000);
// loop...
}
You can see an example of the output on the next page.
Design Patterns Week 5 assignments