mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1013 B
47 lines
1013 B
4 months ago
|
## Singleton Blueprint
|
||
|
|
||
|
### Instructions
|
||
|
|
||
|
You are given an incomplete singleton class. Complete the class to demonstrate your understanding of how the Singleton design pattern works. The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.
|
||
|
|
||
|
### Expected Class
|
||
|
|
||
|
```java
|
||
|
public class Singleton {
|
||
|
public static Singleton instance;
|
||
|
|
||
|
private Singleton() {
|
||
|
// Initialization code
|
||
|
}
|
||
|
|
||
|
public static Singleton get???() {
|
||
|
// Implementation to return the single instance
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
|
Here is a possible `ExerciseRunner.java` to test your class:
|
||
|
|
||
|
```java
|
||
|
public class ExerciseRunner {
|
||
|
public static void main(String[] args) {
|
||
|
// Get the only object available
|
||
|
Singleton singleton = Singleton.getInstance();
|
||
|
|
||
|
// Show the message
|
||
|
singleton.showMessage();
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Expected Output
|
||
|
|
||
|
```shell
|
||
|
$ javac *.java -d build
|
||
|
$ java -cp build ExerciseRunner
|
||
|
Hello, I am a singleton!
|
||
|
$
|
||
|
```
|