- How, and he is also a singleton? And I thought he was normal!
- How many times can I tell you that the words singleton, mediator, decorator and even prototype have nothing to do with the orientation of the code?
(conversation calling a smoker and calling a normal programmer)
Hello everyone, I'm Maxim Kravets , CEO of the Holyweb web engineering team, JavaScript adepts. And today I want to talk about design patterns.
Let's take a look at a small piece of code:
@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.
Is there something to add? Leave your comments! We will add the most interesting ones to the article to make it better.