Lecture 18
Pointers and references to an abstract class can be declared.
Observer Pattern
- publish-subscribe model
- One class:
publisher/subject
- generates data - One or more classes:
subscribe/observer classes
- receive and react to that data - *Example: **publisher spreadsheet cells, observers are graphs or formula cells, when the cells change, graphs/formula cells update themselves.
There can be may different types of observer classes, and the subject should not need to know all the details about them.
Observer Pattern: UML
The abstract class subject contains all the code common to all subjects - the abstract observer contains the interface common to all observers
Sequence of method calls:
- Subject’s state is changed
Subject::notifyObservers
is called (either by the concrete subject when the change happens, or by the client when they change the subject.notifyObservers
calls notify on each observer- Each
ConcreteObserver::noitfy
gets the data from their subject (e.g. withgetState
) and react accordingly
Example: Lets write some code on horse races. The subject publishes the winner, the observers are individual betters who react to winning or losing
If you want a class to be abstract, but don’t naturally have a method to make pure virtual, make the destructor pure virtual (since it should always be virtual if you have inheritance). However, the destructor still needs an implementation, else, the code won’t link. Hence, we implement it outside of the class body.
Now, lets write the Observer
class:
Now, lets write our concrete classes:
For more details on the code, checkout .\\lectures\\se\\02-observer
Decorator pattern:
Suppose we want to enhance an object at runtime.
(e.g. add or change features). For example. a windowing system - we could start with a basic window, and have options to add a scrollbar and a menu. We would like to choose these options at runtime.
Adding these at runtime means we could start with just a basic window and add a scroller when it becomes necessary for example.
// insert picture here
class Component
defines the interface: operations your objects will provide. ConcreteComponent
is your basic undecorated object, so offers base implementations of these.
The decorator
classes all inherit from Decorator
which inherits from component
.
For example: Ordering Pizza
// insert first pizza uml here
// insert second pizza uml here
Now, we need our decorator