lundi 14 février 2022

MVC is (not?) a combined pattern of Composite, Strategy and Observer according to Head First?

The Gang of Four book states the following:

[...] The main relationships in MVC are given by the Observer, Composite and Strategy design patterns.

The best practical example I could find of this was in this github repo:

enter image description here

Where the following holds:

MVC Name GoF Name GoF Pattern
Model Subject Observer
View Observer Observer
ConcreteView ConcreteObserver Observer
Controller Strategy Strategy
Button Leaf Composite
Text Leaf Composite
Window Composite Composite
ConcreteModel ConcreteSubject Observer
ConcreteController ConcreteStrategy Strategy

In contrast, the book Head First Design Patterns states the following about the MVC:

The Controller may also ask the view to change. When the controller receives an action from the view, it may need to tell the view to change as a result. For example, the controller could enable or disable certain buttons or menu items in the interface

In the subsequent example, the authors allow the ConcreteController equivalent BeatController) to define an association with the ConcreteView equivalent (DJView):

package headfirst.designpatterns.combined.djview;
  
public class BeatController implements ControllerInterface {
    BeatModelInterface model;
    DJView view;
   
    // BeatController implementation
}

I can sort of see the practical reason they did this, namely that the Controller needs to talk to the View. However, this association is not present in the UML above, and indicates a deviation from the GoF idea that MVC is an Observer, Composite and Strategy superpattern.

Why did they do this? How does it fit into the GoF description?

Aucun commentaire:

Enregistrer un commentaire