Last active
June 30, 2023 14:23
-
-
Save maxgfr/443df4d17d3d5655dad39fb2e404b2fc to your computer and use it in GitHub Desktop.
Example of Singleton which generate one object or the other
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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