Model View Controller Architectural Pattern and Observer Pattern

Slides:



Advertisements
Similar presentations
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
CSE Software Engineering: Analysis and Design, 2002Lecture 7B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/DMS/2002/15.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
Object-Oriented Analysis and Design
Design Pattern – Bridge (Structural) References Yih-shoung Chen, Department of Information Engineering, Feng Chia University,Taiwan, R.O.C. The Bridge.
Design patterns Observer,Strategi, Composite,Template (Chap 5, 6)
Application Architectures Vijayan Sugumaran Department of DIS Oakland University.
Model View Controller (MVC) Architecture. Terminology and History MVC evolved from Smalltalk-80 Has become a key pattern in web based applications – If.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Şabloane de Proiectare Daniel POP, Ph.D. 2 The Plan Introducing fundamental patterns (today) 18 design patterns will be covered based on the case study.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
MVC pattern and implementation in java
MVC and MVP. References enter.html enter.html
Model View Controller (MVC) Rick Mercer with a wide variety of others 1.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Model View.
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
Aniruddha Chakrabarti
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
CS 325: Software Engineering February 12, 2015 Applying Responsibility-Assignment Patterns Design Patterns Situation-Specific Patterns Responsibility-Assignment.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Design patterns.
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many others 1.
1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt 1.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Chapter 18 The Observer Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
CSC 480 Software Engineering Design With Patterns.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
C++ Design Patterns 1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Joey Paquet, Emil Vassev. 2007, 2011, 2013.
Model View Controller (MVC) an architecture Rick Mercer with help from many of others 1.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Software Architecture and Design BITS C461/IS C341 Software Engineering First Semester Aditya P. Mathur Department of Computer Science Purdue.
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
Design Patterns: MORE Examples
Software Design Refinement Using Design Patterns
The Object-Oriented Thought Process Chapter 15
MPCS – Advanced java Programming
A Brief Introduction to Design Patterns
Observer Design Pattern
Design Patterns - A few examples
CS102 – Bilkent University
Design Patterns Outline
Web Programming Language
Advanced Program Design with C++
CSC 480 Software Engineering
Observer Pattern 1.
Advanced ProgramMING Practices
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Advanced ProgramMING Practices
CSC 480 Software Engineering
Presentation transcript:

Model View Controller Architectural Pattern and Observer Pattern DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Model View Controller Architectural Pattern and Observer Pattern C++ Design Patterns Joey Paquet October 19, 2011

The MVC Architectural Pattern :: Introduction MVC was first introduced by Trygve Reenskaug at the Xerox Palo Alto Research Center in 1979. Part of the basic of the Smalltalk programming environment. Widely used for many object-oriented designs involving user interaction. A three-tier architectural model: MVC and Observer in C++

The MVC Architectural Pattern :: Model Manages the behavior and data of the application domain, Responds to requests for information about its state (usually from the view), Responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react. (see observer pattern) In enterprise software, a model often serves as a software approximation of a real-world process. In a game, the model is represented by the classes defining the game entities, which are embedding their own state and actions. MVC and Observer in C++

The MVC Architectural Pattern :: View Renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. The view renders the contents of a portion of the model’s data. If the model data changes, the view must update its presentation as needed. This can be achieved by using: a push model, in which the view registers itself with the model for change notifications (see the observer pattern) a pull model, in which the view is responsible for calling the model when it needs to retrieve the most current data. MVC and Observer in C++

The MVC Architectural Pattern :: Controller Receives user input and initiates a response by making calls on appropriate model objects. Accepts input from the user and instructs the model to perform actions based on that input. The controller translates the user's interactions with the view it is associated with, into actions that the model will perform. A controller may also spawn new views upon user demand. MVC and Observer in C++

The MVC Architectural Pattern :: Interactions between Model, View and Controller Upon creation of a Model-View-Controller triad: The view registers as an observer on the model. Any changes to the underlying data of the model immediately result in a broadcast change notification, which all associated views receives (in the push back model). Note that the model is not aware of the view or the controller -- it simply broadcasts change notifications to all interested observers. The controller is bound to the view and can react to any user interaction provided by this view. This means that any user actions that are performed on the view will invoke a method in the controller class. The controller is given a reference to the underlying model. MVC and Observer in C++

The MVC Architectural Pattern :: Interactions between Model, View and Controller Once a user interacts with the view, the following actions occur: The view recognizes that a GUI action -- for example, pushing a button or dragging a scroll bar -- has occurred, e.g using a listener method that is registered to be called when such an action occurs. The mechanism varies depending on the technology/library used. In the listener method, the view calls the appropriate method on the controller. The controller translates this signal into an appropriate action in the model, which will in turn possibly be updated in a way appropriate to the user's action. If the model has been altered, it notifies interested observers, such as the view, of the change. In some architectures, the controller may also be responsible for updating the view. Again, technical details may vary according to technology/library used. MVC and Observer in C++

