OCLOperators oclSingleton
No edit summary
(Automatically adding template at the end of the page.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
There is an operator in OCL you may not know about yet: oclSingleton.
There is an operator in OCL you may not know about yet: oclSingleton.


Use it like this: Class1.oclSingleton <and follow with operations or navigations on that single instance>
''Use it like this:'' Class1.oclSingleton <and follow with operations or navigations on that single instance>


If Class1 does not have an instance at the time of executing the oclSingleton, one is created.
If Class1 does not have an instance at the time of executing the oclSingleton, one is created.


For you to use this, set the taggedvalue on Class1 “Eco.IsSingleton” to “true”.
To use this, set the taggedvalue on Class1 “Eco.IsSingleton” to “true”.


It is a very practical design pattern to have a modeled Singleton class – this removes the need for other global variables – like logged-in user, etc. Since the Singleton class can have code-derived attributes and associations, you can pick up important info from the running environment and make it available in the model context. Try it – it will make your code less hairy.
It is a very practical design pattern to have a modeled Singleton class – this removes the need for other global variables – like logged-in user, etc. Since the Singleton class can have code-derived attributes and associations, you can pick up important info from the running environment and make it available in the model context. Try it – it will make your code less hairy.
Line 11: Line 11:
oclSingleton is an operator that helps you implement the Singleton pattern (the pattern states that there is exactly one instance of this class - never more, never less).
oclSingleton is an operator that helps you implement the Singleton pattern (the pattern states that there is exactly one instance of this class - never more, never less).


If you mark a class with IsSingleton=true you can use this oclSingleton operator to get access to the only object instance of this class.
If you mark a class with IsSingleton=true, you can use this oclSingleton operator to get access to the only object instance of this class.


Example:
=== '''Example:''' ===
'''Class:''' SysSingleton


Class: SysSingleton
'''Common expression:''' SysSingleton.oclSingleton.CurrentUser.IsAdmin  -- this expression looks up the only SysSingleton instance that acts as a Global variable that points out the CurrentUser.
 
Common expression: SysSingleton.oclSingleton.CurrentUser.IsAdmin  -- this expression looks up the only SysSingleton instance that acts as a Global variable that points out the CurrentUser.
[[File:2017-08-07 21h31 37.png|none|thumb]]
[[File:2017-08-07 21h31 37.png|none|thumb]]
[[Category:OCLOperators]]
[[Category:OCLOperators]]
{{Edited|July|12|2024}}

Latest revision as of 15:42, 10 February 2024

There is an operator in OCL you may not know about yet: oclSingleton.

Use it like this: Class1.oclSingleton <and follow with operations or navigations on that single instance>

If Class1 does not have an instance at the time of executing the oclSingleton, one is created.

To use this, set the taggedvalue on Class1 “Eco.IsSingleton” to “true”.

It is a very practical design pattern to have a modeled Singleton class – this removes the need for other global variables – like logged-in user, etc. Since the Singleton class can have code-derived attributes and associations, you can pick up important info from the running environment and make it available in the model context. Try it – it will make your code less hairy.

oclSingleton is an operator that helps you implement the Singleton pattern (the pattern states that there is exactly one instance of this class - never more, never less).

If you mark a class with IsSingleton=true, you can use this oclSingleton operator to get access to the only object instance of this class.

Example:

Class: SysSingleton

Common expression: SysSingleton.oclSingleton.CurrentUser.IsAdmin -- this expression looks up the only SysSingleton instance that acts as a Global variable that points out the CurrentUser.

2017-08-07 21h31 37.png
This page was edited 94 days ago on 02/10/2024. What links here