DEV Community

jzfrank
jzfrank

Posted on

HFDP(12) - Compound Pattern

Compound Pattern is to combine two or more patterns to solve a general problem. One of the most famous compound pattern is the MVC (model view controller) pattern.

MVC involves Model, View, and Controller. When user touch a button, it will invoke controller to do certain things. The controller then invoke the model to modify its state. Finally, the model will notify the view that something changes. The view will be rendered accordingly.

MVC

Learning MVC from top down is a bit involving. However, learning it from bottom up is very simple. MVC is nothing but a combination of patterns.

The controller and the view are using the Strategy pattern. The view does not care the concrete behavior, it just invokes the controller. A concrete controller is just an implementation of an interface and could be easily replaced by another implementation.

The model and the view are using the Observer pattern. The model is the Observable and the view is the Observer. The model could notify the subscribed view the changes of its status. The observer view will update the view accordingly.

The view itself is using the Composite pattern. Consider the relationship between a window and a button. They are both visual elements but have an inclusive relation. A view could be a standalone or could contain other views.

Sometimes we want to reuse a controller. Often we need to use Adapter pattern to adapt the interface.

MVC pattern is so useful that it gets adapted and used in many popular frameworks. In the web world, a thin client approach is where the model, most of view, and the controller all reside in the server, while the browser renders the view. Single Page Application approach is all the model, view and controller reside in the browser. Most frameworks lie in between the two extremes.

Top comments (0)