سؤال

I often hear the term "Stateless" and "Immutable". For example, HTTP is a stateless protocol, and a String object is an immutable object. But I have a hard time grasping the difference between the two. When I create a stateless object, it doesn't store any "state" data internally. And if I create an Immutable object, it mean its will never change.

Doesn't that mean the same thing?

Since the immutable object doesn't change, by definition it cannot have a state. It is what it is forever. And if an object doesn't have state, it cannot be mutated (by definition). Thus, aren't all stateless object immutable and immutable object stateless?

What could be an example of mutable stateless object or immutable stateful object?

هل كانت مفيدة؟

المحلول

No. They do not mean the same thing.

An Immutable Object can never change. That doesn't mean the data contained in that Object can't indicate State. It just means that if you want to represent a change in State, you need to create a new Object.

Stateless means that there is no state.

نصائح أخرى

The context is important and there are two unrelated concepts here.

"HTTP is a stateless protocol" means that each request has no implicit knowledge of other requests, including any previous request sent by the same client. This is different from a protocol like FTP or SMTP where a connection is established and then different commands are sent - each command/request is "associated" to the same client/connection. Of course state/tracking is added back via. cookies and tracking URIs, and even pipelining - but the point is that each request is "new" and "separate" in the HTTP protocol.

"String is an immutable object" means that that particular object's data will always be identical to what it is now in every observable manner (this also implies that observable attributes can't be changed). Some purists might argue that it has deeper implications than this, but in practicality is is about observable attributes - the issue becomes more complicated when immutable objects can "contain" mutable objects.

(And yes, by technicality, an object with no allowed data - or state - can't be updated and is thus "immutable". However, once again, context is important and it's odd to talk about Fangs on an Elephant or a Trunk on a Tiger.)

Edit: Difference between the two

An object that has no state is stateless. All stateless objects are immutable (because there is nothing to mutate); this is a tautological technicallity. An object can have state and still be immutable - however, an object with state (immutable or otherwise) can no longer be considered stateless. As per comment on the linked answer: "[an immutable object] has exactly one state", the initial state.

— from my comment

I'd say it is the same thing in some contexts, but we focus on a slightly different aspects.

When people say that "stateless" means it has no state, it makes me laugh. Of course it has some state from some point of view, e.g. stateless service can be backed by complex graph of objects (dependency injection). The thing is network protocol has slightly different meaning of the "state": it is something that depends on previous request/response. But immutable service doesn't depend on previous calls too, by definition.

"Stateless" doesn't always related to HTTP-protocol, the same term we can use to argue setters in service objects in your shiny OOP code. And here you can see that these two terms are same in fact: immutable service is a stateless service and vice versa.

However, it's awkward for me to call a value object "stateless object". It sounds terrible.

Recap: in case of services (network or OOP, doesn't matter) I'd say these terms are interchangeable.

Just example:

interface Logger
{
    public function logWarning(string $message);
    public function logError(string $message);
}

It doesn't matter how many times we called logWarning or logError and order of calls doesn't matter too. Thus, we can call it "stateless service".

But this service is also has no setters and any mutators like changeFileName() -> it is an immutable service/object.

Mutability makes object stateful. Stateful makes object mutable. These terms are interdependent in context of services.

They are definitely not the same.

Immutable objects are the never changed. The state of immutable objects is never modified, aliasing immutable objects is harmless and no alias control is necessary for immutable objects, although alias control may be needed to prove that objects are in fact immutable.

And stateless means there is no state. HTTP is called a stateless protocol because each command is executed independently, without any knowledge of the commands that came before it. It is based on a request paradigm. In this protocol the communication generally takes place over a TCP/IP protocol.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top