Design patterns Flashcards

1
Q

what are design patterns

A

represent the best practices used by experienced OOP devs
solutions to general problems that are faced during SW dev
solutions were obtained by trial and error

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

creational patterns

A

provide a way to create objects while hiding the creation logic rather than instantiating objects using “new”. gives flexibility in deciding which objects to create for a given use case

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

structural patterns

A
concern class and object composition
inheritance used to compose interfaces and compose objects to obtain new functionality
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

behavioral patterns

A

concerned with communication between objects

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

5 creational patterns

A
singleton - restricts initialization of a class to ensure that only one instance of a class can be created
factory - mtakes out the responsibility of instantiating an object from the class to a Factory class
Abstract factory - allows us to create a Factory for factory classes
Builder - creating an object step by steo and a method to finally get the object instance
Prototype - creating a new object instance from another similar instance and modify according to our requirements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

7 structural patterns

A

adapter - provides interface between two unrelated entitities so that they can work together
composite - used when we need to implement a whole-part hierachy
proxy - provide a surrogate or place holder for another object to control access to it
flyweight - caching and reusing object instances, used with immutable objects
Facade - creating a wrapper interfaces on top of existing interfaces to help client applications
Bridge - decouple int interfaces from implementation and hiding implementation from client application
Decorator - used to modify functionality of object at runtime

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

8 behavioral patterns

A
template methods
mediator
chain of responsibility
observer 
strategy
command
state
visitor
interpreter
iterator
memento
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

explain the template method, mediator, chain of responsilitity and observer behavioral pattern

A

template method - create a template method stub and defer step of implementation to subclasses
mediator - provide centralized communication medium between different object in the system
chain of resp - used to achieve loose coupling where a request by a client is passed to a chain of objects to process them
observer - useful when you want to get notified when an object changes state

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

explain the strategy, command, state and visitor behavioral pattern

A

strat - multiple algorithms for a specific task and client decides which implementation to use at run time
command - used to implement lose coupling in a request - response model
state - used when an object change its behavior based on an internal state
visitor - used when we have to perform an operation on a group of similar kind of objects

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

explain the interpreter, iterator and memento patterns

A

interpreter - defines a grammatical representation for language and provides an interpreter to deal with this grammar
iterator - used to provide a standard way to traverse through a group of objects
memento - used when we want to save the state of an object so that we can restore later on

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

what type of relationship btwn subject (observerable) and dependents (observer) does the observer pattern maintain?

A

one to may

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

Explain the observer pattern

A

subject maintains a list of its dependents and notifies them automatically of any state changes - usually by calling one of their methods
used to implement event handling in an event driven system

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

source vs sink of events (observer pattern)

A

source - subject

sink - observers

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

Explain the composite design pattern

A

describes group of objects can be treated in the same way as a single instance of the same object type
allows us to compse objects into tree structures to represent whole-part hierarchies
allows us to treat individual objects and compositions in the same way
each tree node performs a taskhas-a relationship between objects

e.g. burger (component), bun & patty (composite objects), wheat & beef etc.

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