The SAM (State-Action-Model) pattern in Swift.
The entire pattern can be described with:
which translates to: Actions, triggered by view events (Intents), present a dataset (Proposal) to the Model that accepts or rejects the data and performs the actual mutation of its property values. After the Model mutation, the state representation is created based on the derived (control) state. The View displays this state representation. Finally, the NAP (next action predicate) computes any automatic next actions based on the current app state.
- Single Model Tree
- Unidirectional data flow
- View as a pure function of the model
- Reactive loop
- Reusable Actions
- Concept of a "step" for state mutations
Compared to other approaches such as Redux, Elm etc., SAM comes with a number of key advantages:
The pattern processes events as a computation "step" with a clearly defined flow: propose (Action), accept (Model) and learn (State). Unlike other patterns that promote immutability, mutability is an explicit "first class citizen" in SAM (programming is all about state mutation, not state avoidance through immutability.)
SAM is much simpler than Redux/Elm that require additional modules (Thunks, Effects, Sagas etc.) for typical tasks needed in modern async. applications.
Actions are completely decoupled from the View and Model. The Action does not need to know about the Model (which would require the Action to have the entire knowledge of the Model and application state). Actions are modular and reusable. Unlike in Redux/Elm where Actions are only Intents by default and side effects must be separately handled, SAM Actions can perform async. API calls.
Complete decoupling of business logic: Views have no knowledge of the Model or Actions. Views are functions of the State, which are functions of the Model. State itself is stateless! The state representation is only computed after the Model has been fully mutated.
The programming model can be used both client- and serverside.
More info on the SAM website.
The app folder contains a complete and fully documented example of the SAM pattern. The app basically loads, extracts, and displays animated Gifs from the site "The Coding Love".