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