Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

softCours design pattern m youssfi partie 9 creation des objets abstract factory singleton prototype

17,349 views

Published on

Design patterns : Création d'objets
- Abstract Factory
- Singleton
- Prototype

Published in: Software
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

softCours design pattern m youssfi partie 9 creation des objets abstract factory singleton prototype

  1. 1. DDeessiiggnn PPaatttteerrnnss PPaarrtt 99 MMoohhaammeedd YYoouussssffii Laboratoire Signaux Systèmes Distribués et Intelligence Artificielle (SSDIA) ENSET, Université Hassan II Casablanca, Maroc Email : med@youssfi.net Supports de cours : http://fr.slideshare.net/mohamedyoussfi9 Chaîne vidéo : http://youtube.com/mohamedYoussfi Recherche : http://www.researchgate.net/profile/Youssfi_Mohamed/publications med@youssfi.net
  2. 2. Patterns de PPPPPPPaaaaaaatttttttttttttteeeeeeerrrrrrrnnnnnnnsssssss dddddddeeeeeee ccccccccrrrrrrrrééééééééaaaaaaaattttttttiiiiiiiioooooooonnnnnnnn dddddddd’’’’’’’’oooooooobbbbbbbbjjjjjjjjeeeeeeeettttttttssssssss :::::::: - AAbbssttrraaccttFFaaccttoorryy -- SSSSiiiinnnngggglllleeeettttoooonnnn - PPrroottoottyyppee Mohamed Youssfi med@youssfi.net med@youssfi.net
  3. 3. Fabrique abstraite ((AAbbssttrraacctt FFaaccttoorryy ) Catégorie : ◦ Création OBJECTIFS : ◦ Fournir une interface pour créer des objets d'une même famille sans préciser leurs classes concrètes. RREESSUULLTTAATT :: ◦ Le Design Pattern permet d'isoler l'appartenance à une famille de classes. med@youssfi.net
  4. 4. DDiiaaggrraammmmee ddee ccllaasssseess med@youssfi.net
  5. 5. AAbbssttrraacctt FFaaccttoorryy RESPONSABILITES : ◦ AbstractFactory : définit l'interface des méthodes de création. ◦ FactoryImplA et FactoryImplB : implémentent l'interface et instancient la classe concrète appropriée. ◦ AbstractPlugin : définit l'interface d'un type d'objet instancié. ◦ PPlluuggiinnAA eett PPlluuggiinnBB :: ssoonntt ddeess ssoouuss--ccllaasssseess ccoonnccrrèètteess.. EElllleess sont instanciées respectivement par par les FactoryImplA et FactoryImplB ◦ La partie cliente fait appel à une Fabrique pour obtenir une nouvelle instance d'AbstractPlugin. L'instanciation est transparente pour la partie cliente. Elle manipule une AbstractPlugin. med@youssfi.net
  6. 6. IImmpplléémmeennttaattiioonn AbstractPlugin.java package dp; public interface AbstractPlugin { public void traitement(); } AbstractFactory.java package dp; public interface AbstractFactory { public AbstractPlugin getInstence(); med@youssfi.net }
  7. 7. IImmpplléémmeennttaattiioonn PluginImplA.java package dp; public class PluginImplA implements AbstractPlugin { @Override public void traitement() { System.out.println(Traitement par le plugin A .....); } } package dp; public class PluginImplB implements AbstractPlugin { @Override public void traitement() { System.out.println(Traitement par le plugin B .....); } } med@youssfi.net PluginImplB.java
  8. 8. IImmpplléémmeennttaattiioonn FactoryImplA.java package dp; public class FactoryImplA implements AbstractFactory { @Override public AbstractPlugin getInstence() { return new PluginImplA(); } } package dp; public class FactoryImplB implements AbstractFactory { @Override public AbstractPlugin getInstence() { return new PluginImplB(); } } med@youssfi.net FactoryImplB.java
  9. 9. IImmpplléémmeennttaattiioonn Application.java package dp; public class Application { public static void main(String[] args) throws Exception{ // Instanciation statique de la fabrique AbstractFactory factory=new FactoryImplA(); AbstractPlugin plugin=factory.getInstence(); plugin.traitement(); // Instanciation dynamique de la fabrique factory =(AbstractFactory) Class.forName(dp.FactoryImplB).newInstance(); plugin=factory.getInstence(); plugin.traitement(); } } Traitement par le plugin A ..... Traitement par le plugin B .....
  10. 10. PPAATTTTEERRNN SSIINNGGLLEETTOONN med@youssfi.net
  11. 11. PPaatttteerrnn SSiinngglleettoonn Le pattern Singleton permet de garantir la création d’une instance unique d’une classe durant toute la durée d’exécution d’une application LLee ppaatttteerrnn SSiinngglleettoonn ffaaiitt ppaarrttiiee ddeess ppaatttteerrnnss Fabrique. Il très exploité dans les application qui s’exécutent dans un environnement multi-thread. med@youssfi.net
  12. 12. PPaatttteerrnn SSiinngglleettoonn Catégorie : ◦ Création OBJECTIFS : ◦ Restreindre le nombre d' instances d'une classe à une et une seule. ◦ Fournir une méthode pour accéder à cette instance unique. RAISONS DE L'UTILISER : ◦ La classe ne doit avoir qu'une seule instance. ◦ CCeellaa ppeeuutt êêttrree llee ccaass dd''uunnee rreessssoouurrccee ssyyssttèèmmee ppaarr eexxeemmppllee.. ◦ La classe empêche d'autres classes de l'instancier. Elle possède la seule instance d'elle-même et fournit la seule méthode permettant d'accéder à cette instance. RESULTAT : ◦ Le Design Pattern permet d'isoler l'unicité d'une instance. RESPONSABILITES : ◦ Singleton doit restreindre le nombre de ses propres instances à une et une seule. Son constructeur est privé : cela empêche les autres classes de l'instancier. La classe fournit la méthode statique getInstance() qui permet d'obtenir l'instance unique. med@youssfi.net
  13. 13. DDiiaaggrraammmmee ddee ccllaasssseess med@youssfi.net
  14. 14. Exemple dd’’iimmpplléémmeennttaattiioonn public class Singleton { private static final Singleton instance; private int compteur; static { instance=new Singleton(); } private Singleton() { System.out.println(Instanciation); } public static Singleton getInstance(){ return instance; } public void traiter(String tache){ System.out.println(---------- Tâche +tache+----------); for(int i=1;i=5;i++){ ++compteur; System.out.print(.+compteur); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace();} } System.out.println(); System.out.println(Compteur=+compteur); } } med@youssfi.net
  15. 15. AApppplliiccaattiioonn public class Application { public static void main(String[] args) { for(int i=1;i=5;i++){ Singleton singleton=Singleton.getInstance(); singleton.traiter(T+i); } }} IInnssttaanncciiaattiioonn ----------------Tâche T1------------ .1.2.3.4.5 Compteur=5 ----------------Tâche T2------------ .6.7.8.9.10 Compteur=10 ----------------Tâche T3------------ .11.12.13.14.15 Compteur=15 ----------------Tâche T4------------ .16.17.18.19.20 Compteur=20 ----------------Tâche T5------------ .21.22.23.24.25 Compteur=25 med@youssfi.net
  16. 16. Application multi tthhrreeaaddss eett llee ssiinngglleettoonn Dans une application multi thread, le fait que plusieurs threads utilisent un singleton, peut engendrer des problèmes de synchronisation. Pour éviter le problème, les méthodes du singleton peuvent être déclarée synchronized. Ceci entraine la création d’une file d’attente au niveau de l’accès à ces méthodes. Ce qui handicape les performances de l’application. med@youssfi.net
  17. 17. Exemple d’application mmuullttii tthhrreeaaddss public class ThreadedTask extends Thread { private String taskName; public ThreadedTask(String taskName) { this.taskName = taskName; } @@OOvveerrrriiddee public void run() { Singleton singleton=Singleton.getInstance(); singleton.traiter(taskName); } } med@youssfi.net
  18. 18. Exemple d’application mmuullttii tthhrreeaaddss public class ThreadedTask extends Thread { private String taskName; public ThreadedTask(String taskName) { this.taskName = taskName; } @@OOvveerrrriiddee public void run() { Singleton singleton=Singleton.getInstance(); singleton.traiter(taskName); } } med@youssfi.net
  19. 19. Exemple d’application mmuullttii tthhrreeaaddss public class AppMultiThread { public static void main(String[] args) { for(int i=1;i=5;i++){ ThreadedTask t=new ThreadedTask(T+i); t.start(); } } } Instanciation ---------------- Tâche T1------------ .1---------------- Tâche T3------------ ---------------- Tâche T5------------ .2.3---------------- Tâche T4------------ .4---------------- Tâche T2------------ .5.6.8.9.7.10.11.12.14.13.15.16.18.18.18.19.20.21.23.23.24 Compteur=24 Compteur=24 Compteur=24 Compteur=24 Compteur=24 ? med@youssfi.net
  20. 20. Solution ddee ssyynncchhrroonniissaattiioonn public synchronized void traiter(String tache){ Instanciation ----------------Tâche T1------------ .1.2.3.4.5 Compteur=5 --------------------------------TTââcchhee TT44------------------------ :ThradedTask .6.7.8.9.10 Compteur=10 ----------------Tâche T5------------ .11.12.13.14.15 Compteur=15 ----------------Tâche T3------------ .16.17.18.19.20 Compteur=20 ----------------Tâche T2------------ .21.22.23.24.25 Compteur=25 :Singleton :ThradedTask :ThradedTask :ThradedTask :ThradedTask med@youssfi.net
  21. 21. PPAATTTTEERRNN PPRROOTTOOTTYYPPEE med@youssfi.net
  22. 22. PPaatttteerrnn PPrroottoottyyppee Catégorie : ◦ Création Objectifs : ◦ L’objectif du pattern PROTOTYPE est de fournir de nouveaux objets par la copie d’un exemple plutôt que de produire de nouvelles instances non initialisées d’une classe. RRaaiissoonnss dd’’uuttiilliissaattiioonn ◦ Le système doit créer de nouvelles instances, mais il ignore de quelle classe. Il dispose cependant d'instances de la classe désirée. ◦ La duplication peut être également intéressante pour les performances (la duplication est plus rapide que l'instanciation). Résultat : ◦ Le Design Pattern permet d'isoler l'appartenance à une classe. med@youssfi.net
  23. 23. PPrroottoottyyppee Responsabilités : ◦ Prototype : définit l'interface de duplication de soi-même. ◦ PrototypeImplA et PrototypeImplA : sont ddeess ssoouuss--ccllaasssseess ccoonnccrrèètteess ddee PPrroottoottyyppee.. EElllleess implémentent l'interface de duplication. ◦ La partie cliente appelle la méthode clone() de la classe Prototype. Cette méthode retourne un double de l'instance. med@youssfi.net
  24. 24. DDiiaaggrraammmmee ddee ccllaasssseess med@youssfi.net
  25. 25. EExxeemmppllee dd’’iimmpplléémmeennttaattiioonn med@youssfi.net
  26. 26. PPaatttteerrnn PPrroottoottyyppee Point.java package dp; public class Point { protected int x; protected int y; public Point(int x, int y) { this.x = x; this.y = y; } public double distance(Point p){ int a=p.x-this.x; int b=p.y-this.y; return Math.sqrt(a*a+b*b); } @Override public String toString() { return Point [x= + x + , y= + y + ]; } } med@youssfi.net
  27. 27. PPaatttteerrnn PPrroottoottyyppee Figure.java package dp; public abstract class Figure implements Cloneable { protected Point p1; protected Point p2; public Figure(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } @Override protected Figure clone() throws CloneNotSupportedException { return (Figure)super.clone(); } public abstract double getSurface(); @Override public String toString() { return p1= + p1 + , p2= + p2; } } med@youssfi.net
  28. 28. PPaatttteerrnn PPrroottoottyyppee Cercle.java package dp; public class Cercle extends Figure { public Cercle(Point p1, Point p2) { super(p1, p2); } @Override public double getSurface() { double r=p1.distance(p2); return Math.PI*r*r; } @Override public String toString() { return Cercle [ + super.toString() + ]; } } med@youssfi.net
  29. 29. PPaatttteerrnn PPrroottoottyyppee Rectangle.java package dp; public class Rectangle extends Figure { public Rectangle(Point p1, Point p2) { super(p1, p2); } @Override public double getSurface() { int l=p1.x-p2.x; int h=p1.y-p2.y; return l*h; } @Override public String toString() { return Rectangle [ + super.toString() + ]; } } med@youssfi.net
  30. 30. PPaatttteerrnn PPrroottoottyyppee Application.java package dp; public class Application { public static void main(String[] args) throws CloneNotSupportedException { Figure f1=new Cercle(new Point(10, 10), new Point(20, 20)); Figure f2=new Rectangle(new Point(30, 30), new Point(40, 40)); System.out.println(Surface de +f1+ est :+f1.getSurface()); System.out.println(Surface de +f2+ est :+f2.getSurface()); System.out.println(----------------------------------------); Figure f3=f1.clone(); System.out.println(Copie de f1 :); System.out.println(Surface de +f3+ est :+f3.getSurface()); System.out.println(----------------------------------------); Figure f4=f2.clone(); System.out.println(Copie de f2 :); System.out.println(Surface de +f4+ est :+f4.getSurface()); }} Surface de Cercle [p1=Point [x=10, y=10], p2=Point [x=20, y=20]] est :628.3185307179587 Surface de Rectangle [p1=Point [x=30, y=30], p2=Point [x=40, y=40]] est :100.0 ---------------------------------------- Copie de f1 : Surface de Cercle [p1=Point [x=10, y=10], p2=Point [x=20, y=20]] est :628.3185307179587 ---------------------------------------- Copie de f2 : Surface de Rectangle [p1=Point [x=30, y=30], p2=Point [x=40, y=40]] est :100.0 med@youssfi.net

×