Mediator to Model-View Controller

Slides:



Advertisements
Similar presentations
Design Patterns CMPS Design Patterns Consider previous solutions to problems similar to any new problem ▫ must have some characteristics in common.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
MVC Fall 2005 OOPD John Anthony. Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Object-Oriented Analysis and Design
March R McFadyen1 Figure 30.2 Layers in NextGen They only have three layers in this architecture Each layer is shown as a UML Package No separate.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Client/Server Software Architectures Yonglei Tao.
MVC pattern and implementation in java
Model View Controller (MVC) Rick Mercer with a wide variety of others 1.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Putting together a complete system Chapter 10. Overview  Design a modest but complete system  A collection of objects work together to solve a problem.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Model View Controller A Pattern that Many People Think They Understand, But Has A Couple Meanings.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ANDROID AND MODEL / VIEW / CONTROLLER. Slide 2 Design Patters Common solutions to programming problems are called design patterns Design patterns are.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
OOD teaches a complicated method, best for large systems. Here we teach the ten cent version. OO Design for the Rest of Us 1.
MVC COMP 401 Fall 2014 Lecture 19 10/30/2014. Classic MVC 2
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Basic Organization of UI Software. 2 The User Interface n Typically want to think of “UI” as only one component of an overall system – The part that “deals.
Event-driven design will set you free The Observer Pattern 1.
My Observer Goes to 11 Model-View Controller 1. Which is the best class diagram? 2 ___Edge___ drop() _Vertex_ addEdge() dropEdge() _Iterable_ * 2 ___Edge___.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Façade and the DP Wrap-Up 1. Which pattern does this class diagram from the Factory chapter call out for? A.Strategy B.Decorator C.Adapter D.Factory 2.
1. 2 Android location services Determining a device’s current location Tracking device movements Proximity alerts.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Imposing MVC design sample in.NET. Design patterns are very useful to solve complex design issues if used well. The primary concept of the Model View.
Observer Pattern Context:
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
Instructor: Mazhar Hussain
Observer Design Pattern
Design Patterns with C# (and Food!)
Callbacks and Interacting Objects
ADO.NET Entity Framework Marcus Tillett
Model-View-Controller Design Pattern
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
What is MVC Category: System MVC=Model-View-Controller
Patterns of Interaction 2: Publish-Subscribe
Object Oriented Programming (OOP) LAB # 8
Jim Fawcett CSE776 – Design Patterns Summer 2003
Figure 30.2 Layers in NextGen
Model-View-Controller Patterns and Frameworks
Quantum One.
Model-View-Controller (MVC) Pattern
Model-View-Controller
Composite Pattern Context:
Object Oriented Design Patterns - Behavioral Patterns
CSC 480 Software Engineering
Starting Design: Logical Architecture and UML Package Diagrams
Review: Design Pattern Structure
Hardness Of Approximation
GoF Design Patterns (Ch. 26)
Planning and Storyboarding a Web Site
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Introduction to Design Patterns
Model, View, Controller design pattern
Informatics 122 Software Design II
CSC 480 Software Engineering
11. MVC SE2811 Software Component Design
11. MVC SE2811 Software Component Design
The Main Connective (Again)
UML  UML stands for Unified Modeling Language. It is a standard which is mainly used for creating object- oriented, meaningful documentation models for.
Logical Architecture & UML Package Diagrams
Presentation transcript:

Mediator to Model-View Controller My Observer Goes to 11

Mediator Pattern When in doubt add a level of indirection

