Behavioral design pattern
Download
1 / 20

Behavioral Design Pattern - PowerPoint PPT Presentation


  • 166 Views
  • Updated On :

Behavioral Design Pattern. Tutorial 9. Model-View-Controller. “The holy triad”. The Model-View-Controller pattern (MVC). An extension of the observer pattern to include also elements of control. Very much in use when implementing GUI Most of Swing is implemented using MVC.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Behavioral Design Pattern' - hetal


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Model view controller l.jpg

Model-View-Controller

“The holy triad”


The model view controller pattern mvc l.jpg
The Model-View-Controller pattern (MVC)

  • An extension of the observer pattern to include also elements of control.

  • Very much in use when implementing GUI

    • Most of Swing is implemented using MVC.

    • Common also in other toolkits

  • Origin: SmallTalk-80

    • Was later refined to Morph.

  • Today: the common trade.


Mvc elements l.jpg
MVC elements

  • The model

    • Contains the actual data to be viewed and controlled.

    • Interacts with the application’s logic.

  • The View

    • Displays the model, refreshes when notified.

    • Fetches the data from the model.

  • The controller

    • Changes or activates the model.

    • Might cause, indirectly, a refresh of the view.


Mvc diagram l.jpg
MVC diagram

Controller

View

Fetch data

Control

Notify

Model


Mvc sequence l.jpg
MVC sequence

Controller

invoked

Display changes

Controller

View

Fetch

data

Affect the

model

Notify

Model


Mvc classes and interfaces l.jpg
MVC classes and interfaces

View

Controller

View2Model

+ notify()

0..* views

1 model

model

Model

Model2Control

+ controlThis()

+ modifyThat()

Model2View

+ addView()

+ removeView()

+ getData()


Mvc interfaces 1 l.jpg
MVC interfaces (1)

  • Model to Controller

    • Defines the operations and manipulations exposed to the controller.

    • Derives from the model’s functionality.

  • Model to View

    • Allows attaching and detaching views.

    • Defines the data exposed to the view

      • Many times it will be similar to the one the controller manipulates or affects


Mvc interfaces 2 l.jpg
MVC interfaces (2)

  • View to Model

    • Allows notifying the view that a change occurred.

    • Several options:

      • No parameters – all the data is fetched from the model.

      • Event parameter – relevant data is sent as parameter.

      • Name parameter – the name of the attribute that changed is sent as parameter. Relevant data is then fetched.

        • Used with some Java beans mechanisms.

    • There can be several interfaces or variations, according to the model


Finding mvc interfaces l.jpg
Finding MVC interfaces

  • Model to Controller

    • Model data’s setter

    • Application’s functionality

    • Methods used by actors

  • Model to View

    • Model data’s getters

    • Actor class operations

  • The view itself

    • Usually from the customer’s requirements.


The model and the application l.jpg
The model and the application

  • In the MVC framework, the model is both a part of the GUI’s process and the logic itself.

    • It must notify the view in order to make it work.

    • The application is the model.

  • In real life, we might not have such luxury.

    • It forces the logic to be aware of the view.

  • Alternative: the model acts as a mediator between the GUI and the application.


Mvc and application l.jpg
MVC and application

View

Controller

Model

Application


The command pattern l.jpg

The Command Pattern

OOP closures


The command pattern14 l.jpg
The Command Pattern

  • The problem: how to pass code as a parameter?

  • Some OOP languages support closures directly, so the question does not arise.

    • SmallTalk, Python, CLOS…

  • Partial solution: function pointers.

    • Very common in C++/STL

    • Can be problematic

  • The “clean” OOP solution: command objects.


The command pattern classes l.jpg
The Command Pattern classes

Command

+execute()

Client

Receiver

Usually a button

Or some other

controller

The “env.” to

Perform the

command on.

ConcreteCmd

+execute()


Ac quiring the receiver l.jpg
Acquiring the receiver

  • How does the command acquires its “target”?

    • Constructor parameter

      • Can be limiting

    • Parameter to “execute()”

    • Access some “current” pointer

      • “Application.getCurrentDocument()”

    • Dynamic inner class visibility

      • The receiver is the “this” object of the outer class.

      • A bit like real closures.


Some non gui uses of commands l.jpg
Some non-GUI uses of Commands

  • Undo/redo

  • Send “orders” to recipients

    • Even over the net!

  • Stages in some complex algorithm or protocol

  • Composite commands can be macros.

  • Map strings to commands to get

    • A quick-and-dirty scripting lang.

    • A parser

      • XML parser example


Commands and mvc l.jpg
Commands and MVC

  • Commands can act as intermediate controllers to some complex and large model

    • The model is not necessarily a single class!

  • Instead of exposing a large interface, let a set of commands be used by the controllers.

    • A clean interface is not always used; in this case, it is not clear which methods are for the controllers.

    • Maybe the model can even be hidden!

  • As a rule, set of commands can be a handy alternative to inconvenient interfaces


Mvc with commands l.jpg
MVC with commands

View

Op1

Controller

Op1Cmd

Model

Op2

Controller

Op2Cmd

Op3Cmd

Op3

Controller


Adding gui example l.jpg

Adding GUI example

Using MVC and commands for the outline editor


ad