This is the first time, I am involved in writing a complete client for an external server (I am very very new to design pattern).
This server provides couple of protocols to connect and send command e.g. REST, SOAP, and few more.
All of these protocols performs almost same set of actions, but it differes. I need to design and implement a complete client framework for it which will support all these protocols.
As understand and gone through couple of internet links, it seems to me to use an abstract factory pattern and interfaces for it.
my current thought for implementating it is as follows:
-
Create a abstract factory class for Connection (ConnectionFactory). Based on the input, mentioning the protocol to use, the respective Connection object will be created. This abstract class will have an abstract method (processMessage). This method must be implemented in all the protocol’s connection class. This single method (processMessage) will take a arugument mentioning th type of request to execute. There is a separate request name for each protocol. How can I take care of it using constant?
-
Define an interface for Request, Response and Client. All of the protocols will have their own Request, Response and Client class which will implement their respective interfaces.
Please provide me your valuable input about this design; please suggest me something better I can do it here.
I am still not able to finalize the directory structure, please help me for the same.
If you are planning on defining a class hierarchy for the different protocols, try to avoid creating a parallel hierarchy for the data types (Request, Response, etc). This is often considered an anti-pattern and is referred to as a “parallel inheritance hierarchy”. Here’s an example question. The main drawback to this approach is having to maintain multiple parallel class hierarchies.
Creating the Connection factory sounds reasonable. It should most likely return a class instance that has methods createMessage() for sending messages to the server and processMessage() for receiving messages from the server and the factory will plugin the ProtocolHandler explained next.
As for the Request and Response you could define a ProtocolHandler member in the Connection class using a Strategy pattern, whereby each implementation would be a class that can handle, parse, marshal, etc the details of the respective protocol (REST, SOAP, etc). The Connection class processMessage() and createMessage() methods would use the ProtocolHandler class hierarchy.
Here is some pseudo-code in c++, I havent compiled nor tested it, but I hope it gives you a good idea of what Im trying to explain.