The Observer Pattern :: Definition & Applicability - I Motivation The cases when certain objects need to be informed about the changes occurring in other objects are frequent. To have a good design means to decouple as much as possible and to reduce the dependencies. The Observer Design Pattern can be used whenever a subject has to be observed by one or more observers. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. This pattern is a cornerstone of the Model-View-Controller architectural design, where the Model implements the mechanics of the program, and the Views are implemented as Observers that are as much uncoupled as possible to the Model components. C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 8 8 8 8 8 8 8 8

The Observer Pattern :: Definition & Applicability - II “Model” classes “View” classes C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 9 9 9 9 9 9 9 9 9 9

The Observer Pattern :: Definition & Applicability - III The participants classes in the Observer pattern are: Observable - interface or abstract class defining the operations for attaching and de-attaching observers to the client. In the GOF book this class/interface is known as Subject. ConcreteObservable - concrete Observable class. It maintain the state of the observed object and when a change in its state occurs it notifies the attached Observers. Observer - interface or abstract class defining the operations to be used to notify the Observer object. ConcreteObserverA, ConcreteObserverB - concrete Observer implementations. C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 10 10 10 10 10 10 10 10 10

The Observer Pattern :: Definition & Applicability - IV Behavior The client class instantiates the ConcreteObservable object. Then it instantiate and attaches the concrete observers to it using the methods defined in the Observable interface. Each time the (observable) state of the subject is changing, it notifies all the attached Observers using the methods defined in the Observer interface. When a new Observer is added to the application, all we need to do is to instantiate it in the client class and to add attach it to the Observable object. The classes already created will remain mostly unchanged. C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 11 11 11 11 11 11 11 11 11 11

The Observer Pattern :: Example - I // Subject class, also known as “Observable” // class Subject { public:    virtual ~Subject();    virtual void Attach(Observer* o){ //Attach an observer to the Subject _observers->Insert(_observers->end(), o);}    virtual void Detach(Observer* o){ //Detach an observer from the Subject      _observers->remove(o);}    virtual void Notify(){ //Notify all observers upon state change      ListIterator<Observer*>i(_observers);      for (i.First(); !i.IsDone(); i.Next()) {          i.CurrentItem()->Update(this);} protected:    Subject(); private: //List of all observers plugged onto the Subject     List<Observer*> *_observers; }; C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 12 12 12 12 12 12 12 12 12 12 12

The Observer Pattern :: Example - II // A Sub-class of Subject: a Clock Timer // class ClockTimer : public Subject { public:     ClockTimer();      int GetHour(){return hour};     int GetMinute(){return minute};     int GetSecond(){return second};     void Tick(){      // update internal time-keeping state      // ... // The Observable object notifies all its registered observers      Notify();}; private: int hour; int minute; int second; }; In green are the changes to be applied to the class to be made an observable class. C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 13 13 13 13 13 13 13 13 13 13 13 13

The Observer Pattern :: Example - III //Observer Class // class Observer { public:     virtual ~Observer();     virtual void Update(Subject* theChangeSubject) = 0; protected:     Observer(); }; The Observer class is a virtual class C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 14 14 14 14 14 14 14 14 14 14 14 14 14

The Observer Pattern :: Example - IV // A specific Observer to observe ClockTimers: DigitalClock // class DigitalClock: public Observer { public:     DigitalClock(ClockTimer* s){ //Upon instantiation, attaches itself to a ClockTimer      _subject = s;      _subject->Attach(this);};     ~DigitalClock(){ //Upon destruction, detaches itself from its ClockTimer      _subject->Detach(this);};      void Update(Subject* theChangedSubject){ //if the notification concerns my own subject, redraw my clock’s reading      if(theChangedSubject == _subject)         draw();};     void draw(){      int hour = _subject->GetHour();      int minute = _subject->GetMinute();      int second = _subject->GetSecond();      // draw operation}; private:     ClockTimer *_subject; }; C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 15 15 15 15 15 15 15 15 15 15 15 15 15 15

The Observer Pattern :: Example - V int main(void) {   //Create a ClockTimer to be observed ClockTimer *timer = new ClockTimer; //Create a DigitalClock that is connected to the ClockTimer   DigitalClock *digitalClock = new DigitalClock(timer);      //Advancing the ClockTimer updates the DigitalClock //as Tick() calls Update() after it changed its state timer->Tick();   return 0; } C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns C++ Design Patterns 16 16 16 16 16 16 16 16 16 16 16 16 16 16

Resources [1] Christopher Alexander, Sara Ishikawa, Murray Silverstein, Max Jacobson, Ingrid Fiksdahl-King, and Shlomo Angel. A Pattern Language. Oxford University Press, New York, 1977. [2] Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns – Elements of Reusable Object-Oriented Software, Adisson-Wesley, 1995. [3] James W. Cooper, The Design Patterns – Java Companion Elements of Reusable Object-Oriented Software, Adisson-Wesley, 1998. [4] James O. Coplien, Advanced C++ Programming Styles and Idioms, Addison-Wesley, Reading, MA., 1992. [5] www.oodesign.com. Object-oriented design patterns. 2009. [6] Robert Eckstein, Java SE Application Design With MVC, Oracle Technology Network, March 2007. http://www.oracle.com/technetwork/articles/javase/mvc-136693.html MVC and Observer in C++