myCity Story: Show Friends on Map Given I am logged in And my location is known When my friends are online And my friends’ location is known And my friends are near me Then I see them on the map How get friends drawn on the map? What thing does something to itself so friends get drawn on map? class Friend { Location location(); // null −> not-known boolean online(); Bitmap image(); } class Friends implements Iterable<Friend> { Friends online(); Friends locationKnown(); // −> online? }

Nearby Friends: Subclass Approach <interface> _Iterable_ … _GoogleMap_ … x _________Friends_________ - List<friend> friends −−−−−−−−−−−−−−−−− + Friends online() + Friends locationKnown() __NearbyFriendsMap__ - GoogleMap map - Friends friends −−−−−−−−−−−−−−−−− + showNearbyFriends() * Actually has good SRP superclass does map subclass does friends but: “is-a” map or “has-a” friends? “has” all kinds of things, incl. friends DP says subclassing is usually inferier Anyway, we can’t subclass GoogleMap _________Friend_________ + boolean online() + Location location() + Bitmap image()

Nearby Friends: Mediator Approach <interface> _Iterable_ … _________Friends_________ - List<friend> friends −−−−−−−−−−−−−−−−− + Friends online() + Friends locationKnown() _NearbyFriendsTracker_ - GoogleMap map - Friends friends −−−−−−−−−−−−−−−−− + showNearbyFriends() _GoogleMap_ … * This is a mediator design solves problem of complex inter- object relationship (friends <−> Map) lets each focus on their single responsibility _________Friend_________ + boolean online() + Location location() + Bitmap image()

Mediator Pattern Name: Mediator Problem: Object interactions or relationships are complex or interdependent Solution: Introduce an object whose responsibility is mediating the interactions or maintaining the relationships, removing those responsibilities from the mediated objects

Model-View Controller Once again, the ten-cent version

How should NFT get called, by whom? <interface> _Iterable_ … _________Friends_________ - List<friend> friends −−−−−−−−−−−−−−−−− + Friends online() + Friends locationKnown() _NearbyFriendsTracker_ - GoogleMap map - Friends friends −−−−−−−−−−−−−−−−− + showNearbyFriends() _GoogleMap_ … How does NearbyFriendsTracker get called when Friends changes? Rename showNearbyFriends to “update” Put NFT in its own thread and have it poll friends for changes, call sNF Observer pattern: Make Friends a “subject” and NFT an “observer” of it Observer pattern: Make Friend a “subject” and NFT an “observer” of it * _________Friend_________ + boolean online() + Location location() + Bitmap image() A: No. SNF is like the “display” method in ForecastDisplay. We need a separate “update” or “changed” method, which will call SNF. B:You could do that, but it’s massively inefficient, since Friend status changes rarely. Also, complex – hard to understand, debug, etc. Overkill. C: Yes. Recall that Observer is called Listener in Android and Java in general. D: No, that defeats the whole purpose of Friends as an aggregator class. See C.

Discussion A: No. SNF is like the “display” method in ForecastDisplay. We need a separate “update” or “changed” method, which will call SNF B:You could do that, but it’s massively inefficient, since Friend status changes rarely. Also, complex – hard to understand, debug, etc. Overkill C: Yes. Recall that Observer is called Listener in Android and Java in general D: No, that defeats the whole purpose of Friends as an aggregator class. See C.

How should NFT get called, by whom? <interface> _Iterable_ … _________Friends_________ - List<friend> friends −−−−−−−−−−−−−−−−− + Friends online() + Friends locationKnown() _NearbyFriendsTracker_ - GoogleMap map - Friends friends −−−−−−−−−−−−−−−−− + showNearbyFriends() _GoogleMap_ … How does NearbyFriendsTracker get called when GoogleMap changes (e.g., you move)? Observer pattern: have NFT observe your location through LocationManager Observer pattern: Make Friends a “subject” and NFT an “observer” of it Observer pattern: Make GoogleMap a “subject” and NFT an “observer” of it * _________Friend_________ + boolean online() + Location location() + Bitmap image() A: No. That’s just one way the view port on the map changes. B: No. That was for dealing with changes to Friends online status and location, not map’s changing view port. C: Yes. That’s the idea. Of course, GM is final, but it already has a register method and all, like LocationManager.

Discussion A: No. That’s just one way the view port on the map changes B: No. That was for dealing with changes to Friends online status and location, not map’s changing view port C: Yes. That’s the idea. Of course, GM is final, but it already has a register method and all, like LocationManager.

Mediator + Observer (x 2) <interface> _Iterable_ … <interface> _____FriendsListener____ onFriendOnline(Friend) onFriendOffline(Friend) onFriendMove(Friend) <interface> _FriendSubject_ register, notify,… <interface> _____OnCameraChangeListener____ onCameraChange(CameraPosition) * * _NearbyFriendsTracker_ - GoogleMap map - Friends friends −−−−−−−−−−−−−−−−− + showNearbyFriends() _________Friends_________ - List<friend> friends −−−−−−−−−−−−−−−−− + Friends online() + Friends locationKnown() _GoogleMap_ … It’s more than Observer x 2 because NFT directly queries and manipulates Friends and GMap to maintain the relationship between the two. Observer just “observes”, it doesn’t manipulate. * _________Friend_________ + boolean online() + Location location() + Bitmap image()

MVC Object Interactions a NearbyFriendsTracker gm.addMarker(…), etc. friends.registerObserver(this) gm.setOnCameraChangeListener(this) onFriendMove(Friend), etc. onCameraChange(CameraPosition) a Friends a GoogleMap

Who “knows about” who? Friends knows NFT, which knows Gmap Friends and GMap know about NFT NFT knows about Friends and Gmap NFT, Friends, & GMap all know about each other C: NFT knows about Friends and Gmap It’s the only one making direct calls on the other objects. Although Friends and GMap make callbacks on NFT, those are methods first declared in the interfaces. Friends and GMap know about those interfaces, but not the classes that implement the methods. This “separation of concerns” allows NFT to glue together Friends and GoogleMap, without either having to know about the other. They do have to anticipate that something wants to observe them, but they don’t know who that is, just that they implement their observers. Knows about: Type A knows about Type B if A calls or uses B in any way

Discussion of who knows about who Best Answer - C: NFT knows about Friends and Gmap Technically NFT doesn’t even know about Friends, just the FriendsSubject interface. In theory this allows NFT to integrate other implementations of “friends”, as long as they implement the FriendsSubject It’s the only one making direct calls on the other objects Although Friends and GMap make callbacks on NFT, those are methods first declared in the interfaces Friends and GMap know about those interfaces, but not the classes that implement the methods This “separation of concerns” allows NFT to glue together Friends and GoogleMap, without either having to know about the other They do have to anticipate that something wants to observe them, but they don’t know who that is, just that they implement their observers

Model-View Controller Terminology Model: the “data”; also known as Subject (Friends) View: the visualization of the data, often a UI (Gmap) Controller: mediator that manages the relationships MVC handles two limitations of Observer View is not just a passive view (e.g., a visualization) a UI where the View changes to an alternate view View makes changes to the Model Model and View pre-exist Can’t use Observer pattern because View doesn’t implement Observer interface for Subject/Model So, what do you do when you want to connect two pre-existing, incompatible components?

Ten-Cent MVC: Class Diagram <interface> _ModelSubject_ register, notify,… <interface> __ModelListener__ update(…) <interface> _ViewListener_ update(…) <interface> _ViewSubject_ register, notify,… * * __Model__ … _Controller_ _View_ … A: No. That’s just one way the view port on the map changes. B: No. That was for dealing with changes to Friends online status and location, not map’s changing view port. C: No. That’s the idea. Of course, GM is final, but it already has a register method and all, like LocationManager.