Categories
Eclipse

Snippet Eclipse pour créer un singleton

A avoir à tout prix dans sa configuration Eclipse, ce snippet permet de créer instantanément une implémentation du pattern Singleton pour une classe Java.

Pour l’ajouter, allez dans Window->Preferences->Java->Editor->Templates ; puis créez un nouveau snippet que vous appelerez… au hasard … “singleton” ! 🙂

Vous pouvez éventuellement vous créer des variantes pour avoir également une version thread-safe, une autre non “lazy-loadée”, etc…

private static ${enclosing_type} instance;

private ${enclosing_type}(){}

public static ${enclosing_type} getInstance(){
if(null == instance){
instance = new ${enclosing_type}();
}
return instance;
}

By Benjamin Cabé

Benjamin Cabé is a technology enthusiast with a passion for empowering developers to build innovative solutions. He has invented an award-winning open source and open hardware artificial nose that he likes to use as an educational platform for people interested in diving into the world of embedded development.
He is currently a Developer Advocate for the Zephyr Project at the Linux Foundation and lives near Toulouse, France, where he enjoys baking sourdough bread with the help of his artificial nose.

2 replies on “Snippet Eclipse pour créer un singleton”

Leave a Reply

Your email address will not be published. Required fields are marked *