design patterns Flashcards
what are design patterns
reusable solutions to common problems that occur in software design
what are the 4 elements of a design pattern
name; description of the pattern in 2 or less words
problem; when you should use the pattern
solution; elements that make up the design and interactions responsibilities and relationships
consequences; trade offs of using the pattern
what are the 3 categories of design patterns
creational; techniques to create objects
structural; how classes are imposed in larger structures
behavioural; communication between objects
what is the observer design pattern
behavioural design pattern that allows you to define a subscription mechanism to notify other objects about any changes made in the object their observing
subject/publisher
the object that is changing state and informs observers
observers/subscribers
dynamically attached to the subject and can subscribe to event notifications
subject as an abstract class
abstract class defines add remove and notify observers
the concrete subject extends the abstract class
the concrete observer implements the observer interface and registers with concrete subjects to receive updates
subject as an interface
can add remove and notify observers
the concrete subject implements this and keeps a list of the observers and notifies them
the concrete observer implements the observer interface and registers with concrete subjects to receive updates
what is an adaptor design pattern
structural design pattern that converts the interface of a class into an interface that the client expects
what are the 4 classes needed for the adaptor
client; who wants to use the interface
target; the interface that the client uses
adaptor; adapts the adaptee to the target interface
adaptee; the existing interface that needs changing
class adaptor
adapts the adaptee by inheriting it and implementing the target interface
object adaptor
relies on composition
the adaptor implements the target interface and contains the adaptee instance to which it forwards the request
proxy
provides a placeholder for a real object to control access to it
what are 3 reasons for using proxies
authenticates a client
can create the subject on demand and only when needed
can stand in for something complex elsewhere
how does a proxy work (implementation)
both the proxy and the subject classes implement the subject interface whilst the proxy keeps an instance of the real subject
when the client wants to access the object it has to go through the proxy which requests the real object
remote proxy
provides a local representation for an object in a different address space
protection proxy
validates access to the object
the proxy keeps an instance to the object which the client can only access through the proxy
virtual proxy
enables the creation of expensive objects on demand
not all data from storage is loaded into memory when the object is called instead a list is kept of currently needed data therefore you only need to open the file when you need to add something to it
builder
separates the creation of a complex object from its implementation so that the process can be used to create different representations
how does a builder work
individual steps are separated into different methods and the director object specifies the steps in the correct order and different valued can be passed into the methods which will lead to different outcomes
flyweight
optimising memory usage by sharing a common state amongst multiple objects
how does a flyweight work
the flyweight interface has methods that the objects must have; unique data
the concrete implementation has objects that store shared data that can be used by many objects
the flyweight factory manages a pool of flyweight objects reusing them and only making new ones when necessary
the client passes in unique data whilst shared data stays the same