Singleton Design Pattern
#1
Posted 25 November 2009 - 07:08 PM
Any ideas??
#2
Posted 25 November 2009 - 08:29 PM
I'm a GDS fan (no wounder
Then I just select a singelton class type, it's so easy :-)
..one advantage I like about this template (or any GDS class templates) is that if there is a new version of the class template I've used, the GDS toolkit can automatically update the class to the new template.
Cheers,
Mikael
#3
Posted 26 November 2009 - 04:35 AM
#4
Posted 26 November 2009 - 09:22 AM
Singleton or not…
I often use Singleton classes, it might be because I’m lazy wiring the class wire through all my application.
Here is a brief description of why I use singletons.
This is how my standard implementation of my manufacturing rigs looks like.
I use a top level VI, and some singleton classes and a bunch of instrument driver classes.
Let’s look at the current rig I’m working on.
This rig should:
1. Place, align and glue 3 optical components on the DUT.
2. Every component is held by a motorized stage with 4-5 axis.
3. Every stage has about 7-10 additional digital IOs
4. There is an over all vision system with 2 cameras with different lighting controls.
5. There is an additional optical measurement system I’m using for alignment.
In total it’s about 50 IOs in the system from simple position sensors, pneumatic values to 13 motorized stages, cameras, glue dispense systems and other more advanced measurement system.
So where should I start when implementing a large system like this?
I start by looking at the system from an operator’s perspective, what can I see?
I see 3 separate motor stages.
I see a camera and lighting arm, holding 2 cameras and some lamps.
So I decide to create 3 classes, one for every motor stage, this is because they look completely different and hold different components.
I name the classes after what component they are holding.
So one stage will be called “Mirror”-class, and I choose to use a singleton class here.
So why do I use a singleton class?
I know that I will only use one of these objects in my application, but the main reason is that I don’t want to clutter the block diagram of my Main VI, that is going to use this object ;-)
This “Mirror”-class will aggregate all instrument objects it needs, e.g. 4 motion control motors.
This class will encapsulate all functions I might need to perform on the Mirror component.
I will have a public method (a method that my main VI will call) named “Pick up and place mirror at initial location”, and also one called “Cure Mirror Glue”.
These methods will be a bit abstract and I let the method figure out how to perform these actions.
That way I get a good abstraction level, and it’s easy to follow the application flow in my Main VI.
I never allow my main VI to access the instruments direct, so I’m forced to go through an abstraction layer.
By using this approach, I can break down the complex system into smaller solvable parts, in my case I use classes.
And for this project I use only singleton classes in my application component layer that abstracts the driver layer from the main VI.
Cheers,
Mikael
#5
Posted 10 December 2009 - 02:29 AM
Mark Yedinak - Certified LabVIEW Architect and LabVIEW Champion
"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
#6
Posted 11 December 2009 - 03:50 PM
Good software is an investment. Bad software is an expense. Choose wisely.
Dak's First Law of Problem Solving: If the solution looks simple, I don't know enough about the problem.
There are two secrets to success:
Secret #1 - Never tell everything you know.
#7
Posted 15 December 2009 - 06:27 PM
I have used singleton objects when the object will have global affect based on changes from any location in the program. Offhand I can think of logging, error management, security, resource interface/daemon and configuration settings as times I have or might use a singleton object.
"If this was easy our kids would be doing it." - Coworker
#8
Posted 20 December 2009 - 03:40 PM
Tim_S, on 15 December 2009 - 06:27 PM, said:
#9
Posted 20 December 2009 - 04:32 PM
Felix
#10
Posted 20 December 2009 - 04:48 PM
#11
Posted 20 December 2009 - 04:53 PM
Black Pearl, on 20 December 2009 - 04:32 PM, said:
#12
Posted 20 December 2009 - 05:05 PM
Aristos Queue, on 20 December 2009 - 03:40 PM, said:
I read his description as an internal implementation of a singleton object. As long as any class wire accesses the same chunk 'o data it is essentially a singleton regardless of the internal implementation. The two copies on the queue just provide a convenient way for him to cancel a lock without having to requeue the data, as you would have to do with a single element queue.
Black Pearl, on 20 December 2009 - 04:32 PM, said:
I searched... no joy.
Aristos Queue, on 20 December 2009 - 04:53 PM, said:
Sleep? More like a medieval torture device.
Good software is an investment. Bad software is an expense. Choose wisely.
Dak's First Law of Problem Solving: If the solution looks simple, I don't know enough about the problem.
There are two secrets to success:
Secret #1 - Never tell everything you know.
#13
Posted 20 December 2009 - 06:18 PM
http://de.wikipedia....rzeugungsmuster
In the artcle about Singleton, another variant is mentioned: the Multiton.
Another ressource is the following implementation of the Fewtone (german text, but implementation is java/english)
http://www.junit-buc...doc/fewton.html
I also found some real examples why you want to have a Fewton. If you have a limited ressource available (network bandwidth), you want to limit the number of processes (number of downloads, torrents ...) that access these, but to a number >1.
Felix
#14
Posted 21 December 2009 - 08:11 AM
tjs157, on 25 November 2009 - 07:08 PM, said:
All right... here's an object I created (LV 2009) to help test out my Interface Framework and make sure it can handle various ways data can be stored. It implements the following techniques to achieve different kinds of data persistence:
ByVal Data - Plain old vanilla Labview
ByRef Data - Data Value Reference
ByRef Data - Unnamed Queue
Static Data - Global Variable
Static Data - Named Queue
Static Data - Functional Global
You can look to see how I implemented them. Beware: I didn't bother to include any locking behavior as I don't need it for what I'm doing. Race conditions will likely pop up all over the place if you use these exact implementations. I'll happily answer any questions, but if you shoot yourself in the foot you can't say you weren't warned.
Did I mention I really like vanilla?
Black Pearl, on 20 December 2009 - 06:18 PM, said:
I can see why you'd want a limited number of certain kinds of objects. Is this limitation something they try to build directly into the class? If so, that seems like an overly-complex way to go about it. Much easier to just create a regular old by-val object to manage a single process and then create a distributor object that has an array of n process objects and sends them off to clients that request them.
Attached File(s)
-
Numeric Test Object.zip (183.44K)
Number of downloads: 131
Good software is an investment. Bad software is an expense. Choose wisely.
Dak's First Law of Problem Solving: If the solution looks simple, I don't know enough about the problem.
There are two secrets to success:
Secret #1 - Never tell everything you know.
#15
Posted 21 December 2009 - 09:12 AM
If I were to create a Fewton'O'Eight, I also would consider a queue, create 8 objects and store them in the queue. Whenever a request is received, dequeue one of these objects. Whenever a object is returned, enqueue it again. I think this approach is simpler than the array (because the objects are not ordered).
Felix
#16
Posted 21 December 2009 - 05:41 PM
Black Pearl, on 21 December 2009 - 09:12 AM, said:
I know... apparently their mommas never taught them it's not polite to point.
Black Pearl, on 21 December 2009 - 09:12 AM, said:
Yep, you're right.
One potential downside of using a distributor class is that it's relies on cooperative clients that send the object back to the distributor when they are done with it. If a client hangs or forgets to return it you end up losing at least one, and potentially more, of your process objects. If you have a large application with many clients requesting process objects, forgetting to return one of them would show up as slowly decreasing performance over time as the application runs.
I've been wondering if there's a good way to prevent that from happening. I can't think of any way for the distributor object to forcefully recall an object without relying on client-side implementation. If the process objects are active objects you could implement a self-destruct mechanism that operates either from an internal watchdog timer or on a message from the distributor. Once the object has been destroyed the distributor could create a new process object and put it on the internal queue. I haven't prototyped this so I don't know how messy it would get or if it's even feasible.
I'm not sure how well a self-destruct message would work with vanilla objects. In principle you wouldn't want to create a replacement process object until you've confirmed that the old process object was destroyed, but the client controls the object's execution and it's possible that object will never execute again.
Black Pearl, on 21 December 2009 - 09:12 AM, said:
My point was that whereas a singleton is something that is built into the class itself, a fewton that is implemented using a distributor is an artifact of the way the objects are being managed. It could be a useful term to describe the programmer's intent, but I believe it is incorrect to describe a class as a fewton. (Unless, of course, the class itself limited the number of objects that could be created. I have no idea what that would look like in Labview though.)
Good software is an investment. Bad software is an expense. Choose wisely.
Dak's First Law of Problem Solving: If the solution looks simple, I don't know enough about the problem.
There are two secrets to success:
Secret #1 - Never tell everything you know.
#17
Posted 21 December 2009 - 05:58 PM
tjs157, on 25 November 2009 - 07:08 PM, said:
Incidentally, the singleton example that ships with Labview is a good example of a distributor that manages n vanilla byref objects for clients. The examples I posted are ways to build singleton behavior into a class itself. In general following the distributor pattern provides more flexibility; you can always make a byref object into a singleton but you can't make a singleton into a byref object.
Good software is an investment. Bad software is an expense. Choose wisely.
Dak's First Law of Problem Solving: If the solution looks simple, I don't know enough about the problem.
There are two secrets to success:
Secret #1 - Never tell everything you know.
#18
Posted 21 December 2009 - 10:34 PM

