Reputation: 4522
I have some hierarchy of classes. I want to implement deferred updating of any instance from this hierarchy. In other words, we should update an object in the only case where we will use any public method of the object. What is the best design pattern which allows to implement such a behaviour?
Here is a simple example of a such case:
I have a complex model, which cannot update a view (in performance purposes). Therefore the view should automatically update itself during access to any of its public methods.
Upvotes: 0
Views: 475
Reputation: 129
Proxy design pattern is used . It controls access to original object allowing you to perform either before or after request goes to original object. For more information you can refer this link below https://refactoring.guru/design-patterns/proxy
Upvotes: 1
Reputation: 55972
Model-view-viewmodel (MVVM) relates to what vz0 posted.
In this pattern the view observers the view model, and updates itself accordingly, allowing you to decouple the GUI from the data representation.
Upvotes: 0
Reputation: 17104
I am interpreting the phrase deferred updating to mean lazy loading. In that case, it sounds like you're describing the proxy pattern. A proxy is used to control access to another object or resource, and the first reason mentioned by the GoF book for controlling access to an object is,
...to defer the full cost of its creation and initialization until we actually need to use it.
In other words, you can update an object only when one of its public methods is actually called.
Upvotes: 2
Reputation: 32923
Design patters are rarely used for performance purposes. On the contrary, you break a pattern to get better performance.
To listen for a change in the model you use Observer.
To do some action when a method is invoked, you use a Proxy.
Upvotes: 2