Question & Answer
Question
How do I implement a Singleton design pattern in IBM Rational Rhapsody?
Cause
This design pattern example is not found in the samples directory provided with IBM Rational Rhapsody
Answer
Singleton Design Pattern
Requirements
1. The class must contain static instance representing the single instance
2. The class must define a public accessor function to allow client access to the instance.
3. The public accessor must implement "Lazy Initialization"; Only initializing the instance when it is needed.
4. The class must not define public access constructors.
Solution
You can model the implementation by using a self-referencing aggregation association. Note that a composition would be invalid here as a self-referencing composition would create a recursive relationship.
The Counter singleton class is not to instantiated directly. It manages it's own "lazy initialization" via the Counter::instance() method by instantiating the static member.
Internal Counter Variable is updated in the Counter Statechart. To begin execution you therefore, use the following call:
Counter::instance()->startBehavior();
This is called in the Main File.
All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
---|
Product Synonym
Rational Rhapsody
Was this topic helpful?
Document Information
Modified date:
27 May 2022
UID
swg21977213