OOP Design Patterns Flashcards

1
Q

What is Strategy Design Pattern?

A

It’s a behavioral design pattern that utilizes composition rather than inheritance.

Encapsulates algorithms’ behavior and makes the algorithms interchangeable

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

What’s the difference between inheritance versus composition?

A

Inheritance is a “is-a” relationship while composition is a “has-a” relationship.

Both provides code reusability.

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

What are the different categories of design patterns?

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

What is the Observer Design Pattern?

A

Push vs. Poll

It’s a behavioral design pattern that defines a one-to-many relationship where the observable (publisher) pushes its state status to the observers (subscribers).

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

How do you practice design patterns?

A

Draw UML diagrams

UML stands for Unified Modeling Language

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

What is the decorator pattern?

A

It’s a structural pattern that uses composition rather than inheritance in order to share behavior.

It’s used to add responsibilities to an existing object dynamically.

It has both an “is-a” and a “has-a” relationship.

Decorators provide a flexible alternative to subclassing for extended functionality.

Visual Representation: Wrapper

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

What is inheritance not good for?

A
  • It’s not good for code reuse.
  • It’s not good for sharing behavior.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the interface segregation principle (ISP)?

A

States that no code should be forced to depend on methods it does not use.

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

What is the factory method pattern?

A

The factory is responsible for holding the business logic of creation of objects.

It lets a class defer instantiation to subclasses.

Create objects in different ways and/or different subtypes

It uses composition rather inheritance.

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

What is an abstract factory pattern?

A

It is a set of factory methods. It makes use of factory methods.

It provides an interface for creating families of related or dependent objects without specifying their concrete classes.

The difference between the factory method and abstract factory pattern is that the factory method constructs a single object while the abstract factory constructs multiple objects.

Ex. Dialog and buttons for MacOS vs Windows where dialog and buttons are the objects

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

What is the singleton pattern?

A

A class that only has one instance and provides global access to that one instance.

Designed to make it impossible to create a second instance. Constructor is private.

Many consider this pattern a code smell. “One man’s constant is another man’s variable”.

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

What is a command pattern?

A

Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undo operations.

The point is that we encapsulate something trivial so we can pass it around.

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

What’s the difference between command pattern and strategy pattern?

A

Intentions behind it

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

What is the adapter pattern?

A

It’s about making 2 interfaces, that aren’t compatible, compatible. Interface, in this context, means the contract between two things.

It’s also known as a wrapper. It lets classes work together that couldn’t otherwise.

The intention is not to change the underlying behavior.

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

What are the 4 patterns that easily get confused due to similarity?

A
  • Adaptor
  • Proxy
  • Decorator
  • Facade
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the facade pattern?

A

Creating an interface that allows the client to not deal with the complexity behind the facade

Provides an unified interface, which is used by a client, to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.

17
Q

Define

Law of Demeter

A
  • It’s also called the “Principle of Least Knowledge”.
  • It’s the principle of knowing as little as possible
  • “Talk only to your immediate friends”
  • It’s intended to reduce coupling.
18
Q

What is the proxy pattern?

A

The proxy pattern provides a surrogate or placeholder for another object to control access to it.

It’s a another way of introducting another level of indirection.

You’re trying to solve a specific set of problems that are access-related.

The proxy looks like the thing it’s proxying.

Ex. stunt doubles

19
Q

How do you differentiate similar design patterns?

A

It’s not only how you string multiple objects together, but it’s also about the intent behind it.

Some pattern are mostly different in the intent rather than the technicalities.

20
Q

What the different styles of proxy pattern?

A
  • Remote
  • Virtual
  • Protection
21
Q

What is a remote proxy?

A

Used when you want access to a resource that’s “remote”.

For example, it can be a resource that exists in a different server or a different namespace.

Your proxy is responsible for interacting with the remote resource.

Ex. Promises in Node.js

22
Q

What is a virtual proxy?

A

Controls access to a resource that is expensive to create

It’s kind of like caching or lazy evaluation.

23
Q

What is a protection proxy?

A

Access management

24
Q

What is bridge pattern?

A

The intent of the bridge pattern is to decouple an abstraction from its implementation so that the two can vary independently.

25
Q

What are some structural patterns and their intentions?

A
  • Decorator (adding behavior)
  • Facade (simplify)
  • Adaptor (adapting interfaces)
  • Proxy (controlling access)
  • Bridge (decoupling)
26
Q

What’s the difference between a strategy pattern and a bridge pattern?

A

The bridge pattern took the strategy pattern one step further.

Bridge pattern is a generalization of the strategy pattern.

One is behavioral and the other is structural.