3 added a note
source | link

It is difficult to recommend anything in particular because we are discussing only small amount of code. But there are some comments

In approach 01, I call addCustomer on shop.

In this case shop knows about customers. Customers doesn't know about shop. So, there is a one link.

In approach 02, I call addCustomer when actually creating a Customer object keeping a reference on Shop in every Customer.

In this case both shop knows about customers and customers know about shop. So, there are two links and the coupling between classes becomes even stronger (which is mostly bad)

I believe a third approach could be also possible, basically where shop and Customer communicate with events.

This relaxes the coupling somewhat, but does not remove it anyway.

For my understanding coupling should be removed as Shop is not referenced in ? Customer as in the second approach. Am I wrong?

Well, will it be removed or not depends on what exactly you do when event is received. Coupling is just knowledge. If after event is received, customer adds itself to the shop or stores inside itself a reference to the shop, coupling is still there.

There is also an indirect coupling. You are sending an event and this event is processed. It might be processed without involving actual object reference, but it is still there. Your system imposes knowledge about relation between these two classes.

So, you must give objects minimum knowledge about each other. But not less knowledge, that is required for them to function correctly.

It is difficult to recommend anything in particular because we are discussing only small amount of code. But there are some comments

In approach 01, I call addCustomer on shop.

In this case shop knows about customers. Customers doesn't know about shop. So, there is a one link.

In approach 02, I call addCustomer when actually creating a Customer object keeping a reference on Shop in every Customer.

In this case both shop knows about customers and customers know about shop. So, there are two links and the coupling between classes becomes even stronger (which is mostly bad)

I believe a third approach could be also possible, basically where shop and Customer communicate with events.

This relaxes the coupling somewhat, but does not remove it anyway.

For my understanding coupling should be removed as Shop is not referenced in ? Customer as in the second approach. Am I wrong?

Well, will it be removed or not depends on what exactly you do when event is received. Coupling is just knowledge. If after event is received, customer adds itself to the shop or stores inside itself a reference to the shop, coupling is still there.

So, you must give objects minimum knowledge about each other. But not less knowledge, that is required for them to function correctly.

It is difficult to recommend anything in particular because we are discussing only small amount of code. But there are some comments

In approach 01, I call addCustomer on shop.

In this case shop knows about customers. Customers doesn't know about shop. So, there is a one link.

In approach 02, I call addCustomer when actually creating a Customer object keeping a reference on Shop in every Customer.

In this case both shop knows about customers and customers know about shop. So, there are two links and the coupling between classes becomes even stronger (which is mostly bad)

I believe a third approach could be also possible, basically where shop and Customer communicate with events.

This relaxes the coupling somewhat, but does not remove it anyway.

For my understanding coupling should be removed as Shop is not referenced in ? Customer as in the second approach. Am I wrong?

Well, will it be removed or not depends on what exactly you do when event is received. Coupling is just knowledge. If after event is received, customer adds itself to the shop or stores inside itself a reference to the shop, coupling is still there.

There is also an indirect coupling. You are sending an event and this event is processed. It might be processed without involving actual object reference, but it is still there. Your system imposes knowledge about relation between these two classes.

So, you must give objects minimum knowledge about each other. But not less knowledge, that is required for them to function correctly.

2 Added an answer to a question from comment
source | link

It is difficult to recommend anything in particular because we are discussing only small amount of code. But there are some comments

In approach 01, I call addCustomer on shop.

In this case shop knows about customers. Customers doesn't know about shop. So, there is a one link.

In approach 02, I call addCustomer when actually creating a Customer object keeping a reference on Shop in every Customer.

In this case both shop knows about customers and customers know about shop. So, there are two links and the coupling between classes becomes even stronger (which is mostly bad)

I believe a third approach could be also possible, basically where shop and Customer communicate with events.

This relaxes the coupling somewhat, but does not remove it anyway.

For my understanding coupling should be removed as Shop is not referenced in ? Customer as in the second approach. Am I wrong?

Well, will it be removed or not depends on what exactly you do when event is received. Coupling is just knowledge. If after event is received, customer adds itself to the shop or stores inside itself a reference to the shop, coupling is still there.

So, you must give objects minimum knowledge about each other. But not less knowledge, that is required for them to function correctly.

It is difficult to recommend anything in particular because we are discussing only small amount of code. But there are some comments

In approach 01, I call addCustomer on shop.

In this case shop knows about customers. Customers doesn't know about shop. So, there is a one link.

In approach 02, I call addCustomer when actually creating a Customer object keeping a reference on Shop in every Customer.

In this case both shop knows about customers and customers know about shop. So, there are two links and the coupling between classes becomes even stronger (which is mostly bad)

I believe a third approach could be also possible, basically where shop and Customer communicate with events.

This relaxes the coupling somewhat, but does not remove it anyway.

So, you must give objects minimum knowledge about each other. But not less knowledge, that is required for them to function correctly.

It is difficult to recommend anything in particular because we are discussing only small amount of code. But there are some comments

In approach 01, I call addCustomer on shop.

In this case shop knows about customers. Customers doesn't know about shop. So, there is a one link.

In approach 02, I call addCustomer when actually creating a Customer object keeping a reference on Shop in every Customer.

In this case both shop knows about customers and customers know about shop. So, there are two links and the coupling between classes becomes even stronger (which is mostly bad)

I believe a third approach could be also possible, basically where shop and Customer communicate with events.

This relaxes the coupling somewhat, but does not remove it anyway.

For my understanding coupling should be removed as Shop is not referenced in ? Customer as in the second approach. Am I wrong?

Well, will it be removed or not depends on what exactly you do when event is received. Coupling is just knowledge. If after event is received, customer adds itself to the shop or stores inside itself a reference to the shop, coupling is still there.

So, you must give objects minimum knowledge about each other. But not less knowledge, that is required for them to function correctly.

1
source | link

It is difficult to recommend anything in particular because we are discussing only small amount of code. But there are some comments

In approach 01, I call addCustomer on shop.

In this case shop knows about customers. Customers doesn't know about shop. So, there is a one link.

In approach 02, I call addCustomer when actually creating a Customer object keeping a reference on Shop in every Customer.

In this case both shop knows about customers and customers know about shop. So, there are two links and the coupling between classes becomes even stronger (which is mostly bad)

I believe a third approach could be also possible, basically where shop and Customer communicate with events.

This relaxes the coupling somewhat, but does not remove it anyway.

So, you must give objects minimum knowledge about each other. But not less knowledge, that is required for them to function correctly.