chapter 4 Flashcards
define a design pattern.
pattern is a general repeatable solution to commonly occurring problem in OO software design [ context ].
is a design pattern a finished design? why?
- its not a finished design that can be transformed into a code, but rather a template for how to solve a problem.
Example: - house blueprint
the book; The Gang of Four’s book was written based on the principles of _____________ ?
Christopher Alexander
what did the GoF’s book achieve?
initiated the concept of design patterns in software dev’t
according to the GoF, a design pattern should be based on which OO principles?
- program to an interface not an implementation
- favor object composition over inheritance
what are types of DP defined by the GoF?
- creational dp
- structural dp
- behavioral dp
describe creational dp
- creates objs in a manner suitable for the situation.
- deals with obj instantiation
- hides creation logic, instead of just using the new operator
describe a structural dp?
- deals with r/n ships b/n entities, to make it easier for them to work together
- concerned with classes and object composition
describe behavioral dp
- concerned with common communication patterns b/n objects
a factory method is a pattern catalog belonging to ____________ dp?
creational
an adapter is a pattern catalog belonging to ____________ dp?
structural
what are characteristics of a dp?
LV R
- levels - decomposition of patterns
- view points - description of patterns
- roles - “players” in pattern usage
what is the d/ce b/n static and dynamic viewpoints?
- static vp - describes class models [ building blocks ]
- dynamic vp - seq diagrams
what are the forms of a dp?
FDR
- delegation
- recursion
describe delegation dp form.
- a mechanism in w/c an obj delegates a req to another obj
- the other obj carries out the req on behalf of the original obj
describe recursion dp?
- a mechanism in w/c part of the pattern uses itself
Example: - GUIs where windows within windows are allowed
what are the goals of a dp?
GRE
- reuse of designs
- evolution of sw
how can we achieve the goals of a dp?
by reducing dependencies among components
- program to interfaces not implementations
- favor obj composition not inheritance
- use delegation
what are elements of a dp?
NPC are Sus
- name
- problem
- consequence
- solution
____________ is an element of a dp, that describes when to apply a pattern?
problem
____________ is an element of a dp, that describes elements that make up the pattern, their r/n ship and responsibilities?
solution
____________ is an element of a dp, that describes results and the tradeoffs of applying the pattern?
consequence
explain what scope is in a dp
- describes what the pattern applies to
Example: - class patterns : focus on r/n ships b/n classes and subclasses
- obj patterns : focus on r/n ships b/n objs
what scope of dp deals with class inheritance?
class pattern
what scope of dp deals with obj composition?
object pattern
explain how creational patterns provide flexibility?
- the pattern creates objs for us, instead of us instantiating objs
- hence program has flexibility in deciding which objs need to be created
what is the intent of a simple factory pattern?
- abstracts logic of object creation [ simpleBurger Factory ]
- refers to newly created obj through a common interface [ Burger burger ]
describe the adapter design pattern?
the example about xml and json
- is a structural dp that allows incompatible interfaces to collaborate with one another
what is the intent of an adapter pattern?
- matches an existing object to a particular interface
compare and contrast b/n inheritance and composition.
Inheritance : aka white box
- all subclasses are dependent [ tightly coupled ] with the parent class
- unnecessarily complex
- can easily break code upon change
Composition : aka black box
- one class can use another
- loosely coupled
- implementations can be easily replaced with out breaking code
give examples of types of behavioral dp.
- observer dp
- memento dp
what is the observer pattern?
remember the store, owner and customer
- is a behavioral dp that defines a 1-to-many r/n ship b/n objects [ subject and observer ]
- so when a subject changes state, all observers are notifies and updated
what are examples of an observer dp?
- broadcasts
- publish - subscribe
what are the key players in the observer pattern?
- subject / publisher [ list of sub, sub and unsub methods ]
- object / subscriber - an updating interface for obj that get notified upon changes to a subject
- concrete subject
- concrete observer - implements update interface
describe the memento dp?
is a behavioral dp that delegates the task of creating the State of Snapshots to the actual owner of that state.
- prevents other objects from trying to copy the state of the class from outside of the class
what is memento pattern used for?
- is used to restore the state of an object to a previous state
Example: - Ctrl + Z in editors
- check points
what are the actor classes used in memento dp? describe what they do.
- Memento - contains current state of an object
- originator [ TextArea ] - creates and stores states in the Memento object [ snapshots ]
- Caretaker [ editor ] - restores object state from the Memento object [ list of snapshots yizo vibe ]
what is the intent of the memento dp?
- captures and externalizes an objects state with out violating its encapsulation
- since it can only be accessed by the class its contained under
- it will not expose internal data of an object to objects outside the class
how many interfaces does a memento have?
- 2
- originator sees a wide interface
- caretaker sees a narrow interface
___________ is responsible for memento’s safekeeping?
caretaker
what are benefits of a dp?
AFFRII
- provides access from experts to non experts
- facilitates comm
- facilitates design improvement
- reuse of successful designs
- improve design understandability
- improve design doc