Design Patterns Flashcards

1
Q

What is the Strategy Pattern?

A

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.

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

What is the Object Oriented Concept of Abstraction?

A

Unnecessary implementation details should be hidden, so that someone using a piece of code only has to consider relevant information about a thing.

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

What is the Object Oriented Concept of Encapsulation?

A

Using methods to modify state, rather than just exposing state externally.

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

What is the Object Oriented Concept of Polymorphism?

A

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.

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

What is the Object Oriented Concept of Inheritance?

A

Deriving a class from a parent class. You can use this to extend or modify a parent class.

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

Why should you encapsulate what varies?

A

If you hide the implementation details that may change, you keep freedom for both the “using code” and the encapsulated class to evolve independently.

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

Given two class types, TypeA and TypeB, when should you favor composition over inheritance?

A

When TypeB wants only some/part of the behavior exposed by TypeA.

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

Why should you program to interfaces, and not implementations?

A

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.

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

Given two class types, TypeA and TypeB, when should you favor inheritance over composition?

A

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.

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

What is the push version of the Observer Pattern?

A

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.

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

What is the pull version of the Observer Pattern?

A

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.

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

In the observer pattern, can you assume that the Observers are notified in a specific order?

A

No

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

What is “coupling”?

A

The degree of knowledge that one object has about the other object it interacts with.

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

Why is loose coupling better?

A

It increases flexibility and ability for code to handle changes, by reducing the dependency between multiple objects.

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

What is the open-closed principle?

A

Classes should be open for extension, but closed to modification

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

What is the decorator pattern?

A

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.

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

What is the factory method pattern?

A

The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

https://en.wikipedia.org/wiki/Factory_method_pattern

18
Q

What is the dependency inversion principle?

A

Depend upon abstractions. Do not depend on concrete classes.

19
Q

What is the abstract factory pattern?

A

An AbstractFactory defines the interface that all concrete factories must implement. This is useful for having multiple concrete factories for different “kinds” of related objects without specifying their concrete classes.

20
Q

What is a Singleton?

A

A class that can only have one instantiated object of its type.

21
Q

In Java, what could cause a Singleton to be instantiated multiple times?

A

When the classes that use the singleton have different class loaders. https://stackoverflow.com/questions/39533902/java-singleton-usage-with-multiple-class-loaders

22
Q

What is the Command pattern?

A

Encapsulating a request as an object, letting you parameterize clients with different requests, queue, or log requests, or supporting undoable operations.

23
Q

What method does a Command object have?

A

execute(). it holds the code that actually does things.

24
Q

What is the Adapter pattern for?

A

Changing one interface into another interface you want.

25
Q

How is a class adapter different from an object adapter?

A

A class adapter extends the class to be adapted, and implements the target interface. An object adapter implements the target interface, but contains an instance of the class to be adapted.

26
Q

What is the Facade pattern for?

A

Providing a simpler interface for using a more complex object.

27
Q

What is the Template Method Pattern?

A

The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

28
Q

When you’re creating a template method, how do you know when to use abstract methods vs hooks (default implementations that can be overridden) ?

A

Use abstract methods when your subclass MUST provide an implementation of the method or step in the algorithm. Use hooks when that part of the algorithm is optional. With hooks, a subclass may choose to implement that hook, but it doesn’t have to.

29
Q

What is the Hollywood Principle?

A

High level components should call low level components, but not the other way around (Don’t call us, we’ll call you)

30
Q

Setting custom compare behavior for an object, and then a Java Array calling that compare method to sort an array, is an example of using what design pattern?

A

Template Method Pattern

31
Q

The Factory Method is a specialization of what method?

A

The Template Method

32
Q

What two methods does the Iterator pattern have?

A

hasNext(), which returns true if there is another item and next() which returns the next item

33
Q

What is the Iterator pattern?

A

The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation

34
Q

What is the single responsibility principle?

A

A class should only have one reason to change (more responsibilities means more reasons to change)

35
Q

What is the Composite Pattern?

A

The composite pattern allows you to compose objects into tree structures to represent part/whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

36
Q

What is the State pattern?

A

Have a State class with all of the actions you want to be able to take in any given state, then subclass it for the different possible states. This allows an object to alter its behavior when the internal state changes, by changing its class.

37
Q

What is the Proxy pattern?

A

The Proxy Pattern creates a representative object that controls access to another object, which may be remote, expensive to create, or need securing.

38
Q

What patterns is MVC made of?

A

Observer, Strategy, and Composite

39
Q

How does MVC use the observer pattern?

A

The model uses it to keep its observers updated but stay decoupled from them

40
Q

How does MVC use the strategy pattern?

A

The controller is the strategy for the view.

41
Q

How does MVC use the composite pattern?

A

The view uses the composite pattern to implement the user interface, which usually consists of nested components like frames, panels, and buttons