Design Patterns Flashcards

1
Q

What is the Singleton Design Pattern?

A

When you limit your application to only a single instance of a class and making it globally accessible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the benefits of the Singleton Design Pattern?

A

Allows for you to have a single point of truth for a given class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some ways you can use a singleton?

A

Application state, technology drivers, application configuration objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Some consider the singleton design pattern an anti-pattern, why?

A
  1. It promotes global state which is a code smell because it is hard to control
  2. It assumes that you know with certainty that you will never need more than 1 instance of the class which is almost impossible to know (the alternative is to just have a class that you only create one object for without forcing it to only allow a single instance).
  3. Some might say that it violates SRP because it’s responsible for enforcing a single instance as well as be responsible for the original purpose of the class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you make a singleton?

A

Make the constructor private and provide a public static method that is responsible for providing only a single instance of the class. If it already has an instance then return the already created instance instead of a new instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Facade Pattern

A

Provides a high level interface that makes the subsystem easier to use such that a client interacts with the facade rather than the complexity of the subsystem itself.

The facade may know how to handle a potentially complex setup or interaction with the code in question so that the client doesn’t have to know how to itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Decorator Pattern

A

Using composition instead of inheritance to share behavior by attaching additional responsibilities to an object dynamically (at runtime).

“is a” and “has a” of a the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s an advantage of the decorator pattern over inheritance?

A
  1. It adds a level of flexibility where you can compose different combinations of classes at runtime that you otherwise could not do with inheritance.
  2. It is very good for scenarios where the decorators are significantly different.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a disadvantage of the decorator pattern?

A
  1. Multiple levels of composition in which a class is wrapped in another class, etc… can make it very difficult to actually understand what is going on.
  2. This design pattern does not account for or support a scenario where the order in which something is wrapped is important.
  3. It’s easy to over-engineer with this when your decorators are in fact very similar and may be better suited with just being a subtype.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In a decorator what does “this” refer to?

A

It refers to the member variable that is the composition of the decorator type (i.e. this.beverage()).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are some scenarios where it makes sense to use a decorator pattern?

A

Input Stream, ie. FileInputStream, BufferedInputStream, etc…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an adapter pattern

A

Let’s classes work together that couldn’t otherwise because of incomparable interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are some examples of when it makes sense to use an adapter pattern?

A
  1. When there is a signature that is propogated throughout the application. Rather than change each place it’s propogated for your scenario, instead use the adapter pattern to make your class compatable with the current signature.
  2. When it is possible that a signature could be changed in the future, you encapsulate it so that the adapter get’s changed instead of all the code that you’ve already written.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly