2

Here is definition of Strategy Pattern:

Strategy - defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

So, Strategy separate a functionality into two parts: one which doesn't change, and another which can change at some future time.

And, in MVC, they say that:

the controller is the strategy for the view

It means controller is vary and View might not change in the future.

I don't understand so much,yet. I think both of them can change in the future.

Please explain for me why people say that.

1 Answer 1

8

I suspect you may have seen the note "the controller is the strategy for the view" in the C3 wiki section on Model View Controller As An Aggregate Design Pattern, which is interesting, but a bit terse.

The idea is described a bit more completely in the introduction of Design Patterns, where the section on MVC says:

MVC also lets you change the way a view responds to user input without changing its visual presentation. You might want to change the way it responds to the keyboard, for example, or have it use a pop-up menu instead of command keys. MVC encapsulates the response mechanism in a Controller object. There is a class hierarchy of controllers, making it easy to create a new controller as a variation on an existing one.

A view uses an instance of a Controller subclass to implement a particular response strategy; to implement a different strategy, simply replace the instance with a different kind of controller. It's even possible to change a view's controller at run-time to let the view change the way it responds to user input. For example, a view can be disabled so that it doesn't accept input simply by giving it a controller that ignores input events.

The View-Controller relationship is an example of the Strategy design pattern. A Strategy is an object that represents an algorithm. It's useful when you want to replace the algorithm either statically or dynamically, when you have a lot of variants of the algorithm, or when the algorithm has complex data structures that you want to encapsulate.

Summarizing this a bit, you can use the same view with different controllers to have different behavior in different parts of your system, you can replace the controller at run-time to enable or disable particular functions, and the controller encapsulates the complex interactions of updating the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.