Design Patterns Flashcards
What is the Strategy Pattern?
Encapsulating algorithms into objects so that they can be swapped out. E.g. interchangable “fly” behavior objects on different types of duck (rubber, wooden, real)
One possible use of this pattern would be loading data from different sources.
What is the Object Oriented Concept of Abstraction?
Unnecessary implementation details should be hidden, so that someone using a piece of code only has to consider relevant information about a thing.
What is the Object Oriented Concept of Encapsulation?
Using methods to modify state, rather than just exposing state externally.
What is the Object Oriented Concept of Polymorphism?
The concept that different classes can be used with the same interface. That different classes can provide their own unique implementations of the same interface.
What is the Object Oriented Concept of Inheritance?
Deriving a class from a parent class. You can use this to extend or modify a parent class.
Why should you encapsulate what varies?
If you hide the implementation details that may change, you keep freedom for both the “using code” and the encapsulated class to evolve independently.
Given two class types, TypeA and TypeB, when should you favor composition over inheritance?
When TypeB wants only some/part of the behavior exposed by TypeA.
Why should you program to interfaces, and not implementations?
So that client code is not concerned with how the implementation works. This means the interface completely states what a type can do, and makes the code more maintainable.
Given two class types, TypeA and TypeB, when should you favor inheritance over composition?
When TypeB wants to expose the complete interface (all public methods, no less) of TypeA such that TypeB can be used where TypeA is expected.
What is the push version of the Observer Pattern?
You have two kinds of objects, Subjects and Observers. Subjects have a list of observers that can be updated via register/remove, and can send updates to the whole list. Observers have an update method that does something with the data passed in by the Subject.
What is the pull version of the Observer Pattern?
You have two kinds of objects, Subjects and Observers. Subjects have a list of observers that can be updated via register/remove, and can send updates to the whole list. Observers then pull data from the subject when their update method is called.
In the observer pattern, can you assume that the Observers are notified in a specific order?
No
What is “coupling”?
The degree of knowledge that one object has about the other object it interacts with.
Why is loose coupling better?
It increases flexibility and ability for code to handle changes, by reducing the dependency between multiple objects.
What is the open-closed principle?
Classes should be open for extension, but closed to modification
What is the decorator pattern?
Where decorator objects have the same supertype as the object they decorate (wrap around). You can use one or more decorators to wrap an object. Since they have the same supertype, they can be passed around in place of the original object. The decorator adds its own behavior either before/after delegating to the object it decorates, to do the rest of the job. This provides a flexible alternative to subclassing for extending functionality.