A Singleton is useful to express in the engine model configurable information with global visibility through an object whose creation is automatically managed by the system. In the diagram, this type of class is distinguishable by the stereotype
Singleton always corresponds to one instance/record, so it is not possible to declare unique constraints or indexes on attributes of this class.
It is also not possible to set attributes such as [required](/en/guide/modeling/database-schema/attributes/native-attributes/#integrity constraints), since at the first Cloudlet startup none of them is valorized.
Create a Singleton #
Right-click on a free spot in the workspace (Canvas) to open the
Database menu; from the drop-down menu choose
New singleton class. Alternatively, click on the palette button(
Create a new singleton).
Referencing a Singleton #
A Singleton is accessible by all other classes of the engine model, which can reference its attributes in expressions, without having to associate with the class itself. It is not therefore concurred to express relations from other classes towards the Singleton (it is not in fact necessary, given its uniqueness and global visibility); it is instead allowed to express relationships from the Singleton towards other classes of the engine model, including Enum, as long as the minimum cardinality of the target role is zero; for this reason in the
Role menu the options Exactly one (
1) and One or more (
1N) are disabled. Both associations and compositions are allowed, so it is also possible to declare derived attributes of type query on Singleton roles. Associated objects and part objects will also be made available globally as roles of the Singleton instance.
In expressions you can reference the attributes or roles of the Singleton with the following syntax:
<SingletonClassName>.<AttributeName>. In the Expression editor, Singletons are listed in a list accessible from the (
Singleton constants), from which you can select their attributes and roles.
Singleton in the generated application #
Once the Cloudlet is started, the system checks that the Singleton instance exists, and if it does not, it creates it. Initially, the attributes will all have null value, and it is necessary for the user to properly value them.
Since the existence of the Singleton class instance is always guaranteed, the only CRUD services generated are those related to its modification, while no services are generated to create or delete it.
We modeled an application that collects appointment requests at an office. The Singleton class Front_office is used to allow users to define the opening and closing times of the counter when the Cloudlet is first started; this is done by valorizing the attributes
time attributes of the Appointment_request class carry information about the day and time of the requested appointment.
In order to avoid the possibility of creating appointments with invalid times, i.e. times when the counter is closed, we finally created two Class warnings at the application level; the request is created only if the value of
time meets the following conditions
- it is greater than or equal to
time >= Front_office.opening_time)
- is less than
time < Front_office.closing_time)