Let’s say I’m writing an interface for a class A that keeps a list of objects of type B, and I want to be able to manipulate that list through class A’s interface. I could put add_B, remove_B, etc. methods to A’s interface, but that’s a lot of code duplication (this situation occurs in many classes in my programme), so I’d rather return a reference to the list itself. That, however, would break encapsulation.
Is there a standard way to handle this situation?
Let’s say I’m writing an interface for a class A that keeps a list
Share
The code below is based on the proxy design pattern. It preserves encapsulation and avoids a bulky interface in ‘A’ by delegating the interface to the ‘proxy’ object.
Also note how the typedef allows freedom of changing ‘list’ to ‘vector’ or anything else.