views:

64

answers:

1

I want to create a factory which will create a smack XMPPConnection. The factory should return the real XMPPConnection or a NullObject if the connection could not be made.

However smack's XMPPConnection is a concrete class.

It does not implement any interfaces, so I can't use java dynamic proxy API to proxy the sucker. I could extend the XMPPConnection but that's not very elegant.

Are there any other options ? Note that the factory must never return a null !

+3  A: 

You would have to create your own interface that basically duplicates the XMPPConnection and create an Adapter that implements that interface and wrapps the real XMPPConnection class. That way you could have a NullXMPPConnection as well.

willcodejavaforfood
Jacques René Mesrine
If you are only creating this Connection in one place it is probably not worth it but if you use it all over the place I'd say yes. You can also add loads of Design Patterns to your CV :)
willcodejavaforfood
I count 3 places where I will need a XMPPConnection. Note that I am working in a GUICE environment. So the factory is helping me to inject the connections to different classes.
Jacques René Mesrine
I'd probably give it a go then unless it seems like too much work.
willcodejavaforfood
Nope. It looks pretty easy. Working on it right now. Thanks again.
Jacques René Mesrine