Design Patterns Flashcards
What is the Singleton Design Pattern?
When you limit your application to only a single instance of a class and making it globally accessible.
What are the benefits of the Singleton Design Pattern?
Allows for you to have a single point of truth for a given class.
What are some ways you can use a singleton?
Application state, technology drivers, application configuration objects
Some consider the singleton design pattern an anti-pattern, why?
- It promotes global state which is a code smell because it is hard to control
- 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).
- 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 do you make a singleton?
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.
What is the Facade Pattern
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.
What is the Decorator Pattern
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.
What’s an advantage of the decorator pattern over inheritance?
- It adds a level of flexibility where you can compose different combinations of classes at runtime that you otherwise could not do with inheritance.
- It is very good for scenarios where the decorators are significantly different.
What is a disadvantage of the decorator pattern?
- 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.
- This design pattern does not account for or support a scenario where the order in which something is wrapped is important.
- 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.
In a decorator what does “this” refer to?
It refers to the member variable that is the composition of the decorator type (i.e. this.beverage()).
What are some scenarios where it makes sense to use a decorator pattern?
Input Stream, ie. FileInputStream, BufferedInputStream, etc…
What is an adapter pattern
Let’s classes work together that couldn’t otherwise because of incomparable interfaces.
What are some examples of when it makes sense to use an adapter pattern?
- 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.
- 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.