Designing Components

Recall the Model-View-Controller architecture.

Here are just a few patterns that might get used in the design of the MVC components:

 

The Facade Pattern

In a simple application the model might be a single class component. In a more complex application the model might be a component made up of multiple classes. In this case we can use the Facade Pattern to create a single entry point into the model.

The Publisher-Subscriber Pattern

Views need to redraw themselves every time the model is updated. But how does a view know when the model has been updated? After all, it might have been a controller component that updated the model. Also, there may be multiple views that need to be updated. This is called the View Notification Problem and it can be solved using the Publisher-Subscriber Pattern.

An MVC Framework

The MVC architecture combined with the Publisher-Subscriber Pattern can be expressed as a framework:

The MVC Framework

The Composite Pattern

A view can be regarded as a tree in which parent nodes are windows and sub-windows  and leaf nodes are controls (buttons, text fields, etc.). All tree-like structures instantiate the Composite Pattern.

The Command Processor Pattern

The controller can instantiate the Command Processor Pattern in order to implement undo/redo mechanisms and transactions.

The Strategy Pattern

Different views may use different strategies for laying out their components. We can use the Strategy Pattern to decouple View objects from the algorithms they use for layout.

The Decorator Pattern

A basic view is simply a rectangular containing graphics. This is fine for some situations, but we may want views with additional features such as borders, titles, menu bars, etc. We can add features to an existing class using the Decorator Pattern.

The Adapter Pattern

When using the Decorator Pattern, the component being decorated may not implement the interface required by the pattern. We can use the Adapter Pattern to adapt any existing class to a given interface.

The Factory Method Pattern

We may want to provide users some way of creating new models or new views on the fly. The Factory Method Pattern allows us to decouple the logic of creating a new object from the specifics of what object is created.

More Patterns

Pattern Catalog