Let's say we have a class SCORE. It has three objects s1,s2 and s3. SCORE has an attribute RUNS. How to add the runs of all the objects ? SCORE has an internal method int TOTALSCORE(). so when that method is called, it should return the total score .
How should i call that method ? Like s1.TOTALSCORE() ? Or any other way?
Here is a small Proof-Of-Concept:
}
The idea is not to allow creating instances outside your factory method #getInstance(). The instances are tracked using WeakReferences to allow their Garbage Collection. The list is update by a service thread waiting on the reference queue. Hope this helps.
In rare cases the thing that you want could be reasonably, but normally the class is not aware of all it's elements. A total Score is for a Collection of Score elements, maybe a List or a Set.
So you would do:
And outside you would have
Hope that helps!