Passing reference data to class and incapsulation

mark_mcs via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 28 10:11:33 PST 2014


> Yes. Problem is even if you have property that controls correct 
> assignment. If you have getter that returns mutable reference 
> type and you try to access some fields of it or apply index 
> operator (for arrays or AA) *host* cannot control corectness of 
> these assignments or cannot react to these changes (in order to 
> do some changes in model).
>
> The way I see is to create my own type of Array for this that 
> can notify *host object* about changes so it could refresh the 
> model or do some checks.
>
> Another way is to write interfaces in a way so we cant't escape 
> uncontrolled references to mutable data.
>
> What do you think of it? Is there any patterns that could help 
> to solve such problems?
>
> I think that writing your own type every time when you need to 
> enforce consistency and safety is not very good))

In my opinion, this is an application design problem; not a 
language design problem.

Getters break encapsulation: they expose an object's internal 
representation to client code. I tend to try and confine them to 
DTOs, which are generally immutable anyway.

If you're worried about encapsulation, why not add a method to 
the object that modifies its own internal state?


More information about the Digitalmars-d-learn mailing list