13.2. UML

The following Unified Modified Language (UML) diagram details a class design using the Observer Design Pattern (see Figure 13-1) that is discussed in the following list:

Figure 13-1. Figure 13-1
  • MyObject is the Observable object. It contains a protected array of observers called observers. The public method addObserver() takes an instance of an observer and stores it in the array.

  • The doSomething() public method is called. doSomething() applies a state change to the MyObject. It then calls the notify() public method, which loops through the array of observers.

  • MyObjectObserver has a public method called change(), which accepts an instance of MyObject. This particular observer then does something with the content of the MyObject. The notify() method of MyObject directly calls this method when it encounters it in the array of observers.

Get Professional PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.