Skip to content

Commit 6c03b20

Browse files
complete model-view-controller section, see #88
1 parent ac8d8ca commit 6c03b20

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

doc/phet-software-design-patterns.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ MyTrait.mixInto( MyClass );
488488

489489
## Model-View-Controller (MVC)
490490

491-
Author: 🚧 @jessegreenberg 🚧
491+
Author: @jessegreenberg
492492

493493
Model-View-Controller is a software pattern for applications where the developer separates the implementation into three
494494
distinct categories. Model code is responsible for the application data and logic. View code is responsible for the
@@ -525,24 +525,27 @@ applied to create a simulation screen by a class called Screen.js, which may be
525525
In this example you can see that MyModel is oblivious to the view, while the view has a reference to the model to inform
526526
the representation.
527527

528+
Controller code at PhET is generally in input and event handling which is done through `Node.addInputListener`.
528529
Communication from the model to the view is facilitated by classes in axon such as Property and Emitter.
529530
These are Observables which contain model state information and broadcast changes to the view so that it can update
530531
accordingly.
531532

532-
Model-View separation can also be found at other tiers of PhET's implementation. Scenery (which
533-
is used extensively in simulation view code) is implemented with model-view separation as well. For example,
534-
a scenery Node acts as a model which is responsible for state information such as visibility, transformation, bounds
535-
and other things. Meanwhile, other view code in scenery is responsible for rendering this state information for the
536-
user.
537-
538-
**Questions:**
539-
Does PhET actually use Model-View-**Controller**? I don't think of separated "controller" code. If I were to
540-
draw a diagram of PhET's model-view separation it would be:
533+
The "Model" in this pattern doesn't need to be the "domain" model for a simulation. It can contain any logic
534+
for application behavior for any context. Model-View separation can be found at other tiers of PhET's
535+
implementation. Scenery (which is used extensively in simulation view code) is implemented with model-view separation.
536+
For example, a scenery Node acts as a model which is responsible for state information such as visibility,
537+
transformation, bounds and other things. Meanwhile, other view code in scenery is responsible for rendering this state
538+
information for the user. You will find "model-view" separation in the [sun](github.com/phetsims/sun) button
539+
implementation as well.
540+
541+
There are several variations on the Model-View-Controller pattern. Please see [this article for more information](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller).
542+
PhET's usage is most similar to the Model-View-Delegate pattern used by Java in the implementation of Swing. In this
543+
pattern, the controller code is generally packaged with view code. A diagram of this pattern would look like
541544
```text
542545
+----------------+ Axon +------------------+ +-----------+
543546
| | +-------------> | | | |
544547
| | | | User Input | |
545-
| Model | | View | <------------+ | User |
548+
| Model | | View/Controller | <------------+ | User |
546549
| | <--------------+ | | | |
547550
+----------------+ Input Listeners +------------------+ +-----------+
548551
```

0 commit comments

Comments
 (0)