Segurança com caráter, ou mais algumas palavras sobre o padrão Singleton

- Como, e ele também é um solteirão? E eu pensei que ele era normal!





- Quantas vezes posso dizer que as palavras singleton, mediator, decorator e mesmo prototype não têm nada a ver com a orientação do código?





(conversa chamando um fumante e chamando um programador normal)





Olá a todos, sou Maxim Kravets , CEO da equipe de engenharia web da Holyweb, adeptos de JavaScript. E hoje eu quero falar sobre padrões de projeto.





Vamos dar uma olhada em um pequeno trecho de código:





@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





Existe algo a acrescentar? Deixe seus comentários! Adicionaremos os mais interessantes ao artigo para torná-lo melhor.








All Articles