Design Patterns Flashcards

1
Q

Template

A
  • Abstract template class
  • Abstract methods, not implemented
  • Extend, for concrete classes
  • @overide
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Factory

A

So you have an interface, Product, with some methods. Where the subclasses represent different products.

You have a creator class, which is abstract, with a method that returns an instance of the product interface. Have concrete creators that implements this abstract method

Intent: The Factory Method pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created, promoting loose coupling and enabling flexible and extensible object creation

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

Strategy Intent

A

Strategy 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.

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

Strategy Structure

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

Strategy: Pros and Cons

A

Pros
- You can swap algorithms used inside an object at runtime.
- You can isolate the implementation details of an algorithm from the code that uses it.
- You can replace inheritance with composition.
- Open/Closed Principle. You can introduce new strategies without having to change the context.

Cons:
- If you only have a couple of algorithms and they rarely change, there’s no real reason to overcomplicate the program with new classes and interfaces that come along with the pattern.
- Clients must be aware of the differences between strategies to be able to select a proper one.
- A lot of modern programming languages have functional type support that lets you implement different versions of an algorithm inside a set of anonymous functions. Then you could use these functions exactly as you’d have used the strategy objects, but without bloating your code with extra classes and interfaces.

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

Strategy: Relations

A

Bridge, State, Strategy (and to some degree Adapter) have very similar structures. Indeed, all of these patterns are based on composition, which is delegating work to other objects. However, they all solve different problems. A pattern isn’t just a recipe for structuring your code in a specific way. It can also communicate to other developers the problem the pattern solves.

Command and Strategy may look similar because you can use both to parameterize an object with some action. However, they have very different intents.

You can use Command to convert any operation into an object. The operation’s parameters become fields of that object. The conversion lets you defer execution of the operation, queue it, store the history of commands, send commands to remote services, etc.

On the other hand, Strategy usually describes different ways of doing the same thing, letting you swap these algorithms within a single context class.

Decorator lets you change the skin of an object, while Strategy lets you change the guts.

Template Method is based on inheritance: it lets you alter parts of an algorithm by extending those parts in subclasses. Strategy is based on composition: you can alter parts of the object’s behavior by supplying it with different strategies that correspond to that behavior. Template Method works at the class level, so it’s static. Strategy works on the object level, letting you switch behaviors at runtime.

State can be considered as an extension of Strategy. Both patterns are based on composition: they change the behavior of the context by delegating some work to helper objects. Strategy makes these objects completely independent and unaware of each other. However, State doesn’t restrict dependencies between concrete states, letting them alter the state of the context at will.

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

Singleton

A
  • private instance variable
  • private constructor
  • public static synchronised getter method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Iterator Intent

A

It is a design pattern that lets you traverse elements of a collection without exposing underlying representation (list, stack tree)

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

Observer

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

Facade

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

Adapter

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

Decorator

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

Composite

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

Command

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

Compound Command

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

State

A