Design patterns Flashcards
what are design patterns
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
creational patterns
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
structural patterns
concern class and object composition inheritance used to compose interfaces and compose objects to obtain new functionality
behavioral patterns
concerned with communication between objects
5 creational patterns
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
7 structural patterns
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
8 behavioral patterns
template methods mediator chain of responsibility observer strategy command state visitor interpreter iterator memento
explain the template method, mediator, chain of responsilitity and observer behavioral pattern
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
explain the strategy, command, state and visitor behavioral pattern
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
explain the interpreter, iterator and memento patterns
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
what type of relationship btwn subject (observerable) and dependents (observer) does the observer pattern maintain?
one to may
Explain the observer pattern
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
source vs sink of events (observer pattern)
source - subject
sink - observers
Explain the composite design pattern
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.