Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
MVC is actually quite simple. What complicates discussions of it with respect to old Smalltalk systems is that it was tied up into the UI paradigm of the time. You have three actors in typical MVC:
  • the model: the actual object of interest (I would say "business objects" but that's too limiting, of course). These have no idea how they're being watched or controlled, just that they may be. They notify interested Views of potentially interesting changes to themselves.
  • the view: what gets notified when the model says that it's changed
  • the controller: this is what (in Smalltalk MVC systems) translates user input events (mouse/keyboard, etc.) into actions on the model.
Many newer UI systems based on windowing systems merge the view and controller into a single widget. So the C in MVC is mostly not useful. This is why (for instance) the Observer pattern in Design Patterns only has two participants (once you get past the C++ inheritance crap). The important idea is this: the model notifies views when it changes. The views then can update themselves as needed. The easiest implementation of this is to have each model keep a list of views. Upon a change, the model sends a message to each of the views saying that it's changed. Some systems include an indication of what has changed.

package Model; # mix in to model classes that are # implemented as blessed hashes # This is called by a View sub addDependent { my $self = shift; my $dependent = shift; my $dependents = (self->{dependents} ||= []); push @$dependents, $dependent; } # This is called by a View sub removeDependent { my $self = shift; my $dependent = shift; my $dependents = $self->{dependents} || return; @$dependents = grep { $_ != $dependent } @$dependents; } # This is called by the Model # you can pass additional data as needed sub _changed { my $self = shift; my $dependents = $self->{dependents} || return; foreach my $dependent ( @$dependents ) { $dependent->update($self, @_); } } package View; # mix in to observer classes of any kind sub update # stub: do nothing { my ($self, $model, @aspectData) = @_; }
Then this gets used like this (in a Model class):

package MyModel; @MyModel::ISA = qw(SomethingElse Model); sub changeSomething { my $self = shift; my $value = shift; $self->{something} = $value; $self->_changed('something', $value); } package MyView; sub update { my ($self, $model, $aspect, $otherStuff) = @_; # hey! $model has changed its $aspect! } package main; my $view = bless {}, 'MyView'; my $model = bless {}, 'MyModel'; $model->addDependent($view); # now the following # calls $view->update($model, 'something', 123); $model->changeSomething(123);
The passing of the new value is an optimzation, and is not strictly necessary.

update: added more sample code


In reply to Re: Seeking MVC Wisdom by bikeNomad
in thread Seeking MVC Wisdom by coreolyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found