7.4 Singleton Pattern Lab

Overview

7.4 Singleton Pattern Lab

This assignment is based off of the Head First Designs book p. 170-186

Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.

Download and extract the starting code.
1.Download and extract the starting code.

7.4 Singleton LastName.zip

7.4 Singleton LastName.zip
A Download and extract the starting code.
Test the application.
Acceptance Criteria
Acceptance criteria
  • The original test is run before either singleton variation is introduced.
  • The observed instance behavior is retained for comparison with the lazy and eager implementations.
Instructions
2.Test the application.

This application is only 3 classes: Program, EagerSingleton, and Lazy Singleton. In the start code, both Eager and Lazy Singleton have the same code.

A Run the application. There will be 4 object instances all with different random numbers.
Implement the Lazy Singleton pattern.
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Define a static private field of type LazySingleton named instance.
  • The completed implementation satisfies this requirement: Change the constructor from public to private.
  • The completed implementation satisfies this requirement: Define a public static method named GetInstance with a LazySingleton return type.
Instructions
3.Implement the Lazy Singleton pattern.
A In the LazySingleton class...
1 Define a static private field of type LazySingleton named instance.
2 Change the constructor from public to private.
3 Define a public static method named GetInstance with a LazySingleton return type.
4 In the GetInstance method...

Check if the instance field object is null.

If it is, assign the instance field to a new instance of the LazySingleton class, passing in "Lazy Singleton Instance 1: " and a new Random(1).Next(2000)

Return the instance.

B In the Main method...
1 Note that now you cannot instantiate the LazySingleton class anymore.
2 Instead of calling "new", call the GetInstance method in both places.
LazySingleton ls = LazySingleton.GetInstance();
C Check Your Work:
1 Start the application. The first set of numbers will be different, but in the second set of numbers, the Lazy Singleton will…

Start the application. The first set of numbers will be different, but in the second set of numbers, the Lazy Singleton will be the same instance/number of the first set.

Implement the Eager Singleton pattern.
Acceptance Criteria
Acceptance criteria
  • Run the application. Both sets should be the same instance and number.
  • The completed implementation satisfies this requirement: Define a private static field of type EagerSingleton named instance and instantiate it on the same line, passing in "Eager Singleton Instance 1: " and new Random(2).Next(2000)
  • The completed implementation satisfies this requirement: Make the EagerSingleton constructor private.
Instructions
4.Implement the Eager Singleton pattern.
A In the EagerSingleton class...
1 Define a private static field of type EagerSingleton named instance and instantiate it on the same line, passing in "Eager…

Define a private static field of type EagerSingleton named instance and instantiate it on the same line, passing in "Eager Singleton Instance 1: " and new Random(2).Next(2000)

2 Make the EagerSingleton constructor private.
3 Define a public static method named GetInstance with a EagerSingleton return type that returns the instance.
B In the Main method...
1 Note that you can no longer instantiate EagerSingleton from here.
2 Replace the calls to "new" with calls to EagerSingleton's GetInstance method.
C Check Your Work:
1 Run the application. Both sets should be the same instance and number.
Zip up your assignment and submit it to Canvas.
5.Zip up your assignment and submit it to Canvas.
A Zip up your assignment and submit it to Canvas.
Completion review
6.Review the feature acceptance criteria
A Confirm the required results

Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.

Test the application. Implement the Lazy Singleton pattern. Implement the Eager Singleton pattern.