Adapter Template Extension

I have an IA interface. Adapter B implements this interface. IZ is an interface that is implemented by the classes X , Y, and Z. IA is the interface with which my application negotiates with X, Y, and Z using adapter B. B contains an IZ instance (adapter template). Now, from adapter B, I want to access functions that are unique to X, but since it is connected to the IZ interface in the adapter, how can I access this function using clean ? Also, the IZ instance in adapter B is determined at run time based on some manual user activity. Is there a way that I can use dependency injection to create an IZ instance in adapter B (I want to avoid using the new operator)?
IA
|
B (has an IZ instance)

IZ
/ | \
Xyz

EDIT: There is a good chance that X ', Y', Z 'can get into the picture ... all with one IZ interface, but Y' can have one method (maybe not the same as X) that is required accessible from B. Both unique methods Y 'and X will adapt to some common method M in interface IA

I have no control over X, Y and Z or X ', Y', Z ', except that they can implement the IZ interface. But I have control over IA and B. I used the adapter template mainly so that new classes can adapt to my IA interface

+4
source share
1 answer

If your class B wants to access the public interface X , which is not displayed through IZ , then it is no longer an adapter for the IZ interface.

It seems to me that you need separate adapters for different types if they are not available through a common interface. Since your description shows that there are likely to be many common functions, you can use inheritance between adapters to avoid code duplication.

You will probably need a factory to create adapters based on the type of adaptable object. A factory can also be used to create an instance of X , Y & c and the adapter necessary for it based on user activity as necessary.

+2
source

Source: https://habr.com/ru/post/1401033/


All Articles