Skip to content

Instantly share code, notes, and snippets.

@maxgfr
Last active June 30, 2023 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxgfr/443df4d17d3d5655dad39fb2e404b2fc to your computer and use it in GitHub Desktop.
Save maxgfr/443df4d17d3d5655dad39fb2e404b2fc to your computer and use it in GitHub Desktop.
Example of Singleton which generate one object or the other
type Vehicule = "Voiture" | "Camion";
class Voiture {
public taille = "2m"
constructor() {
console.log("Je suis une voiture");
}
}
class Camion {
constructor() {
console.log("Je suis un camion")
}
public attacherRemorque() {
console.log("Le camion peut attacher une remorque")
}
}
type CamionOuVoiture = Voiture | Camion;
class Singleton{
private static instance: Singleton
private constructor() {
}
public static getInstance<T extends CamionOuVoiture>(vehicule: Vehicule) {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
if (vehicule === "Camion") {
return new Camion() as T;
} else if (vehicule === "Voiture") {
return new Voiture() as T;
} else {
throw new Error("Camion ou Voiture, il faut choisir")
}
}
}
const voiture = Singleton.getInstance<Voiture>("Voiture")
console.log(voiture.taille)
const camion = Singleton.getInstance<Camion>("Camion")
console.log(camion.attacherRemorque())
type Vehicule = "Voiture" | "Camion";
class Voiture {
public taille = "2m"
constructor() {
console.log("Je suis une voiture");
}
}
class Camion {
constructor() {
console.log("Je suis un camion")
}
public attacherRemorque() {
console.log("Le camion peut attacher une remorque")
}
}
type CamionOuVoiture = Voiture | Camion;
class Singleton{
private static instance: Singleton
private constructor() {
}
public static getInstance(vehicule: Vehicule) {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
if (vehicule === "Camion") {
return new Camion();
} else if (vehicule === "Voiture") {
return new Voiture();
} else {
throw new Error("Camion ou Voiture, il faut choisir")
}
}
}
const voiture = Singleton.getInstance("Voiture") as Voiture
console.log(voiture.taille)
const camion = Singleton.getInstance("Camion") as Camion;
console.log(camion.attacherRemorque())
type Vehicule = "Voiture" | "Camion";
class Voiture {
public taille = "2m"
constructor() {
console.log("Je suis une voiture");
}
}
class Camion {
constructor() {
console.log("Je suis un camion")
}
public attacherRemorque() {
console.log("Le camion peut attacher une remorque")
}
}
type CamionOuVoiture = Voiture | Camion;
class Singleton{
private static instance: Singleton
private constructor() {
}
public static getInstance(vehicule: Vehicule): CamionOuVoiture {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
if (vehicule === "Camion") {
return new Camion();
} else if (vehicule === "Voiture") {
return new Voiture();
} else {
throw new Error("Camion ou Voiture, il faut choisir")
}
}
}
const voiture = Singleton.getInstance("Voiture");
if(voiture instanceof Voiture)
console.log(voiture.taille)
const camion = Singleton.getInstance("Camion");
if(camion instanceof Camion)
console.log(camion.attacherRemorque())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment