Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dm-docs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hammonds
dm-docs
Commits
2952ab6d
Commit
2952ab6d
authored
9 years ago
by
sveseli
Browse files
Options
Downloads
Patches
Plain Diff
reordered initialization call, added some test code
parent
9c172fe3
No related branches found
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/python/dm/common/utility/singleton.py
+25
-4
25 additions, 4 deletions
src/python/dm/common/utility/singleton.py
with
25 additions
and
4 deletions
src/python/dm/common/utility/singleton.py
+
25
−
4
View file @
2952ab6d
...
...
@@ -4,12 +4,16 @@ class Singleton(object):
__instance
=
None
# This class will behave properly as long as getInstance() is called.
# If object is constructed using constructor, __init__() will be called
# multiple times in the derived class (i.e., one needs protection
# against multiple initializations in the derived class)
def
__new__
(
cls
,
*
args
,
**
kwargs
):
# Allow subclasses to create their own instances.
if
cls
.
__instance
is
None
or
cls
!=
type
(
cls
.
__instance
):
instance
=
object
.
__new__
(
cls
,
*
args
,
**
kwargs
)
cls
.
__instance
=
instance
instance
.
__init__
(
*
args
,
**
kwargs
)
cls
.
__instance
=
instance
return
cls
.
__instance
@classmethod
...
...
@@ -18,12 +22,13 @@ class Singleton(object):
def
__init__
(
self
,
*
args
,
**
kwargs
):
# Only initialize once.
if
Singleton
.
__instance
is
not
None
:
if
self
.
__class__
.
__instance
is
not
None
:
return
####################################################################
# Testing
if
__name__
==
'
__main__
'
:
print
'
Testing Singleton class
'
s1
=
Singleton
.
getInstance
()
s2
=
Singleton
()
s3
=
Singleton
.
getInstance
()
...
...
@@ -33,8 +38,11 @@ if __name__ == '__main__':
print
'
S3:
'
,
s3
print
'
S4:
'
,
s4
class
A
(
Singleton
):
__instance
=
None
def
__init__
(
self
,
x
):
self
.
x
=
x
if
self
.
__class__
.
__instance
is
None
:
print
'
in A.__init__()
'
self
.
x
=
x
class
B
(
Singleton
):
def
__init__
(
self
,
x
):
self
.
x
=
x
...
...
@@ -42,18 +50,31 @@ if __name__ == '__main__':
def
__init__
(
self
):
self
.
x
=
14
print
print
'
Testing Class A
'
print
'
Init A(3)
'
a1
=
A
(
3
)
print
'
Init A(4)
'
a2
=
A
(
4
)
print
'
A.getInstance()
'
a3
=
A
.
getInstance
()
print
'
A.getInstance()
'
a4
=
A
.
getInstance
()
print
a1
print
a2
print
a2
.
x
,
a1
.
x
print
a3
print
a3
.
x
,
a2
.
x
,
a1
.
x
print
print
'
Testing Class B
'
b1
=
B
(
6
)
b2
=
B
(
5
)
print
b1
print
b2
print
b2
.
x
,
b1
.
x
print
print
'
Testing Class C
'
c1
=
C
()
c2
=
C
()
print
c1
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment