Relações (composição e agregação)

101 visualizações

Publicada em

Relações (composição e agregação)

Publicada em: Tecnologia
  • Seja o primeiro a comentar

Relações (composição e agregação)

  1. 1. Abstract classes and interfaces Sérgio Souza Costa Universidade Federaldo Maranhão 28 de junho de 2016 Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 1 / 17
  2. 2. Based in Dan Pilone. “UML 2.0 in a Nutshell.” Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 2 / 17
  3. 3. Summary Association Aggregation Composition Assignment Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 3 / 17
  4. 4. Dependency The weakest relationship between classes, means that one class uses, or has knowledge of, another class. It is typically a transient relationship, meaning a dependent class briefly interacts with the target class but typically doesn’t retain a relationship with it for any real length of time. Are typically read as "...uses a...”.For example, if you have a class named Window that sends out a class named WindowClosingEvent when it is about to be closed, you would say "Window uses a WindowClosingEvent.” You show a dependency between classes using a dashed line with an arrow pointing from the dependent class to the class that is used. Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 4 / 17
  5. 5. Association Associations are stronger than dependencies and typically indicate that one class retains a relationship to another class over an extended period of time. Are typically read as "...has a...". For example, if you have a class named Window that has a reference to the current mouse cursor, you would say "Window has a Cursor". . You show an association using a solid line between the classes participating in the relationship. Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 5 / 17
  6. 6. Navigability “Associations have explicit notation to express navigability. If you can navigate from one class to another, you show an arrow in the direction of the class you can navigate to. ” “Because you can’t navigate from an instance of Cursor to an instance of Window, we explicitly show the navigability arrow and an X where appropriate.” Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 6 / 17
  7. 7. Multiplicity You can express how many instances of a particular class are involved in a relationship. I You don’t specify a value, a multiplicity of 1 is assumed. To show a different value, simply place the multiplicity specification near the owned class. Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 7 / 17
  8. 8. Example Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 8 / 17
  9. 9. Example - Item public class Item { ... public Item(int c, int q, float v, Produto p){ codigo =c; qde = q; valor = v; prod = p; } public cal_valor(){ valor = qde * prog.get_preco(); } } Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 9 / 17
  10. 10. Example - Produto public class Produto { private int codigo; private String nome; private float preco; public Produto(int c, String n, float p){ codigo=c; nome=n; preco=p; } public double get_preco(){ return preco; } } Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 10 / 17
  11. 11. Example 2 Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 11 / 17
  12. 12. Example 2 - Cliente import java.util.Vector; public class Cliente{ private String nome; private String endereco; private Vector ped; public Cliente(String n,String e){ nome=n; endereco=e; ped=newVector(); } public int get_size(){ return ped.size(); } Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 12 / 17
  13. 13. Example 2 - Cliente public void add_pedido(Pedido p){ ped.addElement(p); } public Pedido get_pedido(int i){ return (Pedido)ped.elementAt(i); } public void exc_pedido(int id){ Pedido aux; int tam=ped.size(); for(inti=0;i<tam;i++){ aux=(Pedido)ped.elementAt(i); if(aux.get_id()==id){ ped.removeElementAt(i); } } }} Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 13 / 17
  14. 14. Agregation Is a stronger version of association. Unlike association, aggregation typically implies ownership and may imply a relationship between lifelines. Aggregations are usually read as "...owns a...". For example, if you had a classed named Window that stored its position and size in a Rectangle class, you would say the "Window owns a Rectangle.” In the association example, Window doesn’t own the Cursor; Cursor was shared between all applications in the system.” Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 14 / 17
  15. 15. Composition Composition represents a very strong relationship between classes, to the point of containment. Composition is used to capture a whole-part relationship. The lifetime of instances involved in composition relationships is almost always linked; if the larger, owning instance is destroyed, it almost always destroys the part piece. Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 15 / 17
  16. 16. Composition and the design patterns The Gang of Four suggests a few strategies for creating good object- oriented designs. In particular, they suggest the following: Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it. Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 16 / 17
  17. 17. Assignment Implementem as classes e suas relações como no diagrama abaixo. Sérgio Souza Costa (Universidade Federaldo Maranhão) Paradigmas de Programação 28 de junho de 2016 17 / 17

×