1/18
Looks like no tags are added yet.
What is the Singleton Pattern?
It is a pattern which makes sure that only one instance of a class can ever exist, and it provides a global way to access that instance.
In other words, it creates exactly 1 object and allows the entire program to use that object.
What problem does the Singleton Pattern solve?
The Singleton Pattern solves the problem of needing one shared object that should be used across the entire program. Sometimes it doesn’t make sense to create multiple versions of the same class.

What is the problem with the following code?
The problem is that we would need to create multiple different ScreenManager objects just to display a different screen. This doesn’t make sense, and it especially doesn’t make sense for objects meant to represent a manager.
How can there be multiple managers? Which is the real manager?
For example:
ScreenManager manager1;
ScreenManager manager2;
ScreenManager manager3;
manager1.showScreen("Store Page");
manager2.showScreen("Menu");
manager3.showScrren("Game Over");
What does the Singleton Pattern recommend that we do about a problem like this?
It recommends we only create one ScreenManager object that is accessible by the entire class.

Describe what is happening in each of the following lines of code


What is the problem with the following example?
The problem is that we are forced to create multiple AudioManager objects in order to manage audio, which doesn’t necessarily make much sense in this scenario, because an AudioManger represents a Managerial object, and generally, there should only be a single instance of a Managerial object.

Apply the Singleton Design Pattern to the following example

When should we use Singleton?
Singleton should be used when only one object of a specific type should exist within a system.
What are the Advantages of using SIngleton?
It guarantees that only 1 instance exists
It allows the Singleton instance to be accessed from anywhere in the program
It reduces duplicates of manager-like objects
It centralizes data that should be shared across a system
What are the Disadvantages of Singleton?
Classes can become too dependent on Singleton objects as a result of them being globally accessible, which can lead to high coupling
It can be more difficult to test Singleton Objects because they’re shared globally
Singleton objects can violate the Single Responsibility Principle if it ends up managing too many responsibilities
It can become difficult to maintain because it behaves like a global variable
What is the Factory Pattern for?
The Factory Pattern is for moving object creation into a separate interface known as the Factory class.

What’s the problem with the following code?
The problem is that main is highly coupled to the other concrete classes.
In addition to this, it violates the Open/Closed principle, because we have to keep adding conditional blocks in order to add more and more types of users.
What problem does the Factory Design Pattern solve?
It solves the problem of tight coupling between client code and concrete classes.
More specifically, it prevents main() from knowing too much about the exact classes/implementation details of a class it needs to create objects for.

Label the:
Product Base Class
Product Classes
Factory Base Class


Describe what each line in the following code is doing

Why is this version better than what we had previously?
It’s better because it reduces coupling between the client class (main) and the other concrete classes, by making main only depend on UserFactory and User, unlike the way it depended on multiple concrete classes before.
Additionally, it hides object creation from main. Main doesn’t know how a regular user, or guest user, or member user is created behind the scenes. All of that is abstracted within Factory.
When should we use the Factory Design Pattern?
When we want to reduce coupling between the client class and concrete classes
When we don’t want main to know about the object creation details
When the exact type of object may change, and we want to easily substitute the creation of one object for another
What are the advantages of the Factory Design Pattern?
It supports the Open/Closed Principle by preventing you from having to add a bunch of conditional blocks in main each type a new type of object is created
It follows the Single Responsibility Principle, because object creation is separated from object usage
It reduces coupling between the client and concrete classes
It makes the process of creating objects more flexible
What are the disadvantages of the Factory Design Pattern?