Comments (5)
I would also suggest considering showing a more traditional OOP singleton implementation, seen in languages such as C++
, which I believe a lot of developers might find easier to understand.
class State {
constructor(protected count = 0){
}
getCount(){
return this.count;
}
}
class ApplicationState {
static state;
static get(){
if(!ApplicationState.state){
ApplicationState.state = new State();
}
return ApplicationState.state;
}
}
from typescript-book.
@thefabulousdev one of the goals of the singleton pattern is to prevent any mistaken duplicate instances. Your code doesn't prevent that as new State(1);new State(2)
would be allowed.
Of course it can be messaged to do the right thing but I will probably stick with Ryan's answer.
from typescript-book.
Whoops, I knew I had missed something, here's what I meant
class State {
constructor(protected count = 0){
}
getCount(){
return this.count;
}
}
class ApplicationState {
static state;
constructor(){
if(!ApplicationState.state){
ApplicationState.state = new State();
}
return ApplicationState.state;
}
}
var something = new ApplicationState();
var somethingElse = new ApplicationState();
console.log(something === somethingElse); // true
You can see it running here
But I'm fine with your outcome.
from typescript-book.
But I'm fine with your outcome.
Thanks 🌹
from typescript-book.
@thefabulousdev thanks. I've taken your feedback into account as well : https://basarat.gitbooks.io/typescript/content/docs/tips/singleton.html 🌹
from typescript-book.
Related Issues (20)
- Suggest type addition: never
- Wrong: All members must conform to the string index signature? HOT 1
- Ebook formats no longer available HOT 4
- Questions about section "Index Signatures"
- Link is broken HOT 1
- improve readability of tip in motivation and samples section of generics.md
- New section on Assertion Functions?
- Build fails at pdf file creation (calibre problem) HOT 4
- style guide doesn't provide a reason for prefering interface for object typing
- Example of "All members must conform to the string index signature" looks wrong to me
- [ FEATURE REQUEST ] TypeScript could fix broken "const" variable declaration in JavaScript HOT 2
- Getting started page "extension i wrote" link is broken
- Truthy conversion: swtich from double negation to Boolean?
- All actions/builds are failing. Cannot download .epub version. HOT 9
- Cannot download PDF version of the ebook HOT 1
- What is place in module lookup HOT 1
- Hi,I have a question about ts equal check. HOT 1
- PDF can not be build HOT 1
- Can we use your ts style guide in our project?
- Review links lead nowhere HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript-book.