I’m trying to modulise legacy code and in doing so I’ve come across this problem:
I have two classes that belong in the same module – along with many other classes – and each class has one or more public methods used else-where. I want to create a general interface for the module but I’m not sure how to go about doing this. If I create one interface for the module I’ll end up having to implement blank methods in all the classes that use the interface, which doesn’t sound very good. However if I create multiple interfaces that can be used by specific classes within the module I’m left with combining them all inter one interface that is just used for type referencing:
/-------------\ /-------------\
| Interface A | | Interface B |
\-------------/ \-------------/
/-------------\ /-------------\
| Class A | | Class B |
\-------------/ \-------------/
/-------------\ /-------------\
| Interface A | | Interface B |
\-------------/ \-------------/
^ ^
| |
/------------------\
| Module Interface |
\------------------/
Are there any design patterns that will help me with this or is combining the interfaces that make up a specific module into a sub-interface to represent the module type the correct way to achieve this?
I’m not sure I really understand your problem but you can combine interfaces into one interface through inheritance. That is interfaces support multiple inheritance.
A pattern that might be applicable to you is the adapter pattern: http://en.wikipedia.org/wiki/Adapter_pattern
Also try not to make interfaces for the sake of making them. Java has this long standing history of trying to make interfaces for stuff that will never have another implementation. Unless your making an API consider KISS.