Vincentian I've seen too many discussions about the differences between aggregation and composition in UML class diagrams. Indeed, every so often, someone comes and asks me what the difference in the code and explains his own theories on this issue, this debate is almost as extensive as that of extends and includes the use cases.
Let us begin, we must always remember that one of the biggest criticisms we get UML is that it has saved many ambiguities ... But not all! there are still concepts that lend themselves to dual interpretations, do not know if this will be one of them, but it looks like it discussed. On the other hand, we can make a little history, recalling that first existed the association, then there is the aggregation to represent a structural relationship container / contained and then as an "extension" of the latter composition is born.
To explain my point of view I'll take his hand, the class diagram I have used in another post and then I'll put the Person class code, which is what takes all the burden of the discussion
The code refers only to this model, and is very detailed, even with unnecessary things, or merely theoretical, but what I want is to fix a position, specific and definitive on the subject of relationships. important thing to keep in mind is that an object exists (say, alive, but this is not technically correct because it is not a thread) while there is a reference variable "point" (also correct that no java has pointers, heh) to the object in memory. Ie, they become eligible for deletion by the garbage collector when there is no variable "point" to that object.
import java.util.LinkedList;
import java.util.List;
public class Person {private String name;
private String name;
private List profiles = new LinkedList (); Private List
lugaresFrecuentes = new LinkedList ();
/ / Getters and Setters public String
getName () {return name;} public void
setNombre (String name) {this.name = name;} public String
getApellido () {return name;} public void
setApellido (String name) {this.apellido = name;}
/ / OJO not confuse these are just setters and getters for properties
getPerfiles public List () {return profiles; } public void
setPerfiles (List profiles) {this.perfiles = profiles;}
getLugaresFrecuentes public List () {return lugaresFrecuentes;} public void
setLugaresFrecuentes (List lugaresFrecuentes) {this.lugaresFrecuentes = lugaresFrecuentes;}
The class usually begins with the declaration of instance variables. Where profiles and lugaresFrecuentes are two collections, but quietly could be arrays. to be transformed into containers of items. As properties have getters and setters (accessors and mutators), which have nothing to do with the aggregation and composition.
Now let's see what are the methods that characterize the relationship AGGREGATION
public void
agregarLugarFrecuenta (Place place)
{if (! lugaresFrecuentes.contains (place)) {
lugaresFrecuentes.add (location);
}} public void
removerLugarFrecuenta (Place place) {
if (lugaresFrecuentes.contains (place)) {
lugaresFrecuentes. remove (place);
}}
The first is that the class contains two methods one that adds items to the collection and another that it eliminates . I have something important here ... objects are passed as parameter have not been instantiated in the method, ie we have not made the new object. "Born" elsewhere and we've been through parameter the method to be added to the list lugaresFrecuentes. In other words, the object could die, and the object AHUN could maintain an active reference in any other part of our code so their life cycles are not tied streaks. Not born or dies within the Person.
What is the Difference STRUCTURE?
agregarPerfil public void () {
Profile p = new Profile ();
perfiles.add (p);}
/ / public void overload
agregarPerfil (String name) {
Profile p = new Profile (name )
perfiles.add (p);}
removerPerfil public void (int index) {
perfiles.remove (index); / / Here we remove it from the list}
Well ... the composition also has methods to add and delete. But ... "the new object is performed within the method to add"
instantiation of the object p is made within the add and the reference method does not return (that is void or boolean), the local reference variable will cease to exist once the method finishes running, and the life cycle of that particular instance will be tied to the list, and therefore the Person. Once the object is being referenced person not more (or die, although technically it is not) the object list be no reference, and therefore its elements as well. Furthermore the method is not static, it must first create an instance of Person, then you can add a profile. Starting and to "bind" the life cycle of a profile, that of a person. As for the method
removed, there is nothing extraordinary, simply remove an item from the list.
Deepening and playing with the theory, so that finally we tie the cycles of life, lugaresFrecuentes variable should not be property, and the Profile class should be an Inner Class . In
removed, there is nothing extraordinary, simply remove an item from the list.
getting paranoid theory of paranoia
Deepening and playing with the theory, so that finally we tie the cycles of life, lugaresFrecuentes variable should not be property, and the Profile class should be an Inner Class . In
Inner case of class, we do this so that they have to use an instance of the class "Outer" and then get an instance of class Inner. For example if in our code, the class profile, has an Inner class public class Person (means no, "that is within the file that Persona.java) to get Profile instance out of class person would to do:
Person person = new Person ();
Persona.Perfil persona.new profile = Profile ();
Profile
And the kind exist as long as the instance of person. (Life cycles tied)
For instance variable that should not have lugaresFrecuentes public getters and setters, this is because no other class, except for the Person class, should have the opportunity to hold a live reference to an object container. And much less get all the list from the outside! A Profile object lives and dies with the person!
this also may release other delusions, such as questions of inheritance and the like.
But again, and I will not tire of saying ... This is an extreme!, it's just to chat with friends, or boast with a professor, there is nothing practical, or real, except in specific cases.
"They thought we were done? can be even more paranoid!! much has been discussed on these issues, and much was paranoia theoretical. The following is something that will take them to your friends and teacher to say, "... well but that's crazy"
override the finalize () method of class Person, which is a method that all classes inherit Object, and is invoked just before an object is erased from memory by the garbage collector for java.
unreferenced In each of the items in the list a second before the object type person disappears from memory, a millisecond, or something! finally tying the birth and death, the life cycle of a content item with its container.
But again! PARANOIA IN THE CODE IS NOT GOOD! only serves those nights of binge drinking among programmers, which closed bowling and paint stay home with friends.
I rescue all this biri-biri that "the new method is performed within the" and nothing more!
But again, and I will not tire of saying ... This is an extreme!, it's just to chat with friends, or boast with a professor, there is nothing practical, or real, except in specific cases.
Paranoia Paranoia of
"They thought we were done? can be even more paranoid!! much has been discussed on these issues, and much was paranoia theoretical. The following is something that will take them to your friends and teacher to say, "... well but that's crazy"
override the finalize () method of class Person, which is a method that all classes inherit Object, and is invoked just before an object is erased from memory by the garbage collector for java.
public void finalize () {
for (Profile p: profiles) {
p = null;
}}
unreferenced In each of the items in the list a second before the object type person disappears from memory, a millisecond, or something! finally tying the birth and death, the life cycle of a content item with its container.
But again! PARANOIA IN THE CODE IS NOT GOOD! only serves those nights of binge drinking among programmers, which closed bowling and paint stay home with friends.
I rescue all this biri-biri that "the new method is performed within the" and nothing more!
0 comments:
Post a Comment