In the project I am working on, I will have many objects each of which should be connectable and disconnectable from eachother. Each object will have several 'connectable interfaces', (i.e C1, C2, C3), that can be connected to one of the connection interfaces of a different object. The interfaces themselves are to exist regardless of whether a connection is present or not and should be addable/removable.
So for example... linking objects might look something like this:
obj1 = ConnectableObject()
obj1.addInterface(name=C1,someproperties)
obj2 = ConnectableObject()
obj2.addInterface(name=C2,someotherproperties)
obj1.connectTo(obj2,C1,C2) #Connects obj1 and obj2 through their interfaces C1 and C2
I was thinking of maybe creating a third class called 'linkage' that is created when the interfaces are connected and then could destroy itself upon being disconnected. This idea seemed like it might be overkill however, so I'm looking for feedback. Is there a recommend implementation or design pattern for this type of object interaction that I could draw on?
1 Answer
Sounds reasonable. You may want to look into Composite pattern and adopt it for your use. You haven't stated what you intend to do with your objects, assuming you will traverse them and perform operations according to the attached interfaces - you can use the Iterator pattern.
Note, however, that it's always a question of benefit vs. investment when considering the usage of design patterns - just like you stated, it might be an overkill and you should examine the pros and cons carefully.