- Wie, und er ist auch ein Singleton? Und ich dachte er wäre normal!
- Wie oft kann ich Ihnen sagen, dass die Wörter Singleton, Mediator, Decorator und sogar Prototype nichts mit der Ausrichtung des Codes zu tun haben?
(Gespräch mit einem Raucher und einem normalen Programmierer)
Hallo allerseits, ich bin Maxim Kravets , CEO des Holyweb-Web-Engineering-Teams, JavaScript-Experten. Und heute möchte ich über Designmuster sprechen.
Schauen wir uns einen kleinen Code an:
@Injectable()
export class SomeClass implements SomeInterface {
private currentData: Data
setData() {
this.currentData = new Data()
}
, : « decorator SomeClass Dependency Injection, setData Builder new currentData ...»
. , , . , , . — . — , . .
— . , — . , « ». , — , . ( Singleton).
. . ,
Singleton — - , . :
:
,
, :
public static getInstance(): SingletonClassName {
!instance ? instance = new SingletonClassName : instance
}
, ? ! , ! , …
(), ( ) , ( , ). — 100 ().
( ) 100 . , , 100 . , …
, )) , . , () ? ? .
( ) . , - — . ? — ! , (). (), ( ...), (... ).
, , . , .
. .
Singleton — (), , () , .
//
class DataBase {
// Singleton-
private static instance
// ,
// new
private constructor() {
//
….
// ,
this.instance = null
}
//
// , Singleton-
public static getInstance() {
if ( this.instance === null ) {
// ,
this.instance = new DataBase()
}
// ,
return this.instance
}
// query
//
public getQuery(payload) {
//
...
}
}
//
class AppModule {
//
Data() {
//
let foo = DataBase.getInstance()
let bar = DataBase.getInstance()
// foo ,
// bar
foo.getQuery("SELECT ...")
bar.getQuery("SELECT ...")
}
}
, Singleton , , , Singleton , .
Singleton — TypeScript, , .
, Singleton , « » — , . , , , .
Singleton, , , , TTD.
? , ! , , . , (DI), ( Singleton) , .
. . , ?
. Singleton — , . . , , , . Singleton. , :
|
|
. |
. |
. |
. |
. |
. |
, Singleton «» . , Singleton. Singleton.
, , . , , : Telegram @maximkravec.
Gibt es etwas hinzuzufügen? Hinterlasse deine Kommentare! Wir werden die interessantesten zum Artikel hinzufügen, um ihn besser zu machen.