![]() |
Telelogic Rhapsody (steve huntington) | ![]() |
Topic Title: <<singleton>>'s as parts on structure diagrams Topic Summary: Created On: 29-Jun-2005 12:45 Status: Read Only |
Linear : Threading : Single : Branch |
![]() |
![]()
|
![]() |
|||||
How is it possible to use a <<singleton>> object on a structure diagram?
The Singleton design pattern says, that the constructor should be protected, and all access to the object should be via a class operation ("Instance()"), but Rhapsody expects a public constructor to instantiate a part on a structure diagram. If I make the constructor public instead of protected, and sets the static member attribute "uniqueInstance" equal "this" in the constructor, it seems to work, but in that way I feel that I'm violating the basic concept of the Singleton design pattern. A nicer way would be to specify the class operation, "Instance()", as the initialization operation (in the feature dialog for the part), but it is only possible to pick constructors in the combobox, and thats a shame! (Do you I-Logix guys see this?) Has anyone given this a thought, or even better, found a solution? ------------------------- Jesper Gissel Johnson Controls Denmark, Marine Controls |
|||||
![]() |
|||||
![]() |
|||||
Hi Jesper
Normally, the "singleton object" on your structure diagram will cause Rhapsody to define an object of your Singleton class in the package's implementation file, giving you the protected constructor error (as a Singleton is intended to!) To get around this, I've adjusted Rhapsody's code generation to define a pointer to the class instead, and to set that pointer using from the class instance() operation. The result is that the object on the structure diagram doesn't actually exist, and relations to it will be redirected to the actual singleton. To achieve this, create a Singleton stereotype with the following properties, and applied it to the "singleton object": CG::Relation::Implementation = "EmbeddedScalar" OMContainers::EmbeddedScalar::CType = "$(constant)$target$reference" OMContainers::EmbeddedScalar::Get = "($RelationTargetType) $cname=$target::instance()" OMContainers::EmbeddedScalar::RelationTargetType = "$CType" (These container code generation properties are a bit of a hazy area; my choice of "EmbeddedScalar" was purely based on which implementation could be successfully adjusted for the desired effect.) This gives compilable code with a protected constructor in the Singleton class, and the ability to create links to the singleton. I've enclosed a Rhapsody 6.0 example model to demonstrate the approach. Note that I've assumed that the Singleton's instance() operation returns a pointer. Please let me know if that helps! best regards, Simon Morrish Panasonic TV Design Centre UK [email]simon.morrish@eu.panasonic.com[/email] ------------------------- Simon Morrish simon.morrish@eu.panasonic.com http://panasonic.co.uk Panasonic ideas for life Edited: 28-Feb-2008 at 19:38 by Simon Morrish |
|||||
![]() |
|||||
![]() |
|||||
Hi Simon,
Thanks for your answer ![]() When draging the singleton class inside a Composite class, and makeing the Singleton class an object (a part), the code generator places a new statement in the Composite class to instantiate the Singleton part, and thats a problem! I can't make the constructor protected and if it is public, I get a new instance of the Singleton class every time, and thats NOT the meaning. So... Can the code generator be manipulated to use the instance() operation instead of new? Could new be overloaded in the Singleton class and in that way invoke instance(), and how is this done in Rhapsody? If you, or any other have an answer to any of these questions, I would like to know... ------------------------- Jesper Gissel Johnson Controls Denmark, Marine Controls |
|||||
![]() |
|||||
![]() |
|||||
Hi, Its me again
![]() I've modified the singleton pattern so it works for my purposes. What I've done, is: - Made the ctor public. - overloaded the new (and the delete) operator, so new calls instance(). - The unique singleton instance (theInstance) is implemented as a static member attribute. - Made instance() call the global new on its first invocation only. - Made a stereotype (EnbeddabeSingleton) that makes the singleton object a "Scalar" (a pointer), so its being created byRef. The limitaion is, that it doesn't work if the singleton object is created byValue (new is not called). I still need to make the deletion of the singleton object work! (That's what, intentionally, makes the attached model crash after 5 sec.). Any ideas about that issue? Its my first overloading of new and delete, so I might be missing some fundamentals! ------------------------- Jesper Gissel Johnson Controls Denmark, Marine Controls |
|||||
![]() |
|||||
![]() |
|||||
Hi Jesper
This is a reply to your message of 2 July... I've created another example, based on my previous example and using the same "stereotype to change the code generation" approach that shows a singleton object appear as a "part" in two different composite classes. (See enclosed zip file.) Try it and see if it works for you... In general, my approach is to avoid Rhapsody creating an instance of the singleton class, since that defeats the meaning of "Singleton". However, I'm left with a composite class whose structure diagram shows it containing a singleton object, which is at least misleading, if not downright wrong! Your approach seems to be to allow multiple instances of the singleton class to be created, but to override the creation so that each one points back to the "unique" instance. (This can't ever work for instances created statically or on the stack, since there is no pointer-to-a-new-instance to redirect to the unique instance.) This approach also seems to be, at heart, a deception... (No offence intended! ![]() Stepping back for a moment, I'd be interested to know why you want to include a singleton as a part of a composite. Is the composite also a singleton? Is it a way for an object to get access to a singleton's facilities without having an external association to it? Or? yours, interested, Simon PS. I'll have a look at the example you posted tomorrow...
------------------------- Simon Morrish simon.morrish@eu.panasonic.com http://panasonic.co.uk Panasonic ideas for life |
|||||
![]() |
|||||
![]() |
|||||
Hi Simon,
It seems, that in my exploration process, I ended up used "Scalar" and not "EmbeddedScalar" in the Singleton stereotype. This resulted in "new'ing" the part, and that gave me a problem with the protected ctor. If I had only restrained my curiosity, I had not ended up where I did! ![]() My motivation for using a singleton as a part: In my design, I use a Composite Class, "Build", which is the only class that is initalized in the configuration. This build class contains all the subsystems and instantiates all these and all the connecting links. All the subsystems in turn, are likewise Composite classes, that instantiates all sub-subsystem and etc. etc... I have a singleton class/object "Database", which is accessed from a number of client objects via ports. As explained earlier, Rhapsody instantiates all the links in the system, but if I cannot use the "Database" as an object (or part), Rhapsody cannot do that for me! And the instantiation code, that I must write manualy, is not trivial, and it is not where I wish to use my time. It's as simple as that. Do you have any comments on that? To sum it all up: I'll stick to your singleton stereotype, since it covers all my needs, and it seems to be the right way to do it. ![]() Thank you for taking your time helping me out. Regards Jesper Gissel YORK Marine ApS, Controller division [email="JeGissel@YorkRef.com"]JeGissel@YorkRef.com[/email] ------------------------- Jesper Gissel Johnson Controls Denmark, Marine Controls |
|||||
![]() |
|||||
![]() |
|||||
Hi Simon,
Isn't there a problem if the singleton is reactive? It seems that the generated code expects that the singleton is "byValue". It generates statements like: [font=Courier New]itsMySingleton.setShouldDelete(false)[/font], [font=Courier New]itsMySingleton.setActiveContext(theActiveContext, false)[/font], [font=Courier New]itsMySingleton.destroy()[/font] and [font=Courier New]itsMySingleton.startBehavior()[/font]. How do "we" ![]() Regards Jesper Gissel YORK Marine ApS, Controller division [email="JeGissel@YorkRef.com"]JeGissel@YorkRef.com[/email] ------------------------- Jesper Gissel Johnson Controls Denmark, Marine Controls |
|||||
![]() |
|||||
![]() |
|||||
Hi Jesper
This is getting complex, isn't it? Thanks for explaining your motivation. I subscribe to it also; at least I agree with the need to show links to singletons on structure diagrams and have Rhapsody automatically generate the link code. (I do things slightly differently - rather than a top-level composite, I create objects and links in an "implementation" package and include that package in the component scope - no initialisation specified in the configuration.) I my stereotype was too complex a solution, and evidently causes problems by breaking other parts of Rhapsody's code generation. A simpler approach, as it turns out, is this: 1) Change the «Singleton» stereotype to have only the following property: CG::Relation::Implementation = "EmbeddedScalar" 2) Define your own "Singleton *newTheSingleton(void)" operation in your composite class that, rather than trying to instantiate, simply returns Singleton::instance(). (This will override the implementation normally generated by Rhapsody.) 3) Similarly redefine "void deleteTheSingleton(void)" to set the pointer NULL without trying to delete the (only) instance. You can see an example in the enclosed model. This method is not as convenient because it required some hand-coding for each singleton, but it still saves the niggly work of hand-writing instantiation and link creation code! And it may even be possible to automate the creation of these operations using VBA... And anyway, simple is always best...! Do "we" have a solution? ![]() best regards, Simon Morrish Panasonic TV Design Centre UK [email]simon.morrish@eu.panasonic.com[/email]
------------------------- Simon Morrish simon.morrish@eu.panasonic.com http://panasonic.co.uk Panasonic ideas for life |
|||||
![]() |
|||||
![]() |
|||||
:eek: There was an error in my previous message. It should have read:
1) Change the «Singleton» stereotype to have only the following property: CG::Relation::Implementation = "[COLOR=DarkRed]Scalar[/COLOR]" :rolleyes: ------------------------- Simon Morrish simon.morrish@eu.panasonic.com http://panasonic.co.uk Panasonic ideas for life |
|||||
![]() |
|||||
![]() |
|||||
Hi Simon,
Now everyhing seems to be working just fine ![]() I'm working on a VBA macro to do the job. I will post it if you are interested!? I really appreciate your help. Thanks. Best regards Jesper ------------------------- Jesper Gissel Johnson Controls Denmark, Marine Controls |
|||||
![]() |
|||||
![]() |
|||||
You're welcome, Jesper.
It was interesting, and applicable to my work too. It would be great if you could post the VBA... best regards, Simon ------------------------- Simon Morrish simon.morrish@eu.panasonic.com http://panasonic.co.uk Panasonic ideas for life |
|||||
![]() |
|||||
![]() |
|||||
Hi Simon,
Here is the VBA script. /Jesper
------------------------- Jesper Gissel Johnson Controls Denmark, Marine Controls |
|||||
![]() |
FuseTalk Standard Edition v3.2 - © 1999-2009 FuseTalk Inc. All rights reserved.