Design Patterns Flashcards
Describe the Factory design pattern
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.
Describe the Proxy design pattern
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.
Describe the Singleton design pattern
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.
Describe the Builder design pattern
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.
Describe the Command design pattern
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.
Describe the Strategy Design Pattern
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.
What is the difference between the Command and Strategy design patterns?
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.