Forum

DIP Example + Pointers  

  RSS

kerys
(@kerys)
New Member
Joined:11 months  ago
Posts: 2
27/01/2018 4:12 pm  

Hi,

I'm not completely understanding the Account - Customer example for Dependency Inversion Principle. I see how it helped with not having forward declarations, but how did it actually solve the problem of circular pointers? Don't we still have a pointer from Customer to Account and from Account to Owner (which can be a customer)?

Thanks!

Kerys


ReplyQuote
Stephan Roth
(@stephan-roth)
Author Admin
Joined:1 year  ago
Posts: 2
27/01/2018 4:53 pm  

Hi Kerys,

first of all thank you for your question.

The answer to your question can be found in parentheses in your last sentence: Account has a pointer to Owner, which can be a Customer (or anything else that implements interface Owner, e.g. a mock object during testing).

But Account has no direct dependency relationship anymore to Customer. You can take a look into the top of  the file Account.h and there is no #include instruction required for a file named Customer.h, neither the Customer must be forward-declared. Also take a look at the direction of the arrows in Figure 6-6: The class Account has no longer any association to Customer, and the realization relationship also points away from the Customer.

Which concrete object is used as an Owner by an instance of an Account at runtime, is also determined at runtime.

I hope I have been able to help you with that answer. If there are any further questions, please ask!

Regards,
Stephan


ReplyQuote
kerys
(@kerys)
New Member
Joined:11 months  ago
Posts: 2
28/01/2018 6:13 pm  

Hi Stephan,

thanks for the answer. Actually, what I don't understand is how the objects are created and destroyed when being used. I would have assumed the following:

1. Create two new shared pointers for a new Account and a new Customer

2. Use the setOwner / setAccount functions -> shared pointers get copied

3. Now when the original copies of the shared pointers get out of scope, the Account and Customer memory doesn't get released because they still have a pointer pointing at each other, but we have lost all the references to them -> memory leak?

Where is my mistake in this reasoning?

Kerys

 

 


ReplyQuote
Share: