Project: The Singleton Design Pattern

Singleton Lab

NOTE: The tricky part about this lab is avoiding an infinite loop when creating the team members. 

Create an Abstract Base Class (ABC) called TeamMember. Add a private data member name of type String. Also add getName() and setName() methods. Create a single constructor that takes a name parameter. Also create an abstract method called work in this ABC.

Then create separate classes for PoliceOfficer, FireFighter, and Doctor. Each of these classes should be created using the Singleton design pattern. Override the work method for each class. Inside their respective work methods, have each team member say that they are working.

Inside the PoliceOfficer class, put a FireFighter and a Doctor object (Class Composition). Likewise, in the FIreFighter class, put a PoliceOfficer and a Doctor object. Finally, the Doctor class should contain a PoliceOffier and a FireFighter. Create a giveHelp() method in each class. Inside this method, print that the object is giving help.

Now in the test code, create the three objects in this order: 

  1. PoliceOfficer
  2. FireFighter
  3. Doctor
Then command each object to do work. Then have the PoliceOfficer object ask its FireFighter and Doctor for help. Then have the FireFighter ask its PoliceOfficer and Doctor for help. Then have the Doctor ask its PoliceOfficer and FireFighter for help.

After demonstrating this sequence to your instructor, comment out this sequence and uncomment the following sequence:

  1. Doctor
  2. FireFighter
  3. PoliceOfficer
Then run the same sequence of work and help commands to prove that the sequence of object creation did not matter and that your program worked regardless.

1 point
I have completed the project(s) for this lesson and have demonstrated them to my instructor.