Chapter 9: Creational Patterns

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

19 Terms

1
New cards

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.

2
New cards

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.

3
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>What is the problem with the following code?</strong></span></p>

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");


4
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>What does the Singleton Pattern recommend that we do about a problem like this?</strong></span></p>

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.

5
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>Describe what is happening in each of the following lines of code</strong></span></p>

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

knowt flashcard image
6
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>What is the problem with the following example?</strong></span></p>

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.

7
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>Apply the Singleton Design Pattern to the following example</strong></span></p>

Apply the Singleton Design Pattern to the following example

knowt flashcard image
8
New cards

When should we use Singleton?

Singleton should be used when only one object of a specific type should exist within a system.

9
New cards

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

10
New cards

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

11
New cards

What is the Factory Pattern for?

The Factory Pattern is for moving object creation into a separate interface known as the Factory class.

12
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>What’s the problem with the following code?</strong></span></p>

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.

13
New cards

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.

14
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>Label the:</strong></span></p><ul><li><p><strong>Product Base Class</strong></p></li><li><p><strong>Product Classes</strong></p></li><li><p><strong>Factory Base Class</strong></p></li></ul><p></p>

Label the:

  • Product Base Class

  • Product Classes

  • Factory Base Class


knowt flashcard image
15
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>Describe what each line in the following code is doing</strong></span></p>

Describe what each line in the following code is doing

16
New cards
<p><span style="color: rgb(255, 6, 159);"><strong>Why is this version better than what we had previously?</strong></span></p>

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.

17
New cards

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

18
New cards

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

19
New cards

What are the disadvantages of the Factory Design Pattern?