POPULAR
AQSingleton.zip (42.97K)
Number of downloads: 284
No wrapping library. No replication of VIs caused by having public wrappers and private implementations. No excessive copy overhead. And, yes, thread safety for parallel public calls.
This demo class implements three public functions: Set Path, Append Path (which does a read-modify-write) and Get Path.
Note that there is one minor issue with this implementation *if* (and only if) you have multiple top-level VIs all using the same central storage and the one that accesses the global first quits before the others are ready. The workaround solutions for that are ... problematic. It is in the set of "holes in the LV language" that we've been slowly patching o'er these many years.
- Norm Kirchner and Daklu like this
- Like This
#19
Posted 22 December 2009 - 01:12 PM
Aristos Queue, on 21 December 2009 - 10:34 PM, said:
Very nice.
Is this going to be included as a shipped example in 2010?
(I'm curious, is that implementation something you've been sitting on for a while waiting to see if the community would 'discover' it or did you actually have to sit down and think about it for a while?)
Good software is an investment. Bad software is an expense. Choose wisely.
Dak's First Law of Problem Solving: If the solution looks simple, I don't know enough about the problem.
There are two secrets to success:
Secret #1 - Never tell everything you know.
#20
Posted 22 December 2009 - 02:07 PM
Daklu, on 22 December 2009 - 01:12 PM, said:














