Association, aggregation, etc. in practice?

Asked

Viewed 248 times

2

I am studying POO and still can not see, in practice, the difference between them.

Basically everything ends up in one class having as attribute the instance of another class. But if it is Departamento and Professor would be aggregation. If it is Aluno and Professor is more to the association.

What differs is only in theory and concept? In practice it is the same thing?

Here’s a practical example I did:

    <?php
//Agregação
class Produto{
    public $nome;
    public $preco;
    function __construct($nome,$valor){
        $this->nome=$nome;
        $this->preco=$valor;
    }   
}


class Carrinho{
    public $produtos;
    public function add_produto(Produto $produto){
        $this->produtos[]=$produto;
    }
    public function print(){
        foreach ($this->produtos as $value ) {
                echo $value->nome;
        }
    }
}

$produto = new Produto("Teclado","50");
$produto2 = new Produto("Mouse","10");


$carrinho = new Carrinho();
$carrinho->add_produto($produto);
$carrinho->add_produto($produto2);

$carrinho->print();


?>
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

2

In practice you must learn the theory to understand this. People are so concerned with practice that they don’t understand anything they’re doing, they just follow cake recipe.

The theory was passed on What is the difference between Association, Aggregation and Composition in OOP?. There’s a link to another good question on the subject. The theory is very clear on the differences between them, if you had studied the theory would know this.

In its practical example may be an association or aggregation, there is no object property, but I do not know if there is dependency of the main object with the secondary, at least it is what can be inferred by universal knowledge, its requirement could be another (this is another mistake that people make in practice, apply the theory, which often it does not understand well without looking at the context, the practical requirement).

The Carrinho does not own the Produto, the latter exists regardless of the former, this is easy to say, I doubt it has a requirement that speaks anything different. But I can’t say if there is dependence on it, that is, if there is no product in the Carrinho what happens? If the Carrinho cannot exist without a Produto then it is an aggregation, but if it can continue to exist even without a product then it is an association.

Your code gives an indication that the Carrinho can exist even without a Produto in it, after all did not use a constructor that requires it, but the code is very incomplete so I do not know if I can infer anything from it. For example, there is no way to remove the product and it is valid that in the last withdrawal of Produto the Carrinho must be erased.

Without the right theory and requirements, you can’t do anything right in practice. Programming is not easy, modeling is very difficult and this is what counts in software development, and OOP requires even more to do right, so some more experienced people who do not follow cake recipe avoid where it does not create very great value in code.

In practice learned that uses attribute, but the theory uses the term field. So you’re doing it wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.