Programmation et Conception Orientées Objet Correction du partiel
Transcription
Programmation et Conception Orientées Objet Correction du partiel
Programmation et Conception Orientées Objet Correction du partiel 2013 Bertrand Estellon Département Informatique et Interactions Aix-Marseille Université 19 novembre 2013 Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 1 / 20 Exercice 1 – Dessins et stratégies Nous avons la classe suivante : public class Graph { public enum Style { CIRCLE, LINE } private Point[] points ; private Style style ; public Graph(Style style, Point... points) { this.points = points ; this.style = style ; } /* ... */ } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 2 / 20 Exercice 1 – Dessins et stratégies Suite du code de la classe Graph : public class Graph { /* ... */ protected void paint(Graphics g, int h) { for (Point p : points) { switch (style) { case CIRCLE: g.fillOval(p.x - 5, h - p.y - 5, 10, 10); break ; case LINE: g.drawLine(p.x, h - p.y, p.x, h); break ; } } } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 3 / 20 Exercice 1 – Dessins et stratégies Question 1. Que doit-on modifier pour disposer d’un nouveau style d’affichage des points ? protected void paint(Graphics g, int h) { for (Point p : points) { switch (style) { /* ... */ case SQUARE: g.drawRect(p.x - 5, h - p.y - 5, 10, 10); break ; } } } public enum Style { CIRCLE, LINE, SQUARE } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 4 / 20 Exercice 1 – Dessins et stratégies Question 2. Quel(s) principe(s) SOLID est(sont) violé(s) par le code de la classe Graph ? ⇒ OCP et SRP (n’oubliez pas d’expliquer pourquoi !!) Question 3. (yuml.me) Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 5 / 20 Exercice 1 – Dessins et stratégies Question 4. public interface Style { void drawPoint(Graphics graphics, Point point, int height); } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 6 / 20 Exercice 1 – Dessins et stratégies Question 4. public class LineStyle implements Style { public void drawPoint(Graphics graphics, Point point, int height) { graphics.drawLine(point.x, height - point.y, point.x, height); } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 7 / 20 Exercice 1 – Dessins et stratégies Question 4. public class CircleStyle implements Style { public void drawPoint(Graphics graphics, Point point, int height) { graphics.fillOval(point.x - 5, height - point.y - 5, 10, 10); } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 8 / 20 Exercice 1 – Dessins et stratégies Question 4. public class Graph { private Point[] points ; private Style style ; public Graph(Style style, Point... points) { this.points = points ; this.style = style ; } protected void paint(Graphics graphics, int height) { for (Point point : points) style.drawPoint(graphics, point, height); } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 9 / 20 Exercice 2 – Décoration et composition public class Html { public enum Tag { BOLD, HYPERLINK } List<Tag> tags ; List<String> urls ; String text ; public Html(String text) { this.text = text ; tags = new ArrayList<Tag>(); urls = new ArrayList<String>(); } public void addTag(Tag tag, String url) { tags.add(tag); urls.add(url); } /* ... */ } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 10 / 20 Exercice 2 – Décoration et composition public class Html { /* ... */ public String code() { String res = "" ; for (int i = tags.size()-1 ; i>=0 ; i--) { Tag tag = tags.get(i); switch (tag) { case BOLD: res+="<b>" ; break ; case HYPERLINK: res+="<a href=\""+urls.get(i)+"\">" ; break ; } } res+=text ; for (int i = 0 ; i < tags.size(); i++) { Tag tag = tags.get(i); switch (tag) { case BOLD: res+="</b>" ; break ; case HYPERLINK: res+="</a>" ; break ; } } return res ; } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 11 / 20 Exercice 2 – Décoration et composition Question 1. Html html = new Html("site de AMU"); html.addTag(Html.Tag.BOLD, null); html.addTag(Html.Tag.HYPERLINK, "http://www.univ-amu.fr"); System.out.println(html.code()); produit la sortie : <a href="http://www.univ-amu.fr"><b>site de AMU</b></a> Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 12 / 20 Exercice 2 – Décoration et composition Question 2. Modifications pour prendre en charge la balise “<i></i>” : public enum Tag { BOLD, HYPERLINK, ITALIC } public String code() switch (tag) { case ITALIC } /* ... */ switch (tag) { case ITALIC } /* ... */ } {/* ... */ /* ... */ : res+="<i>" ; break ; /* ... */ : res+="</i>" ; break ; Principes SOLID violés : OCP et SRP (n’oubliez pas de justifier votre réponse à l’examen !) Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 13 / 20 Exercice 2 – Décoration et composition Question 3. (yuml.me) Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 14 / 20 Exercice 2 – Décoration et composition Question 4. public interface HtmlElement { public String code(); } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 15 / 20 Exercice 2 – Décoration et composition Question 4. public class BoldElement implements HtmlElement { HtmlElement content ; public BoldElement(HtmlElement content) { this.content = content ; } @Override public String code() { return "<b>"+content.code()+"</b>" ; } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 16 / 20 Exercice 2 – Décoration et composition Question 4. public class HyperLinkElement implements HtmlElement { HtmlElement content ; String url ; public HyperLinkElement(HtmlElement content, String url){ this.content = content ; this.url = url ; } @Override public String code() { return "<a href=\""+url+"\">" + content.code() + "</a>" ; } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 17 / 20 Exercice 2 – Décoration et composition Question 4. public class TextElement implements HtmlElement { String text ; public TextElement(String text) { this.text = text ; } public String code() { return text ; } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 18 / 20 Exercice 2 – Décoration et composition Question 5. public class ListElement implements HtmlElement { private List<HtmlElement> items ; public ListElement() { items = new ArrayList<HtmlElement>(); } public void addItem(HtmlElement item) { items.add(item); } /* ... */ } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 19 / 20 Exercice 2 – Décoration et composition Question 5. public class ListElement implements HtmlElement { /* ... */ @Override public String code() { String result = "<ul>" ; for (HtmlElement item : items) result+="<li>"+item.code()+"</li>" ; result+="</ul>" ; return result ; } } Bertrand Estellon (DII – AMU) Partiel 2012 19 novembre 2013 20 / 20