design patterns model – view – controller

9
Design Patterns Design Patterns Model – View – Model – View – Controller Controller

Upload: nevada-brown

Post on 02-Jan-2016

30 views

Category:

Documents


2 download

DESCRIPTION

Design Patterns Model – View – Controller. History. A framework pattern for reusable applications. Depends on the Observer pattern. First developed by Xerox PARC for Smalltalk-80. Used by the Application Kit system in NeXTstep. Used by the Cocoa APIs for Apple’s OS X. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Design Patterns Model – View – Controller

Design PatternsDesign PatternsModel – View – Model – View –

ControllerController

Page 2: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

HistoryHistory

► A framework pattern for reusable applications.A framework pattern for reusable applications.► Depends on the Observer pattern.Depends on the Observer pattern.

► First developed by Xerox PARC for Smalltalk-80.First developed by Xerox PARC for Smalltalk-80.► Used by the Application Kit system in NeXTstep.Used by the Application Kit system in NeXTstep.► Used by the Cocoa APIs for Apple’s OS X.Used by the Cocoa APIs for Apple’s OS X.► Recommended structural framework pattern in Recommended structural framework pattern in

J2EE.J2EE.

► I have used this pattern for nearly ten years.I have used this pattern for nearly ten years.

Page 3: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

Observer PatternObserver Pattern

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

►Used to decouple the subject from the Used to decouple the subject from the observer, since the subject needs little observer, since the subject needs little information to notify the observer.information to notify the observer.

►Can result in excessive notifications.Can result in excessive notifications.

Page 4: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

Observer Class DiagramObserver Class Diagram

Observable

+addObserver(Observer)+deleteObserver(Observer)+notifyObservers(Object)

#hasChanged() : boolean#setChanged()

Observer

+update(Observable,Object)

AccountView

+update(Observable,Object)

BankAccount

+widthdraw(double) : long+deposit(double) : long+getBalance() : double

SummaryView

+update(Observable,Object)

Page 5: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

Transactions Happen!Transactions Happen!

Controller BankAccount AccountView SummaryViewdeposit()

setChanged()

notifyObservers()

update()

update()

getBalance()

getBalance()

Page 6: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

Observer Rocks!Observer Rocks!

►The Observer pattern allowed the The Observer pattern allowed the BankAccount class to notify multiple BankAccount class to notify multiple views without minimal information.views without minimal information.

►Observers can register themselves with Observers can register themselves with their Subjects. No strings attached!their Subjects. No strings attached!

►Transactions would cause this design to Transactions would cause this design to collapse under spurious notifications!collapse under spurious notifications!

Page 7: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

Architecture DiagramArchitecture Diagram

ViewView

model representation

ModelModel

business logic

ControllerController

user interaction

UpdatUpdatee

EventEvent

UserUserActionAction

ss

ChangChangee

ViewView

SetSet

StateState

GetGetStateState

Page 8: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

AdvantagesAdvantages

►Separation between the data layer and Separation between the data layer and the interface is the key:the interface is the key: The view is easily replaced or expanded.The view is easily replaced or expanded. Model data changes are reflected in all Model data changes are reflected in all

interfaces because all views are Observers.interfaces because all views are Observers. Better scalability since UI and application Better scalability since UI and application

logic are separated.logic are separated. Distribution over a network is greatly Distribution over a network is greatly

simplified.simplified.

Page 9: Design Patterns Model – View – Controller

Copyright © 2001 DeLorme28 November 2001

ProblemsProblems

►Problems of translation:Problems of translation: Business logic bleeds into the Controller.Business logic bleeds into the Controller. Observer pattern is non-obvious for EJBs.Observer pattern is non-obvious for EJBs.

See See EJBObserverEJBObserver by Greg Comeau. by Greg Comeau.

►Problems of the pattern:Problems of the pattern: Excessive coupling between the Model Excessive coupling between the Model

and View and the Model and Controller.and View and the Model and Controller. Frozen interfaces are hard to manage!Frozen interfaces are hard to manage!