Skip to content

Commit

Permalink
update doc about new mixin pattern, phetsims/scenery#1340
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Feb 8, 2022
1 parent 31d7931 commit f9c22b7
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion doc/phet-software-design-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,25 @@ or is difficult to use.
### Examples
An example of PhET mixin is phet-core/Poolable. An example of a PhET trait is scenery/Paintable.

The Mixin and Trait pattern was overhauled when PhET converted to use Typescript.

For recent examples, see the following:
* SCENERY/Paintable
* SCENERY/Imageable
* SCENERY/Voicing
* SUN/AccessibleValueHandler

Mixins and traits cannot mutate the constructor signature, and also cannot have private or protected members. This is
due to the implemention which creates an anonymous class that dynamically extends the provided Type. Thus, the constructor
signature should just pass up an `args` param using the spread operator:

```js
constructor( ...args ) {
super( ...args );
}
```

<details><summary>Old, deprecated Mixin pattern</summary>
Creating and using mixins and traits will look similar. Both will have
- A `mixInto` method that is called on the class using the mixin/trait.
- An `initialize{{Name}}` method that will be called in the constructor of the class using the mixin/trait.
Expand All @@ -396,7 +415,6 @@ Creating and using mixins and traits will look similar. Both will have
method of the mixing type.
- The class using the mixin/trait will have `@mixes Name` annotation at the constructor.

The only difference is traits should have assertions in the `mixInto` method to verify the class and requirements.

<details><summary>Trait Example</summary>

Expand Down Expand Up @@ -455,6 +473,7 @@ class MyClass extends SuperClass {
MyTrait.mixInto( MyClass );
```
</details>
</details>

## Model-View-Controller (MVC)

Expand Down

0 comments on commit f9c22b7

Please sign in to comment.