Disadvantages of "1 Object" and "2 Objects" pattern:
Source code becomes flooded with algorithms, data structures etc.,
Tough to understand and maintain.
Makes the code less flexible to changes and difficult to reuse because of tight coupling.
All the above issues can be solved by using "3 objects" pattern (MVC).
Model-View-Controller (MVC)
It is an architectural pattern. Mainly used in GUI architectures.
Helps to decouple presentation, functionality and input handling.
- Model: Functionality or data
- View: Screen presentation of model
- Controller: Defines how view reacts to user input
VIEW:
For Example,
Create scrollbar, title, border, etc., as decorators and design the View in such a way that decorators can be added or removed at runtime.
This design pattern allows new behavior to be added to an existing class menu view class dynamically. In other words, using this functionality, menu can be extended at runtime.
CONTROLLER:
Different algorithms are needed to support these features and it has to be designed in such a way that View can easily select an algorithm at runtime.
This relationship between controller and view can be represented by using strategy design pattern.
Strategy design pattern:
Encapsulates each algorithm, and makes them selectable at runtime.
Using this pattern, Algorithms can be replaced either statically or dynamically.
Decouples an algorithm from its host.
MODEL:
Model notifies View, when Controller changes the Data. This has to be achieved without coupling Model and View.
For Example,
These menus need different functionality, so coupling Model and View directly makes it less flexible to changes and difficult to reuse.
This design problem can be solved by using Observer design pattern.
Observer design pattern:
Represents relationship between Model and View.
It helps to decouple Model from the View.
Notifies associated Views when the Model changes.
For more information about MVC pattern.
Model-View-Controller - Wikipedia
Model-View-Controller - MSDN
No comments:
Post a Comment