views:

93

answers:

3

I have a Channel with some properties and a ChannelProxy extending Channel. This ChannelProxy holds the original values so that Optimistic Concurrency can be applied. You can switch between no concurrency check and Optimistic Concurrency check.

Now I want to implement a LazyLoadedChannel which can be a Channel or a ChannelProxy. Extending both is not possible. What's a good solution for this kind of problem?

I could create a LazyLoadedChannel (extending Channel) and a LazyLoadedChannelProxy (extending ChannelProxy), but that means that code will be duplicated in both classes.

+2  A: 

Looks like Decorator pattern is an option.

Dmitry Ornatsky
A: 

As said above (Dmitri) it looks like a decorator that you put over your channel. Assuming that your Proxy accesses the values only if needed - i.e. when they're about to be changed.

Rashack
So in you Decorator you'll need the same getters and setters as in the Channel (or ChannelProx), but forwarding them to the Channel instance? (except the ones you want to be lazyloaded).
Lieven Cardoen
Yes. I see Frederick gave you the guidance you needed ;-) I thought you were doing that in your Proxy since proxy is more or less kind of decorator.
Rashack
+2  A: 

Decorator Patterns sounds a good choice to me as well.

As an alternative, however, you could do this:

  1. Make your Channel configurable as lazy loaded or non-lazy-loaded. You could use a Strategy Pattern to achieve this.
  2. Once Channel is configurable thusly, ChannelProxy, since it is only a subclass, should be automatically configurable too. So you may not have to do anything more.
  3. Or : depending upon what 'loading' entails, ChannelProxy may have to provide its own LoadingStrategy class.
Frederick
Indeed, I had seen this in an example .NET project. I will try both of them to see what suits me more. thx.
Lieven Cardoen