Search code examples
classobjectstateumlstate-machine

What is an object in state machine diagram?


I'm trying to draw state machine diagram for my android app. Many web sites and books said that it shows transitions between various objects. Because of lack of UML knowledge, however, I can't understand the exact meaning of an object.

In https://www.uml-diagrams.org/object.html, Object is an instance of a class.

So does an object mean instance of the specific class in the code or an entire system? I'm confused about this. Can you give me an example about an obejct?


Solution

  • Objects in UML

    An object is defined in UML as follows:

    A classifier describes a set of objects. An object is an individual with a state and relationships to other objects. The state of an object identifies the values for that object of properties of the classifier of the object.

    But this is difficult to grasp: the definition of a classifier relies on the concept of object, and the definition of object relies on the concept of classifiers. The shorter "objects are instances of classes" do not help to sort this chicken and egg problem out.

    To make it even harder, this way of seing objects fits well class based programming, but less the prototype-based programming.

    Objects more pragmatically

    I will not try to explain what has already been explained in hundreds of books. Let's take Grady Booch's definition (he is one of the UML founder):

    Object: Something you can do things to. An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class. The terms instance and object are interchangeable.

    Even better, GoF's very naive but nonetheless very helpful definition :

    An object packages both data and procedure that operates on that data.

    States

    "State" is an ambiguous term, and may not refer to the same reality when used in "object state", and "state of a state machine".

    For objects, the state often refers to all the data of an object. If you save and restore all the data of an object, you would expect that it its operations will lead to the same results and the same behaviors. This is often true, but not always. A more accurate definition from Booch is:

    The cumulative results of the behavior of an object; one of the possible conditions in which an object may exist (...)

    A state of a state machine has a slightly different meaning. A state correspond not to a single object state but to a set of possible object states (of a single object, or of several objets, or of a whole system). Usually a state-machine state describes in very broad terms the underlying set. A state could for example describe a stage in the lifecycle or operations (created, initialized, in use, archived, discarded) of an object or a component.

    Example for a state machine controlling a room's temperature:

    Named state (State machine)   Object state
    TOO_COLD                      temp=12, min=16, max=19, power=1500
                                  temp=13, min=16, max=19, power=1500
                                  temp=15, min=16, max=19, power=900
    RIGHT_TEMPERATURE             temp=16, min=16, max=19, power=100
                                  temp=18, min=16, max=19, power=50                   
                                  temp=19, min=16, max=19, power=50   
    TOO_WARM                      temp=20, min=16, max=19, power=50
                                  temp=21, min=16, max=19, power=0
                                  temp=20, min=16, max=19, power=0
                
    

    In this example, you can see that the state machine state can (and mostly will) correspond to a (potentially large) set of object states.