Design Patterns Flashcards

1
Q

Describe the Factory design pattern

A

The Factory pattern is a creational design pattern useful when you have multiple objects from the same abstraction and you only know at runtime which one needs to be created. Object creation and/or method selection is delegated to a factory method or class. Typically an enumeration is used to determine the routing at runtime. An example would be ‘payment type’ selection in the UI with various provider types implementing an IProvider interface.

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

Describe the Proxy design pattern

A

The Proxy pattern is used when you need to provide an object that controls access to another object. One of the biggest reasons why you should do this is related to the cost of creating the object that is being controlled. The Proxy pattern has been largely replaced with Lazy Loading.

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

Describe the Singleton design pattern

A

The Singleton design pattern is a creational design pattern that restricts the instantiation of a class to a single instance. This could be useful for example with a configuration class shared throughout the application. To implement the pattern add a private constructor and static reference with a GetInstance() method.

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

Describe the Builder design pattern

A

The Builder design pattern is intended to delegate the construction of a complex object with different behavior due to its configuration. We decouple the configuration from its usage. Define a simple class that implements an interface then a separate builder class that contains a Build() method to construct the object.

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

Describe the Command design pattern

A

The Command pattern encapsulates a request as an object - including all of the information needed to perform an action. An example would be a Like, Unlike with Undo functionality. Define an ICommand interface with Execute() and Undo() methods. Inject the methods into a Package class and provide methods to encapsulate the calls.

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

Describe the Strategy Design Pattern

A

The Strategy pattern is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable. The pattern consists of a Strategy interface with typically an Execute method and a context class that maintains a reference to the strategies.

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

What is the difference between the Command and Strategy design patterns?

A

Although the architecture of the Command pattern and Strategy pattern is similar, they serve different purposes. Command pattern does some action on the information stored in the command object, while the Strategy pattern decides how to process the given object. The Strategy pattern has an orchestrator that determines how something should be done. A strategy for sorting is a good example.

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