1

Say I have some code which consumes a class called Subject, which implements ISubject. Would there be any concerns if I were to build a ProxySubject, which inherits Subject?

I like this style because it makes it so simple to wrap the real subject: ProxySubject inherits Subject and overrides the required fields (mostly relying upon ThisBase.ThatOverridenProperty). If the targeted fields are not virtual (not overridable), then you can always re-implement ISubject directly from ProxySubject.

I did not find any suggestions for doing so; which makes me wonder it might actually not be such a good idea?

Ama
  • 247
  • 1
  • 7
  • 1
    *Why* do you want to use the proxy pattern here? It sounds like you want ordinary inheritance, without any particular design patterns? – amon Mar 20 '20 at 11:33
  • Understand [the yo-yo problem](https://en.m.wikipedia.org/wiki/Yo-yo_problem) before doing this. – candied_orange Mar 20 '20 at 11:58
  • @Amon, for example to cache some fields: the field returns a [value holder](https://en.wikipedia.org/wiki/Lazy_loading#Value_holder), which itself references the field in the base class. This way the base class implements the behaviour without any concerns for costly operations, and then the proxy wrapps the required fields. – Ama Mar 20 '20 at 13:12
  • @candied_orange Actually, this is precisely what I wish to avoid: by inheriting and overriding, you do not need for example to re-assign the interface implementation. The behaviour and the interface assignment are handled by `RealSubject`, whilst `ProxySubject` only overrides where required, solely for the purpose of proxy-ing. – Ama Mar 20 '20 at 13:16
  • One obvious disadvantage is that you cannot rely on lazy instantiation, since instantiating the proxy subject will inevitably instantiate the real subject. – Ama Mar 20 '20 at 13:18

0 Answers0