From UML to Java and return (object-oriented modeling II) Bruce Eckel, Thinking in Java, 4th edition, PrenticeHall, New Jersey, cf. http://mindview.net/books/tij4 jvo@ualg.pt José Valente de Oliveira 6-1 Previously on OOP Problem: given a square, generate the tangential inner circumference UML Ponto Circunferência Quadrado 1
What class should be responsible for a given task? Q1: What should be the class responsible for checking whether two points really define a square? Re: Square; it is its class invariant. Q2: What should be the class responsible for computing the distance between two points? Re: Point. Why? Q3: What should be the class responsible for computing the center and radius of the circunference, given a square? Re: Two hypothesis: Square or Circunference. In general, we decide in favour of the class that have all the required information to solve the task. In this case, we decide by Square. jvo@ualg.pt José Valente de Oliveira 3-3 An initial class diagram Quadrado lado origem validapontos geracircunferencia Ponto x y soma Circunferencia centro raio 2
From class diagram to code Quadrado lado validapontos geracircunferencia origem Ponto x y soma centro Circunferencia raio A possible client Call method String tostring() from class Circunferencia 3
class Circunferencia public class Circunferencia { private Ponto centro; private float raio; public Circunferencia(Ponto c, float r) { if (r<=0) { System.err.println("ERRO: Circunferencia com raio negativo."); System.exit(1); centro=c; raio=r; public String tostring() { return centro.tostring() + " " + raio; Class Quadrado - Constructor public class Quadrado { private Ponto origem; private int lado; public Quadrado (Ponto A, Ponto B) { if (validapontos(a, B)== false) { System.err.println("Erro: os pontos "+ A + " e " + B + " nao definem um quadrado"); System.exit(1); int x = Math.min(A.getX(), B.getX()); int y = Math.min(A.getY(), B.getY()); origem = new Ponto(x, y); lado = Math.abs(A.getX()-B.getX()); public boolean validapontos(ponto A, Ponto B) { int lado1 = Math.abs(A.getX()-B.getX()); int lado2 = Math.abs(A.getY()-B.getY()); return lado1 == lado2; origem 4
Class Quadrado conclusion public class Quadrado { // public Circunferencia geracircunferencia() { float raio = lado / 2.0f; Ponto centro = origem.soma(new Ponto((int) raio, (int) raio)); Circunferencia c = new Circunferencia (centro, raio); return c; The previous class Ponto, with minor changes class Ponto { private int _x_, _y_; public Ponto() { _x_ = 0; _y_ = 0; public Ponto(int x, int y) { setx(x); sety(y); public int getx() { return _x_; public int gety() { return _y_; public void setx(int x) { assert x>0; _x_ = x; public void sety(int y) { assert y>0; _y_ = y; public double dist(ponto that) { int dx = this.getx() - that.getx(); int dy = this.gety() - that.gety(); return Math.sqrt(dx*dx+dy*dy); 5
Refining class Ponto class Ponto { // public Ponto soma(ponto that) { int x = this.getx() + that.getx(); int y = this.gety() + that.gety(); return new Ponto(x, y); Ponto A = new Ponto (1, 2); Ponto B = new Ponto (2, 0); Ponto C = A.soma(B); System.out.println(C); // (3, 2) public String tostring() { return "("+getx() + "," + gety()+")"; Documenting the code with an UML generator jvo@ualg.pt http://www.objectaid.com/ 3-12 6
The initial client What changes are needed to run this new client code? 7
Changes version 1.0 Quadrado q = new Quadrado (P1, P2); public class Circunferencia { //Circunferencia c = q.geracircunferencia (q); private Ponto centro; Circunferencia c = new Circunferencia (q); private float raio; // public Circunferencia (Quadrado q) { raio = q.getlado() / 2.0f; centro = q.getorigem().soma (new Ponto((int) raio, (int) raio)); public class Quadrado { private Ponto origem; private int lado; // public int getlado () { return this.lado; public Ponto getorigem () { return this.origem; Changes version 2.0 Quadrado q = new Quadrado (P1, P2); public class Circunferencia { //Circunferencia c = q.geracircunferencia (q); private Ponto centro; Circunferencia c = new Circunferencia (q); private float raio; // public Circunferencia (Quadrado q) { Circunferencia c = q. geracircunferencia (); centro = c.getcentro(); raio = c.getraio(); 8
Object diagrams jvo@ualg.pt José Valente de Oliveira 3-17 Object Diagrams Several ways of representing objects in UML: write only the class name preceded by a colon and underlined : Student write the name of specific object with it s class onestudent : Student multiple objects : : Student 9
A possible object diagram (A class diagram) The object diagram for the given problem instance jvo@ualg.pt José Valente de Oliveira 3-19 Object Diagram Captures instances and associations at a given point in execution time 10
Class Diagram Where are we in OOM? Modeling and UML, an Introduction Classes and relationship between classes: association, composition, and agregation Introduction to UML class and object diagrams A complete example Notion of design pattern The Expert pattern jvo@ualg.pt José Valente de Oliveira 3-22 11
Patterns: a first notion Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Pattern Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995 jvo@ualg.pt José Valente de Oliveira 3-23 Pattern Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice, (Christopher Alexander et al, A Pattern Language: Towns, Buildings, Construction, 1977) jvo@ualg.pt José Valente de Oliveira 3-24 12
Pattern Pattern is a named and well-known problem/solution pair that can be applied in new contexts, with advice on how to apply it in novel situations and discussion of its tradeoffs, implementations, variations, and so forth. (Craig Larman, Applying UML and Patterns, 1995) jvo@ualg.pt José Valente de Oliveira 3-25 The Expert pattern (padrão Especialista) Name: Problem: Solution: (advice) Expert What is a basic principle by which to assign responsibilities to objects? Assign a responsibility to the class that has the information needed to fulfill it. jvo@ualg.pt José Valente de Oliveira 3-26 13
The Expert pattern (padrão Especialista) Discussão: O Expert leva-nos habitualmente a programas onde o objeto no programa tem o comportamento que é normalmente encontrado no objeto do mundo real. O Expert promove o encapsulamento, uma vez que o objeto usa os seus próprios dados para realizar as suas tarefas. jvo@ualg.pt José Valente de Oliveira 3-27 Design patterns A design pattern captures design expertise abstracted from existing design examples Design patterns allow to reuse design expertise Design patterns allow one to study how experts do design jvo@ualg.pt José Valente de Oliveira 3-28 14