Free Essay

Gut1 Task 3

In: Computers and Technology

Submitted By stinkyhduck
Words 746
Pages 3
Design Patterns
By XXX

Agenda
§ Model-View-Controller (MVC) § Observer Pattern

Model-View-Controller Design Pattern (MVC)

What is MVC?
§ MVC stands for Model-View-Controller. § MVC originated from SmallTalk in the 1970s. § Common pattern which is now used for web applications as well as the standard pattern used for iOS applications. § MVC is a proven design pattern to create a complex application that enforces separation of concerns, provides loose coupling and provides an avenue to create testable code.

Model-View-Controller
Controller

Model Model never talks to the View

View

Model Responsibilities
§ The “M” in MVC. § Consists of application data, business rules and logic, domain specific classes/methods/functions. § Example: Hydrating model objects like Person object from a database. § Models can broadcast messages (notifications) if other objects update the model (subscribe/unsubscribe pattern).
Controller

Model Model never talks to the View

View

Controller Responsibilities
§ The “C” in MVC. § The traffic cop that communicates between the Models and the Views. § The controller understands how to take the models and provide them appropriately to the view. § The controller also understands how to update the model based on user interactions from the view.
Controller

Model Model never talks to the View

View

View Responsibilities
§ The “V” in MVC § Any output that is visible to the user. (ex. Buttons & Graphics) § A view may have other embedded views. (ex. A screen may have two charts. Each chart would have its own encapsulated view. § View sends messages to the controller without knowing anything about the controller. (ex. delegates or protocols)
Controller

Model Model never talks to the View

View

Benefits of MVC
§ Code reusability § Testable code § Clean separation of concerns

Applications of MVC
§ The MVC pattern is the core pattern for iOS applications. The pattern provides good separation of concerns, loose coupling between objects and lends itself to testability which is essential to delivering high quality software. § As of recent the MVC pattern has been adopted by ASP.NET. ASP.NET paradigm by nature has tight coupling between the code behind (logic) and the view (html pages). The logic cannot be easily tested since there is tight coupling with the view. The MVC movement solves the tight coupling problem between the view and the logic by introducing the controller and splitting out the concerns. This then lends itself to testable code and loose coupling.

Observer Pattern

What is the Observer pattern?
§ The Observer pattern is used in conjunction with the MVC pattern. § This pattern provides the loose coupling desired between the Model and the Controller and the View and the Controller. § This pattern is also known as the Publish-Subscribe pattern.
§ An object can subscribe or listen to notifications/messages for another object. The publishing object does not know all it’s subscribers.

§ Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observer Pattern
(Think magazine subscription) Send magazine to subscribed customer

Customer1 Customer2 Customer3 Subscribe to Subscription Magazine Publisher

Send magazine to subscribed customer

MVC Implementation
§ Where does the observer pattern fit into the MVC pattern? § The key benefit of the observer pattern is loose coupling between objects. § The model would implement the observer pattern.
§ Provides a subscription model where other objects can subscribe to notifications. § When the model state changes, any “observers” or subscribers would receive a notification without the model object knowing anything about it’s subscribers.

§ The view would implement the observer pattern.
§ The controller would subscribe to UI events. § The view would UI event messages to any subscribers without having a need to know which type of object is listening.

Benefits of the Observer Pattern
§ Used to implement the MVC pattern. § Provides a mechanism to communicate between objects using without each object having to know anything about the other object. § By it’s nature the pattern lends itself to loose coupling.

Applications of the Observer Pattern
§ The Observer pattern is a key component of MVC. § Any MVC implementation would require the use of the observer pattern to provide the ability to create loosely coupled objects. § Model objects may be updated by other threads or other objects in your application. To communicate the state changes in your model objects, the observer pattern provides a mechanism to send messages to all of it’s subscribers.
§ The controller can subscribe to these messages without having to create a direct link to the model objects which would otherwise break the MVC pattern (high coupling).

Thank you for your time!

@

Similar Documents