uranther

uranther

The SAM (State - Action - Model) pattern - General Discussion, Blog Posts, Wiki

SAM is a new reactive/functional pattern that simplifies Front-End architectures by clearly separating the business logic from the view and, in particular, strictly decoupling back-end APIs from the Front-End. SAM is technology independent and as such can be used to build Web Apps or Native Apps. It is also protocol independent and can be implemented over HTTP, WebSockets…

Creator: @jdubray
Gitter chat: https://gitter.im/jdubray/sam

SAM is a pattern that aims at replacing the MVC pattern. If you are already familiar with MVC, you should be able to make an easy transition to SAM.

There are two main differences between MVC and SAM. First, SAM is reactive, while MVC is generally implemented in an interactive way. Some attempts have been made at implementing it in a reactive way, however, SAM is not just a reactive implementation of MVC. Second, SAM introduces the concept of State as a mechanism to further decouple the View from the Model and treats the view merely as a “State Representation”.

Articles

Examples

  • sam-tic-tac-toe
  • SAM Lambda — SAM Lambda is a boiler plate project that implements the SAM pattern in AWS Lambda using DynamoDB to manage application/session state

Temporal logic of actions (TLA)

Articles

  • Temporal logic of actions (TLA) is a logic developed by Leslie Lamport, which combines temporal logic with a logic of actions. It is used to describe behaviours of concurrent systems.

  • TLA+

    … is a formal specification language developed by Leslie Lamport. It is used to design, model, document, and verify concurrent systems. TLA+ has been described as exhaustively-testable pseudocode and blueprints for software systems.

    TLA+ is based on the idea that the best way to describe things formally is with simple mathematics, and that a specification language should contain as little as possible beyond what is needed to write simple mathematics precisely. TLA+ is especially well suited for writing high-level specifications of concurrent and distributed systems.

  • Use of Formal Methods at Amazon Web Services

  • Why We Should Build Software Like We Build Houses

INDIRECT

Presentations

Academic Papers

See The Writings of Leslie Lamport for a full list of publications from this researcher.

“Programming languages provide no well-defined notion of a program step”

“State machines provide a framework for much of computer science. They
can be described and manipulated with ordinary, everyday mathematics”

Books

Related architectures

First 10 of 48 Posts! Switch mode

gregvaughn

gregvaughn

Interesting. This is the first time I heard it called SAM. I first heard about it as the Reactor model, or even “The Elm Architecture”

StefanHoutzager

StefanHoutzager

Because Dubray has some critique on elm and says that an alignment with SAM would make it stronger I posted a question in an elm discussiongroup: Redirecting to Google Groups

jdubray

jdubray

What’s missing in modern frameworks like Elm is the notion of a “Step” you just cannot arbitrarily process events, that is the problem. SAM suggests that a “step” (in response to an event) is composed of three phases: propose, accept and learn. When Elm issues a series of tasks to run some effects, it misses the essence of state mutation.

SAM is based on TLA+ semantics which introduce a new way to look at state machines, a lot closer to the way we write every day code (as opposed to traditional semantics like tuples (S0,A,S1). TLA+ provides enough structure without the burden of traditional state machines.

Happy to continue the discussion if you’d like.

rvirding

rvirding

Creator of Erlang

Shouldn’t the model and the state be one? Which states you have and transitions between them is completely defined by the model so splitting them seems strange.

But I may have misinterpreted the meaning of state and model.

Robert

StefanHoutzager

StefanHoutzager

Mr. Dubray, I would love to get some more explanation about your graphql critique here: Api for an SPA - #4 by benwilson512.
Thanks for joining the discusion (on the elm forum also)!

jdubray

jdubray

I’d prefer if you call me JJ.

Disclaimer, I was one of the original author of the SDO specification (Service Data Object- http://www.oracle.com/technetwork/testcontent/sdo-specification-java-v2-131073.pdf). I also worked for companies like Object Design (Object and XML Database) and built some solutions with products like MetaMatrix (now part of JBoss)

So, I am certain that GraphQL can solve a class of problems. I am sure it does wonders to Facebook because they can align their entire back-end to play nicely with GraphQL but this is not true of everyone. Stitching a complex back-end to GraphQL will always be the problem like it was for SDO or MetaMatrix. The other problem is more fundamental, data is relational and not “navigational”, no matter how you look at it and as soon as you start distributing it, you’ll run into performance issues each time you’ll need to perform joins across system boundaries. There is simply no way around it.

So again, I am certain it works well for Facebook, but it will fail entirely in the enterprise. You just can’t fight the relationality of data.

mkunikow

mkunikow

Ok I agree that GraphQL not always be prefect match. But what about the case you can have full control of backed and you can easy write back end as GraphQL?

Another solution for complex backed is BFF.

Anyway developers are moving away to handle different endpoint REST services in client and prefer to have some simplify/more optimized solutions. Imagine if you have 20 different REST endpoints you need to handle in mobile app. This is nightmare.

For me interesting alternatives to REST:

  • GraphQL/Relay
  • pouchdb <->CouchDB (client read/writes to local database → automatic populated to server database)
  • https://www.firebase.com/ (real time database)
jdubray

jdubray

David Fall has created a SAM sample using FireBase, it’s really good. We are currently discussing it on Gitter

https://github.com/509dave16/sam-tic-tac-toe

gitter.im/jdubray/sam

jdubray

jdubray

Yes, that was exactly the problem I was trying to solve with SAM. This problems comes mainly from templates and databinding (1 API per screen/view)

That being said, weaving API calls in React is a real nightmare, a complete after thought in their programming model.

With SAM, APIs are hidden behind the model and the actions, so that you can use coarse grained APIs, up to direct persistence of the model in a document oriented database. I wrote an AWS Lambda / DynamoDB sample if you want to take a look.

mkunikow

mkunikow

Sure I will check with pleasure :slight_smile:

Last Post!

jdubray

jdubray

Well at some point you have to mutate something, making the application state immutable is what buy you nothing in terms of managing mutability. Let’s compare:

var m = new Model();
m.accept(proposal).then(access)

with:
access(m = accept(m, proposal))

what’s the difference? a big malloc?

If X is a problem f(X) is rarely a solution, nor X$, not to mention, new X(). You probably need to dig a bit deeper than putting a big function box on some bad code to make it behave properly.

If you spend some time looking at TLA+ / Paxos and make an attempt to make mutation a first class citizen of your programming model, perhaps you’ll understand what I mean, otherwise, it looks like we’ll have to agree to disagree.

That being said, and quite strangely to be frank, Erik never talks about TLA+… I saw him mentioning it once in one of his talks telling the audience that he’ll have to learn about it, but he didn’t elaborate in any way.

Where Next?

Trending in Wikis Top

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement