简体   繁体   中英

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! :)

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.

战略

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

我想到了战略模式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM