Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

design pattern for pluggable communications modules

I am designing my application to have several plugins that will provide different communication methods such as Bluetooth, TCP, UDP, XMPP, etc.

At the moment I have some of those communication methods included inside of the project. And I call one of those methods in a dirty way using simple switch cases.

Can you recommend me some design patterns I could apply here?

Thank you in advance! :)

like image 298
biquillo Avatar asked May 23 '26 18:05

biquillo


1 Answers

Enter Strategy design pattern.

Basically you have a "context" class which needs to perform an action in different ways depending on the scenario.

Your create an abstract strategy ( or a Java interface in this case ) defining the methods that concrete strategies should implement. You get your plugins to implement that interface, and in your first switch statement you create the correct concrete instance.

strategy

If you need to load them at runtime, you could use Class.forName

like image 61
OscarRyz Avatar answered May 25 '26 08:05

OscarRyz