- , .
Singleton
, ,
.
, .
, .
. -
-
(). .
, -
? , -
.
- ,
, , -
,
. .
, :
,
;
-
, -
.
a Singleton - :
- Instance,
. Instance - ,
Smalltalk -
C++;
- -
.
Singleton
Instance.
:
Q .
Singleton ,
, ;
. - -
.
, -
;
. Singleton -
, -
. -
, ;
. -
Singleton. -
, .
|
|
, Singleton;
, . -
- , -
- C++ Smalltalk.
,
. , -
C++ , -
.
:
. ,
, , - -
, .
, , (
- ),
. -
, , -
.
, -
.
C++ --
Instance Singleton.
Singleton Jill
- instance,
.
Singleton :
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* instance;
};
:
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance () {
if (_instance == 0) {
_instance = new Singleton;
}
return instance;
}
-
Instance. instance , -
- Instance ,
, 0.
Instance : -
.
, . , -
Singleton , -
. ,
.
, instance - Singleton,
- Instance
.
≪ ≫.
C++ . -
,
. :
- ,
;
- -
.
, ,
;
- C++
|
|
[ES90]. ,
.
, .
( ) /
, , ,
. - .
Smalltalk , ,
Singleton. -
, new.
( Solelnstance - ,
):
new
self error: ' 1
default
Solelnstance isNil ifTrue: [Solelnstance:= super new].
^ Solelnstance
Singleton. , -
, , ,
. , ,
,
. - , -
Instance Singleton. -
≪ ≫ , -
.
Singleton - -
Instance (, MazeFactory) -
. C++ -
( ,
), --
.
, -
. -
,
Singleton ≪-
≫ . ,
.
.
Singleton
Instance,
.
. Instance
, . -
, , , -
. Instance ≪≫
Singleton
Singleton.
Singleton , :
class Singleton {
public:
static void Register(const char* name, Singleton*);
static Singleton* Instance ().;
protected:
static Singleton* Lookup(const char* name);
private:
static Singleton* instance;
static List<NameSingletonPair>* registry;
Register Singleton -
. , -
NameSingletonPair.
. Lookup . ,
:
Singleton* Singleton::Instance () {
if (instance == 0) {
const char* singletonName = getenv("SINGLETON");
//
//
_instance = Lookup(singletonName);
// Lookup 0,
return instance;
Singleton ? -
- . , MySingleton
|
|
:
MySingleton::MySingleton() {
Singleton::Register("MySingleton", this);
}
, , -
, ,
! C++ , -
My Single ton. ,
static MySingleton theSingleton;
, MySingleton.
Singleton .
-
. ,
, - : -
Singleton, -
.
, MazeFactory -
, . 99. MazeFactory
. -
, , -
BombedWall, Wall.
, Maze
,
. - .
MazeFactory , -
, , .
,
MazeFactory. ( .) C++
, , MazeFactory
Instance _instance, -
. -
, , -
:
class MazeFactory {
public:
static MazeFactory* Instance();
//
protected:
MazeFactory();
private:
static MazeFactory* instance;
};
:
MazeFactory* MazeFactory::_instance = 0;
MazeFactory* MazeFactory::Instance 0 {
if (_instance == 0) {
_instance = new MazeFactory;
}
return _instance;
}
, , MazeFac tory -
, . -
, ,
Singleton
MazeFactory .
Instance, -
MazeFactory:
MazeFactory* MazeFactory::Instance () {
if (_instance == 0) {
const char* mazeStyle = getenv("MAZESTYLE");
if (strcmp(mazeStyle, "bombed") == 0) {
.instance = new BombedMazeFactory;
} else if (strcmp(mazeStyle, "enchanted") == 0) {
_instance = new EnchantedMazeFactory;
//...
} else { //
_instance = new MazeFactory;
}
}
return _instance;
}
, Instance
MazeFactory. , ,
, , , -
.
- , -
|
|
≪≫. ,
.
Smalltalk-80 [] -
, ChangeSet. -
.
, .
( , ),
, .
Interviews
[LCI+92] - -
Session () WidgetKit ( ). Session
.
-
. WidgetKit -
. Widget-
Kit:: instance () WidgetKit
, Session. -
Session ≪≫, -
, Session.
. .
, .