Java-OCA-OCPP
Java-OCA-OCPP copied to clipboard
Refactor downcast of message types in higher level components
As I mentioned in #93, we downcast to check. This creates direct dependencies from higher level entities to lower level entities.
Here is an example where we downcast to check if it's an instance of a 3rd part library, thereby creating a hard dependency to that 3rd part library. In this case an XML component, and since XML is being replaced by json in the OCPP standard, this dependency is unfortunate:
if (payload instanceof Document) {
logger.trace("Receive a message: {}", SugarUtil.docToString((Document) payload));
} else {
logger.trace("Receive a message: {}", message);
}
I have written a short guide to Visitor pattern, that may be a good solution for this issue: https://github.com/ChargeTimeEU/Java-OCA-OCPP/wiki/Example-of-visitor-pattern-(In-C%23,-oh-my)
Any ideas and/or better approaches?
Is anyone of for a challenge?