Design Pattern
A design pattern is the description of communicating objects and classes that are
customized to solve a general design problem in a particular context, identifying the
participating classes and objects, their roles and collaborations, and the distribution of
responsibilities.
Gang of Four (GoF) Book
Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm,
Johnson, Vlissides, 1994.
Creational patterns
A category of design patterns that includes 5 patterns: Abstract factory, Builder, Factory
method, Prototype, Singleton.
Structural patterns
A category of design patterns that includes 7 patterns: Adapter, Bridge, Composite,
Decorator, Façade, Flyweight, Proxy.
Behavioral patterns
A category of design patterns that includes 11 patterns: Observer, State, Strategy,
Template method, Visitor, Chain of responsibility, Command, Interpreter, Iterator,
Mediator, Memento.
Observer Pattern
Intent: define a 1-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updated automatically..
Observer - interface
An interface that defines the methods for observers to implement.
Observable - class
A class that maintains a list of observers and notifies them of state changes.
Subject
<<interface>> that defines methods for attaching, detaching, and notifying observers.
ConcreteObserver
A specific implementation of the Observer interface that defines how to update when
notified.
,ConcreteSubject
A specific implementation of the Observable class that notifies observers of state
changes.
notify()
A method that subjects must call when they change state to inform observers.
update()
A method that observers must define to update themselves when notified by the
subject.
Patterns
Recurring arrangements of elements that document experience/expertise and serve as
a vocabulary for communication.
Key elements of a design pattern
Name, Problem, Solution, and Consequences.
OO Design Patterns
Object-oriented programming techniques that involve a small group of classes and
objects, leading to reusable design.
Learning Objectives
By the end of this video, you will be able to list the three categories of design patterns
and classify into one of the three pattern categories given a design pattern.
Change notification
The process by which observers are informed of changes in the state of the subject.
Multiple Displays of Same Application Data
A scenario where different observers display the same data from the subject.
Relative Percentages
A method of representing data in terms of proportions, such as A=10%, B=40%,
C=30%, D=20%.
Application data
The data that is requested and modified by the observers.
Observable
Class
Observer
Interface
Subject
,Has a list of observers and methods for attaching/detaching an observer
observers
A collection of Observer instances
ConcreteSubject
Implements Subject and manages state and observers
ConcreteObserver
Implements updating interface for objects that get notified of changes in a subject
StateInfo
Holds the state information relevant to observers
Observer Pattern
A design pattern that defines a one-to-many dependency between objects
attach(Observer)
Method to add an observer to the subject
detach(Observer)
Method to remove an observer from the subject
notify()
Method to notify all attached observers of a state change
update(Subject, StateInfo)
Method called by the subject to update observers with new state information
Cycles of Dependence
A situation where package A cannot function without package B
Eliminating Dependence
Using the Observer pattern to reduce direct dependencies between classes
Loose coupling
A design principle that allows for less interdependence between components
broadcast communication
A method where all observers are notified of changes
Composite Pattern
A design pattern used to treat individual objects and compositions uniformly
Component
An abstract class that defines a part of a composite structure
, children
A set of child components in a composite structure
Book
An example of a composite that can contain chapters and paragraphs
Paragraph
A component that can exist within a chapter in a book
Chapter
A component that can contain paragraphs and is part of a book
Composite Pattern
(Gamma et al., 1994) A design pattern that allows you to compose objects into tree
structures to represent part-whole hierarchies.
Composite
Only Composite can store and enumerate children (aggregation) and add or remove
children.
Leaf
A component that does not have any children.
Part-Of Rule
A is a part of B if, and only if, B is the composite of A.
Ensuring Consistency
The public operations on a composite and its components ensure that pointers from
components to composite and composite to components are consistent.
addChild() in Composite
The addChild operation of Composite updates the container of the component.
Book Example
An example of a composite structure where a Book contains Chapters, Sections, and
Paragraphs.
Interpreter Pattern
A design pattern that defines a representation for a language's grammar along with an
interpreter that uses the representation to interpret sentences in the language.