OOP Design Patterns Flashcards
What is Strategy Design Pattern?
It’s a behavioral design pattern that utilizes composition rather than inheritance.
Encapsulates algorithms’ behavior and makes the algorithms interchangeable
What’s the difference between inheritance versus composition?
Inheritance is a “is-a” relationship while composition is a “has-a” relationship.
Both provides code reusability.
What are the different categories of design patterns?
- Structural
- Behavioral
- Creational
What is the Observer Design Pattern?
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 do you practice design patterns?
Draw UML diagrams
UML stands for Unified Modeling Language
What is the decorator pattern?
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
What is inheritance not good for?
- It’s not good for code reuse.
- It’s not good for sharing behavior.
What is the interface segregation principle (ISP)?
States that no code should be forced to depend on methods it does not use.
What is the factory method pattern?
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.
What is an abstract factory pattern?
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
What is the singleton pattern?
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”.
What is a command pattern?
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.
What’s the difference between command pattern and strategy pattern?
Intentions behind it
What is the adapter pattern?
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.
What are the 4 patterns that easily get confused due to similarity?
- Adaptor
- Proxy
- Decorator
- Facade
What is the facade pattern?
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.
Define
Law of Demeter
- 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.
What is the proxy pattern?
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
How do you differentiate similar design patterns?
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.
What the different styles of proxy pattern?
- Remote
- Virtual
- Protection
What is a remote proxy?
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
What is a virtual proxy?
Controls access to a resource that is expensive to create
It’s kind of like caching or lazy evaluation.
What is a protection proxy?
Access management
What is bridge pattern?
The intent of the bridge pattern is to decouple an abstraction from its implementation so that the two can vary independently.
What are some structural patterns and their intentions?
- Decorator (adding behavior)
- Facade (simplify)
- Adaptor (adapting interfaces)
- Proxy (controlling access)
- Bridge (decoupling)
What’s the difference between a strategy pattern and a bridge pattern?
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.